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

y-mirrored area chart; fill support for stacked area #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ Chart(data: [0.1, 0.3, 0.2, 0.5, 0.4, 0.9, 0.1])
```swift
Chart(data: matrix)
.chartStyle(
StackedAreaChartStyle(.quadCurve, colors: [.yellow, .orange, .red])
StackedAreaChartStyle(.quadCurve, colors: [.yellow, .orange, .red], yMirror: false)
)

Chart(data: matrix)
.chartStyle(
StackedAreaChartStyle(.quadCurve, fills: [AnyView(Color.yellow), AnyView(Color.orange), AnyView(Color.red)], yMirror: true)
)
```

Expand Down
55 changes: 53 additions & 2 deletions Sources/Charts/Chart/Chart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Chart_Previews: PreviewProvider {
ColumnChartDemo()
BarChartDemo()
StackedAreaChartDemo()
StackedAreaChartDualDemo()
CompositeChartDemo()
}
}
Expand Down Expand Up @@ -100,7 +101,7 @@ private struct AreaChartDemo: View {
var body: some View {
Chart(data: data2)
.chartStyle(
AreaChartStyle(.quadCurve, fill: LinearGradient(gradient: .init(colors: [Color.red.opacity(0.8), Color.red.opacity(0.2)]), startPoint: .top, endPoint: .bottom))
AreaChartStyle(.quadCurve, fill: LinearGradient(gradient: .init(colors: [Color.red.opacity(0.8), Color.red.opacity(0.2)]), startPoint: .top, endPoint: .bottom), yMirror: false)
)
.background(
Color.gray.opacity(0.1)
Expand Down Expand Up @@ -151,7 +152,7 @@ private struct StackedAreaChartDemo: View {
var body: some View {
Chart(data: matrixData)
.chartStyle(
StackedAreaChartStyle(.quadCurve, colors: [.yellow, .orange, .red])
StackedAreaChartStyle(.quadCurve, fills: fills, yMirror: false)
)
.background(
Color.gray.opacity(0.1)
Expand All @@ -164,6 +165,56 @@ private struct StackedAreaChartDemo: View {
.cornerRadius(16)
.padding()
}

private var fills: [AnyView] {
[
AnyView(
LinearGradient(gradient: .init(colors: [Color.yellow.opacity(0.8), Color.green.opacity(0.2)]), startPoint: .top, endPoint: .bottom)
),
AnyView(
LinearGradient(gradient: .init(colors: [Color.orange.opacity(0.8), Color.green.opacity(0.2)]), startPoint: .top, endPoint: .bottom)
),
AnyView(
LinearGradient(gradient: .init(colors: [Color.red.opacity(0.8), Color.green.opacity(0.2)]), startPoint: .top, endPoint: .bottom)
)
]
}
}

private struct StackedAreaChartDualDemo: View {
@State var matrixDataTop: [[CGFloat]] = (0..<20).map { _ in (0..<3).map { _ in CGFloat.random(in: 0.00...0.33) } }
@State var matrixDataBottom: [[CGFloat]] = (0..<20).map { _ in (0..<3).map { _ in CGFloat.random(in: 0.00...0.33) } }

var body: some View {
VStack(spacing: 0) {
Chart(data: matrixDataTop)
.chartStyle(
StackedAreaChartStyle(.quadCurve, colors: [.yellow, .orange, .red])
)
.background(
Color.gray.opacity(0.1)
.overlay(
GridPattern(verticalLines: matrixDataTop.count)
.inset(by: 1)
.stroke(Color.red.opacity(0.2), style: .init(lineWidth: 1, lineCap: .round))
)
)
Chart(data: matrixDataBottom)
.chartStyle(
StackedAreaChartStyle(.quadCurve, colors: [.yellow, .green, .blue], yMirror: true)
)
.background(
Color.gray.opacity(0.1)
.overlay(
GridPattern(verticalLines: matrixDataBottom.count)
.inset(by: 1)
.stroke(Color.red.opacity(0.2), style: .init(lineWidth: 1, lineCap: .round))
)
)
}
.cornerRadius(16)
.padding()
}
}

private struct CompositeChartDemo: View {
Expand Down
11 changes: 8 additions & 3 deletions Sources/Charts/Chart/Styles/Area/AreaChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct AreaChart: Shape {
private let lineType: LineType
private let unitPoints: [UnitPoint]
private let bottomUnitPoints: [UnitPoint]?
private let yMirror: Bool

public func path(in rect: CGRect) -> Path {
Path { path in
Expand All @@ -24,6 +25,9 @@ struct AreaChart: Shape {
case .quadCurve:
path.addQuadCurves(bottomUnitPoints.reversed().points(in: rect))
}
} else if yMirror {
path.addLine(to: CGPoint(unitPoint: .bottomTrailing, in: rect))
path.addLine(to: CGPoint(unitPoint: .bottomLeading, in: rect))
} else {
path.addLine(to: CGPoint(unitPoint: .topTrailing, in: rect))
path.addLine(to: CGPoint(unitPoint: .topLeading, in: rect))
Expand All @@ -32,10 +36,11 @@ struct AreaChart: Shape {
}
}

init<Data: RandomAccessCollection>(unitData: Data, bottomUnitData: Data? = nil, lineType: LineType) where Data.Element : BinaryFloatingPoint {
init<Data: RandomAccessCollection>(unitData: Data, bottomUnitData: Data? = nil, lineType: LineType, yMirror: Bool = false) where Data.Element : BinaryFloatingPoint {
self.lineType = lineType
self.yMirror = yMirror
let step: CGFloat = unitData.count > 1 ? 1.0 / CGFloat(unitData.count - 1) : 1.0
self.unitPoints = unitData.enumerated().map { (index, dataPoint) in UnitPoint(x: step * CGFloat(index), y: CGFloat(dataPoint)) }
self.bottomUnitPoints = bottomUnitData?.enumerated().map { (index, dataPoint) in UnitPoint(x: step * CGFloat(index), y: CGFloat(dataPoint)) }
self.unitPoints = unitData.enumerated().map { (index, dataPoint) in UnitPoint(x: step * CGFloat(index), y: yMirror ? 1 - CGFloat(dataPoint): CGFloat(dataPoint)) }
self.bottomUnitPoints = bottomUnitData?.enumerated().map { (index, dataPoint) in UnitPoint(x: step * CGFloat(index), y: yMirror ? 1 - CGFloat(dataPoint): CGFloat(dataPoint)) }
}
}
6 changes: 4 additions & 2 deletions Sources/Charts/Chart/Styles/Area/AreaChartStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import Shapes
public struct AreaChartStyle<Fill: View>: ChartStyle {
private let lineType: LineType
private let fill: Fill
private let yMirror: Bool

public func makeBody(configuration: Self.Configuration) -> some View {
fill
.clipShape(
AreaChart(unitData: configuration.dataMatrix.map { $0.reduce(0, +) }, lineType: self.lineType)
AreaChart(unitData: configuration.dataMatrix.map { $0.reduce(0, +) }, lineType: self.lineType, yMirror: yMirror)
)
}

public init(_ lineType: LineType = .quadCurve, fill: Fill) {
public init(_ lineType: LineType = .quadCurve, fill: Fill, yMirror: Bool = false) {
self.lineType = lineType
self.fill = fill
self.yMirror = yMirror
}
}
18 changes: 13 additions & 5 deletions Sources/Charts/Chart/Styles/Area/StackedAreaChartStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import Shapes

public struct StackedAreaChartStyle: ChartStyle {
private let lineType: LineType
private let colors: [Color]
private let fills: [AnyView]
private let yMirror: Bool

public func makeBody(configuration: Self.Configuration) -> some View {
ZStack {
ForEach(Array(configuration.dataMatrix.transpose().stacked().enumerated()), id: \.self.offset) { enumeratedData in
colors[enumeratedData.offset % colors.count].clipShape(
fills[enumeratedData.offset % fills.count].clipShape(
AreaChart(
unitData: enumeratedData.element,
lineType: self.lineType
lineType: self.lineType,
yMirror: yMirror
)
)
.zIndex(-Double(enumeratedData.offset))
Expand All @@ -20,9 +22,15 @@ public struct StackedAreaChartStyle: ChartStyle {
.drawingGroup()
}

public init(_ lineType: LineType = .quadCurve, colors: [Color] = [.red, .orange, .yellow, .green, .blue, .purple]) {
public init(_ lineType: LineType = .quadCurve, colors: [Color] = [.red, .orange, .yellow, .green, .blue, .purple], yMirror: Bool = false) {
let fills = colors.map { AnyView($0) }
self.init(lineType, fills: fills, yMirror: yMirror)
}

public init(_ lineType: LineType = .quadCurve, fills: [AnyView] = [], yMirror: Bool = false) {
self.lineType = lineType
self.colors = colors
self.fills = fills
self.yMirror = yMirror
}
}

Expand Down