Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LineChartView compile again #227

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftUICharts/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 39 additions & 11 deletions Sources/SwiftUICharts/LineChart/Line.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<CGRect>,
touchLocation: Binding<CGPoint>,
showIndicator: Binding<Bool>,
minDataValue: Binding<Double?>,
maxDataValue: Binding<Double?>,
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?
Expand All @@ -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))
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions Sources/SwiftUICharts/LineChart/LineChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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)%")
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand Down