From cdeec4cd4af2229522a6048d57ac8694b8771168 Mon Sep 17 00:00:00 2001 From: Albert Date: Mon, 17 May 2021 10:42:00 -0300 Subject: [PATCH 1/7] Fixing line chart view dynamic height --- Sources/SwiftUICharts/LineChart/LineChartView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftUICharts/LineChart/LineChartView.swift b/Sources/SwiftUICharts/LineChart/LineChartView.swift index 1e38b34f..ff2bf4c1 100644 --- a/Sources/SwiftUICharts/LineChart/LineChartView.swift +++ b/Sources/SwiftUICharts/LineChart/LineChartView.swift @@ -58,7 +58,7 @@ public struct LineChartView: View { ZStack(alignment: .center){ RoundedRectangle(cornerRadius: 20) .fill(self.colorScheme == .dark ? self.darkModeStyle.backgroundColor : self.style.backgroundColor) - .frame(width: frame.width, height: 240, alignment: .center) + .frame(width: frame.width, height: frame.height, alignment: .center) .shadow(color: self.style.dropShadowColor, radius: self.dropShadow ? 8 : 0) VStack(alignment: .leading){ if(!self.showIndicatorDot){ From 180df91f651b96a2a9acb7133307f37c67a12374 Mon Sep 17 00:00:00 2001 From: Albert Date: Mon, 17 May 2021 10:58:56 -0300 Subject: [PATCH 2/7] Fixing height to fit offset --- Sources/SwiftUICharts/LineChart/LineChartView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftUICharts/LineChart/LineChartView.swift b/Sources/SwiftUICharts/LineChart/LineChartView.swift index ff2bf4c1..9bf3762a 100644 --- a/Sources/SwiftUICharts/LineChart/LineChartView.swift +++ b/Sources/SwiftUICharts/LineChart/LineChartView.swift @@ -58,7 +58,7 @@ public struct LineChartView: View { ZStack(alignment: .center){ RoundedRectangle(cornerRadius: 20) .fill(self.colorScheme == .dark ? self.darkModeStyle.backgroundColor : self.style.backgroundColor) - .frame(width: frame.width, height: frame.height, alignment: .center) + .frame(width: frame.width, height: self.formSize.height + 30, alignment: .center) .shadow(color: self.style.dropShadowColor, radius: self.dropShadow ? 8 : 0) VStack(alignment: .leading){ if(!self.showIndicatorDot){ From 7fe65267ef9f22ba0dd352442c2025a05cbf1393 Mon Sep 17 00:00:00 2001 From: Albert Rayneer Date: Wed, 26 May 2021 10:17:33 -0300 Subject: [PATCH 3/7] Adding line chart view colors --- Sources/SwiftUICharts/LineChart/Line.swift | 2 +- Sources/SwiftUICharts/LineChart/LineChartView.swift | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftUICharts/LineChart/Line.swift b/Sources/SwiftUICharts/LineChart/Line.swift index d29c1874..7e752b3e 100644 --- a/Sources/SwiftUICharts/LineChart/Line.swift +++ b/Sources/SwiftUICharts/LineChart/Line.swift @@ -62,7 +62,7 @@ public struct Line: View { ZStack { if(self.showFull && self.showBackground){ self.closedPath - .fill(LinearGradient(gradient: Gradient(colors: [Colors.GradientUpperBlue, .white]), startPoint: .bottom, endPoint: .top)) + .fill(LinearGradient(gradient: Gradient(colors: [gradient.start, gradient.end]), startPoint: .bottom, endPoint: .top)) .rotationEffect(.degrees(180), anchor: .center) .rotation3DEffect(.degrees(180), axis: (x: 0, y: 1, z: 0)) .transition(.opacity) diff --git a/Sources/SwiftUICharts/LineChart/LineChartView.swift b/Sources/SwiftUICharts/LineChart/LineChartView.swift index 9bf3762a..83f6eba3 100644 --- a/Sources/SwiftUICharts/LineChart/LineChartView.swift +++ b/Sources/SwiftUICharts/LineChart/LineChartView.swift @@ -105,7 +105,8 @@ public struct LineChartView: View { touchLocation: self.$touchLocation, showIndicator: self.$showIndicatorDot, minDataValue: .constant(nil), - maxDataValue: .constant(nil) + maxDataValue: .constant(nil), + gradient: self.style.gradientColor ) } .frame(width: frame.width, height: frame.height) From edd7ae1aa9fe28be15693e9bf474992eee354e75 Mon Sep 17 00:00:00 2001 From: Niklas Saers Date: Fri, 26 Nov 2021 10:08:51 +0100 Subject: [PATCH 4/7] Make LineChartView compile again --- Sources/SwiftUICharts/LineChart/LineChartView.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftUICharts/LineChart/LineChartView.swift b/Sources/SwiftUICharts/LineChart/LineChartView.swift index 8e48faa1..d4457923 100644 --- a/Sources/SwiftUICharts/LineChart/LineChartView.swift +++ b/Sources/SwiftUICharts/LineChart/LineChartView.swift @@ -76,12 +76,12 @@ public struct LineChartView: View { if let rateValue = self.rateValue { - if (rateValue ?? 0 >= 0){ + if (rateValue >= 0){ Image(systemName: "arrow.up") }else{ Image(systemName: "arrow.down") } - Text("\(rateValue!)%") + Text("\(rateValue)%") } } } @@ -142,10 +142,10 @@ public struct LineChartView: View { struct WidgetView_Previews: PreviewProvider { static var previews: some View { Group { - LineChartView(data: [8,23,54,32,12,37,7,23,43], title: "Line chart", legend: "Basic") + LineChartView(data: [8,23,54,32,12,37,7,23,43], title: "Line chart", legend: "Basic", rateValue: 1) .environment(\.colorScheme, .light) - LineChartView(data: [282.502, 284.495, 283.51, 285.019, 285.197, 286.118, 288.737, 288.455, 289.391, 287.691, 285.878, 286.46, 286.252, 284.652, 284.129, 284.188], title: "Line chart", legend: "Basic") + LineChartView(data: [282.502, 284.495, 283.51, 285.019, 285.197, 286.118, 288.737, 288.455, 289.391, 287.691, 285.878, 286.46, 286.252, 284.652, 284.129, 284.188], title: "Line chart", legend: "Basic", rateValue: 1) .environment(\.colorScheme, .light) } } From 9b21759c9abde02d2f0ee7e284c2195062adc0bc Mon Sep 17 00:00:00 2001 From: Niklas Saers Date: Fri, 26 Nov 2021 10:19:42 +0100 Subject: [PATCH 5/7] Just targetting modern builds --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 293b294d..1343ab29 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "SwiftUICharts", platforms: [ - .iOS(.v13), .watchOS(.v6), .macOS(.v11) + .iOS(.v14), .watchOS(.v7), .macOS(.v11) ], products: [ // Products define the executables and libraries produced by a package, and make them visible to other packages. From 31167ad9569c2da35172a56592b6657832aba23b Mon Sep 17 00:00:00 2001 From: Niklas Saers Date: Fri, 26 Nov 2021 13:56:53 +0100 Subject: [PATCH 6/7] Open up a bit --- Sources/SwiftUICharts/Helpers.swift | 2 +- Sources/SwiftUICharts/LineChart/Line.swift | 24 +++++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Sources/SwiftUICharts/Helpers.swift b/Sources/SwiftUICharts/Helpers.swift index 5198c841..a8732702 100644 --- a/Sources/SwiftUICharts/Helpers.swift +++ b/Sources/SwiftUICharts/Helpers.swift @@ -262,7 +262,7 @@ extension Color { } } -class HapticFeedback { +public class HapticFeedback { #if os(watchOS) //watchOS implementation static func playSelection() -> Void { diff --git a/Sources/SwiftUICharts/LineChart/Line.swift b/Sources/SwiftUICharts/LineChart/Line.swift index 7e752b3e..3e3410b7 100644 --- a/Sources/SwiftUICharts/LineChart/Line.swift +++ b/Sources/SwiftUICharts/LineChart/Line.swift @@ -9,24 +9,26 @@ import SwiftUI public struct Line: View { - @ObservedObject var data: ChartData - @Binding var frame: CGRect - @Binding var touchLocation: CGPoint - @Binding var showIndicator: Bool - @Binding var minDataValue: Double? - @Binding var maxDataValue: Double? + @ObservedObject public var data: ChartData + @Binding public var frame: CGRect + @Binding public var touchLocation: CGPoint + @Binding public var showIndicator: Bool + @Binding public var minDataValue: Double? + @Binding public var maxDataValue: Double? @State private var showFull: Bool = false - @State var showBackground: Bool = true - var gradient: GradientColor = GradientColor(start: Colors.GradientPurple, end: Colors.GradientNeonBlue) - var index:Int = 0 + @State public var showBackground: Bool = true + public var gradient: GradientColor = GradientColor(start: Colors.GradientPurple, end: Colors.GradientNeonBlue) + public var index:Int = 0 let padding:CGFloat = 30 - var curvedLines: Bool = true + public var curvedLines: Bool = true + var stepWidth: CGFloat { if data.points.count < 2 { return 0 } return frame.size.width / CGFloat(data.points.count-1) } + var stepHeight: CGFloat { var min: Double? var max: Double? @@ -49,10 +51,12 @@ public struct Line: View { } return 0 } + var path: Path { let points = self.data.onlyPoints() return curvedLines ? Path.quadCurvedPathWithPoints(points: points, step: CGPoint(x: stepWidth, y: stepHeight), globalOffset: minDataValue) : Path.linePathWithPoints(points: points, step: CGPoint(x: stepWidth, y: stepHeight)) } + var closedPath: Path { let points = self.data.onlyPoints() return curvedLines ? Path.quadClosedCurvedPathWithPoints(points: points, step: CGPoint(x: stepWidth, y: stepHeight), globalOffset: minDataValue) : Path.closedLinePathWithPoints(points: points, step: CGPoint(x: stepWidth, y: stepHeight)) From c582a1e1bbfb3de934b58f30d1d325f75fce72b4 Mon Sep 17 00:00:00 2001 From: Niklas Saers Date: Fri, 26 Nov 2021 14:15:09 +0100 Subject: [PATCH 7/7] Keep making public --- Sources/SwiftUICharts/Helpers.swift | 6 +++--- Sources/SwiftUICharts/LineChart/Line.swift | 24 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftUICharts/Helpers.swift b/Sources/SwiftUICharts/Helpers.swift index a8732702..034d48b1 100644 --- a/Sources/SwiftUICharts/Helpers.swift +++ b/Sources/SwiftUICharts/Helpers.swift @@ -265,17 +265,17 @@ extension Color { public class HapticFeedback { #if os(watchOS) //watchOS implementation - static func playSelection() -> Void { + public static func playSelection() -> Void { WKInterfaceDevice.current().play(.click) } #elseif os(iOS) //iOS implementation let selectionFeedbackGenerator = UISelectionFeedbackGenerator() - static func playSelection() -> Void { + public static func playSelection() -> Void { UISelectionFeedbackGenerator().selectionChanged() } #else - static func playSelection() -> Void { + public static func playSelection() -> Void { //No-op } #endif diff --git a/Sources/SwiftUICharts/LineChart/Line.swift b/Sources/SwiftUICharts/LineChart/Line.swift index 3e3410b7..decfc818 100644 --- a/Sources/SwiftUICharts/LineChart/Line.swift +++ b/Sources/SwiftUICharts/LineChart/Line.swift @@ -22,6 +22,30 @@ public struct Line: View { let padding:CGFloat = 30 public var curvedLines: Bool = true + init( + data: ChartData, + frame: Binding, + touchLocation: Binding, + showIndicator: Binding, + minDataValue: Binding, + maxDataValue: Binding, + showBackground: Bool = true, + gradient: GradientColor = GradientColor(start: Colors.GradientPurple, end: Colors.GradientNeonBlue), + index: Int = 0, + curvedLines: Bool = true + ) { + self.data = data + self._frame = frame + self._touchLocation = touchLocation + self._showIndicator = showIndicator + self._minDataValue = minDataValue + self._maxDataValue = maxDataValue + self.showBackground = showBackground + self.gradient = gradient + self.index = index + self.curvedLines = curvedLines + } + var stepWidth: CGFloat { if data.points.count < 2 { return 0