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. diff --git a/Sources/SwiftUICharts/Helpers.swift b/Sources/SwiftUICharts/Helpers.swift index 5198c841..034d48b1 100644 --- a/Sources/SwiftUICharts/Helpers.swift +++ b/Sources/SwiftUICharts/Helpers.swift @@ -262,20 +262,20 @@ extension Color { } } -class HapticFeedback { +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 d29c1874..decfc818 100644 --- a/Sources/SwiftUICharts/LineChart/Line.swift +++ b/Sources/SwiftUICharts/LineChart/Line.swift @@ -9,24 +9,50 @@ 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 + + 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 } return frame.size.width / CGFloat(data.points.count-1) } + var stepHeight: CGFloat { var min: Double? var max: Double? @@ -49,10 +75,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)) @@ -62,7 +90,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 8e48faa1..ae2432d4 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: self.formSize.height + 30, alignment: .center) .shadow(color: self.style.dropShadowColor, radius: self.dropShadow ? 8 : 0) VStack(alignment: .leading){ if(!self.showIndicatorDot){ @@ -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)%") } } } @@ -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) @@ -142,10 +143,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) } }