From 49c99b35d1a9d568d5947aeeed1024c2fa5bb7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Gabriel=20de=20Medeiros=20Pereira?= Date: Sun, 23 Aug 2020 19:25:32 -0300 Subject: [PATCH 1/5] add enum format --- .../SwiftUICharts/Base/Label/ChartLabel.swift | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift index 2a8fb07a..8cf66dd0 100644 --- a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift +++ b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift @@ -8,12 +8,26 @@ public enum ChartLabelType { case legend } +public enum ChartLabelFormat { + case custom(completion: (Double) -> String) + case none + + func format(value: Double) -> String { + switch self { + case .custom(let completion): + return completion(value) + case .none: + return String(format: "%.01f", value) + } + } +} + public struct ChartLabel: View { @EnvironmentObject var chartValue: ChartValue @State var textToDisplay:String = "" - + private var title: String - + private var format: ChartLabelFormat private var labelSize: CGFloat { switch labelType { case .title: @@ -61,9 +75,10 @@ public struct ChartLabel: View { } } - public init (_ title: String, + public init (_ title: String, format: ChartLabelFormat, type: ChartLabelType = .title) { self.title = title + self.format = format labelType = type } @@ -78,11 +93,17 @@ public struct ChartLabel: View { self.textToDisplay = self.title } .onReceive(self.chartValue.objectWillChange) { _ in - self.textToDisplay = self.chartValue.interactionInProgress ? String(format: "%.01f", self.chartValue.currentValue) : self.title + self.setTextToDisplay() } if !self.chartValue.interactionInProgress { Spacer() } } } + + func setTextToDisplay() { + self.textToDisplay = self.chartValue.interactionInProgress ? format.format(value: self.chartValue.currentValue) : self.title + } } + + From c5a61b1d2c01399ffefda5003cb2da35c489a6d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Gabriel=20de=20Medeiros=20Pereira?= Date: Sun, 23 Aug 2020 19:27:26 -0300 Subject: [PATCH 2/5] add default parameter --- Sources/SwiftUICharts/Base/Label/ChartLabel.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift index 8cf66dd0..53738b55 100644 --- a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift +++ b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift @@ -75,7 +75,7 @@ public struct ChartLabel: View { } } - public init (_ title: String, format: ChartLabelFormat, + public init (_ title: String, format: ChartLabelFormat = .none, type: ChartLabelType = .title) { self.title = title self.format = format From 9c6ce0dfd60ed4932fe5344ad3bf2d0a5c47df45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Gabriel=20de=20Medeiros=20Pereira?= Date: Sun, 23 Aug 2020 19:29:36 -0300 Subject: [PATCH 3/5] change other of parameters --- Sources/SwiftUICharts/Base/Label/ChartLabel.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift index 53738b55..af37c410 100644 --- a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift +++ b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift @@ -75,8 +75,8 @@ public struct ChartLabel: View { } } - public init (_ title: String, format: ChartLabelFormat = .none, - type: ChartLabelType = .title) { + public init (_ title: String, + type: ChartLabelType = .title, format: ChartLabelFormat = .none) { self.title = title self.format = format labelType = type From 541b1340bdd88a49a43698b047b09e2fba6ea70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Gabriel=20de=20Medeiros=20Pereira?= Date: Sun, 23 Aug 2020 19:45:50 -0300 Subject: [PATCH 4/5] adjust code --- Sources/SwiftUICharts/Base/Label/ChartLabel.swift | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift index af37c410..7199f378 100644 --- a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift +++ b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift @@ -78,8 +78,8 @@ public struct ChartLabel: View { public init (_ title: String, type: ChartLabelType = .title, format: ChartLabelFormat = .none) { self.title = title - self.format = format labelType = type + self.format = format } public var body: some View { @@ -93,17 +93,11 @@ public struct ChartLabel: View { self.textToDisplay = self.title } .onReceive(self.chartValue.objectWillChange) { _ in - self.setTextToDisplay() + self.textToDisplay = self.chartValue.interactionInProgress ? format.format(value: self.chartValue.currentValue) : self.title } if !self.chartValue.interactionInProgress { Spacer() } } } - - func setTextToDisplay() { - self.textToDisplay = self.chartValue.interactionInProgress ? format.format(value: self.chartValue.currentValue) : self.title - } } - - From 3c03e00aeb614fada7331078389910caf037c75d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Gabriel=20de=20Medeiros=20Pereira?= Date: Sun, 23 Aug 2020 21:02:15 -0300 Subject: [PATCH 5/5] add self --- Sources/SwiftUICharts/Base/Label/ChartLabel.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift index 7199f378..c9729b44 100644 --- a/Sources/SwiftUICharts/Base/Label/ChartLabel.swift +++ b/Sources/SwiftUICharts/Base/Label/ChartLabel.swift @@ -93,7 +93,7 @@ public struct ChartLabel: View { self.textToDisplay = self.title } .onReceive(self.chartValue.objectWillChange) { _ in - self.textToDisplay = self.chartValue.interactionInProgress ? format.format(value: self.chartValue.currentValue) : self.title + self.textToDisplay = self.chartValue.interactionInProgress ? self.format.format(value: self.chartValue.currentValue) : self.title } if !self.chartValue.interactionInProgress { Spacer()