Skip to content

Commit c6065f9

Browse files
authored
Add trim parameters to LineChartStyle (#15)
* Add `trim` params to LineChartStyle * update playground with example
1 parent 1d2062b commit c6065f9

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

Playgrounds/Charts.playground/Contents.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ struct ContentView: View {
1515

1616
@State var matrixData1: [[CGFloat]] = (0..<20).map { _ in (0..<3).map { _ in CGFloat.random(in: 0.00...0.33) } }
1717

18+
@State var trim: CGFloat = 0
19+
1820
var chart1: some View {
1921
HStack {
2022
VStack {
@@ -33,7 +35,7 @@ struct ContentView: View {
3335
VStack {
3436
Chart(data: data1)
3537
.chartStyle(
36-
LineChartStyle(.quadCurve, lineColor: .blue, lineWidth: 5)
38+
LineChartStyle(.quadCurve, lineColor: .blue, lineWidth: 5, trimTo: $trim)
3739
)
3840
.padding()
3941
.background(
@@ -42,6 +44,12 @@ struct ContentView: View {
4244
.stroke(Color.gray.opacity(0.1), style: .init(lineWidth: 2, lineCap: .round))
4345
)
4446
.frame(height: 300)
47+
.onAppear {
48+
trim = 0
49+
withAnimation(.easeInOut(duration: 3)) {
50+
trim = 1
51+
}
52+
}
4553

4654

4755
AxisLabels(.horizontal, data: 2010...2020, id: \.self) {

Sources/Charts/Chart/Styles/Line/LineChartStyle.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,33 @@ import SwiftUI
22
import Shapes
33

44
public struct LineChartStyle: ChartStyle {
5+
56
private let lineType: LineType
67
private let lineColor: Color
78
private let lineWidth: CGFloat
8-
9-
public func makeBody(configuration: Self.Configuration) -> some View {
9+
10+
@Binding private var trimFrom: CGFloat
11+
@Binding private var trimTo: CGFloat
12+
13+
@ViewBuilder
14+
public func makeBody(configuration: Configuration) -> some View {
1015
switch lineType {
1116
case .line:
12-
return AnyView(Line(unitData: configuration.dataMatrix.map { $0.reduce(0, +) })
13-
.stroke(self.lineColor, style: .init(lineWidth: self.lineWidth, lineCap: .round)))
17+
Line(unitData: configuration.dataMatrix.map { $0.reduce(0, +) })
18+
.trim(from: trimFrom, to: trimTo)
19+
.stroke(lineColor, style: .init(lineWidth: lineWidth, lineCap: .round))
1420
case .quadCurve:
15-
return AnyView(QuadCurve(unitData: configuration.dataMatrix.map { $0.reduce(0, +) })
16-
.stroke(self.lineColor, style: .init(lineWidth: self.lineWidth, lineCap: .round)))
21+
QuadCurve(unitData: configuration.dataMatrix.map { $0.reduce(0, +) })
22+
.trim(from: trimFrom, to: trimTo)
23+
.stroke(lineColor, style: .init(lineWidth: lineWidth, lineCap: .round))
1724
}
1825
}
1926

20-
public init(_ lineType: LineType = .quadCurve, lineColor: Color = .accentColor, lineWidth: CGFloat = 1) {
27+
public init(_ lineType: LineType = .quadCurve, lineColor: Color = .accentColor, lineWidth: CGFloat = 1, trimFrom: Binding<CGFloat> = .constant(0), trimTo: Binding<CGFloat> = .constant(1)) {
2128
self.lineType = lineType
2229
self.lineColor = lineColor
2330
self.lineWidth = lineWidth
31+
_trimFrom = trimFrom
32+
_trimTo = trimTo
2433
}
2534
}

0 commit comments

Comments
 (0)