Skip to content

Commit f464c9f

Browse files
authored
Merge pull request #20 from SwiftUIExtensions/axis-labels
Axis labels
2 parents f9da4a7 + 353aeb4 commit f464c9f

File tree

3 files changed

+90
-21
lines changed

3 files changed

+90
-21
lines changed

Examples/ChartsExamples/ChartsView.swift

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,60 @@ import Charts
33
import Shapes
44

55
struct ChartsView: View {
6-
@State var data1: [CGFloat] = (0...20).map { _ in .random(in: 0.1...1.0) }
7-
@State var data2: [CGFloat] = (0...50).map { _ in .random(in: 0.1...1.0) }
8-
@State var data3: [CGFloat] = (0...100).map { _ in .random(in: 0.1...1.0) }
6+
@State var data1: [CGFloat] = (2010...2020).map { _ in .random(in: 0.1...1.0) }
7+
@State var data2: [CGFloat] = (0..<50).map { _ in .random(in: 0.1...1.0) }
8+
@State var data3: [CGFloat] = (0..<100).map { _ in .random(in: 0.1...1.0) }
99

10-
@State var data4: [CGFloat] = (0...100).map { _ in .random(in: 0.4...1.0) }
11-
@State var data5: [CGFloat] = (0...100).map { _ in .random(in: 0.1...0.3) }
12-
@State var data6: [CGFloat] = (0...100).map { _ in .random(in: 0.3...0.4) }
10+
@State var data4: [CGFloat] = (0..<100).map { _ in .random(in: 0.4...1.0) }
11+
@State var data5: [CGFloat] = (0..<100).map { _ in .random(in: 0.1...0.3) }
12+
@State var data6: [CGFloat] = (0..<100).map { _ in .random(in: 0.3...0.4) }
1313

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

1616
var body: some View {
1717
ScrollView {
18-
Chart(data: data1)
19-
.chartStyle(
20-
LineChartStyle(.quadCurve, lineColor: .blue, lineWidth: 5)
21-
)
22-
.padding()
23-
.background(
24-
GridPattern(horizontalLines: data1.count, verticalLines: 20)
25-
.inset(by: 1)
26-
.stroke(Color.gray.opacity(0.1), style: .init(lineWidth: 2, lineCap: .round))
27-
)
28-
.frame(height: 300)
29-
.padding()
18+
HStack {
19+
VStack {
20+
AxisLabels(.vertical, data: 1...10, id: \.self) {
21+
Text("\(110 - $0 * 10)")
22+
.fontWeight(.bold)
23+
.font(Font.system(size: 8))
24+
.foregroundColor(.secondary)
25+
}
26+
.frame(width: 20)
27+
28+
Rectangle().foregroundColor(.clear).frame(width: 20, height: 20)
29+
}
30+
31+
32+
VStack {
33+
Chart(data: data1)
34+
.chartStyle(
35+
LineChartStyle(.quadCurve, lineColor: .blue, lineWidth: 5)
36+
)
37+
.padding()
38+
.background(
39+
GridPattern(horizontalLines: 10 + 1, verticalLines: data1.count + 1)
40+
.inset(by: 1)
41+
.stroke(Color.gray.opacity(0.1), style: .init(lineWidth: 2, lineCap: .round))
42+
)
43+
.frame(height: 300)
44+
45+
46+
AxisLabels(.horizontal, data: 2010...2020, id: \.self) {
47+
Text("\($0)".replacingOccurrences(of: ",", with: ""))
48+
.fontWeight(.bold)
49+
.font(Font.system(size: 8))
50+
.foregroundColor(.secondary)
51+
}
52+
.frame(height: 20)
53+
.padding(.horizontal, 1)
54+
}
55+
.layoutPriority(1)
56+
}
57+
58+
.padding()
59+
3060

3161
Chart(data: data2)
3262
.chartStyle(

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ Chart(data: matrix)
7676
## Roadmap
7777
- Bar chart style
7878

79-
## Code Contibutions
79+
## Code Contributions
8080
Feel free to contribute via fork/pull request to master branch. If you want to request a feature or report a bug please start a new issue.
8181

82-
## Coffee Contibutions
83-
If you find this project useful please consider becoming a sponsor.
82+
## Coffee Contributions
83+
If you find this project useful please consider becoming my GitHub sponsor.

Sources/Charts/Chart/AxisLabels.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import SwiftUI
2+
3+
public struct AxisLabels<Content: View>: View {
4+
let axis: Axis
5+
let labels: [AnyView]
6+
7+
public var body: some View {
8+
GeometryReader { geometry in
9+
if self.axis == .horizontal {
10+
HStack(spacing: 0) {
11+
ForEach(0..<self.labels.count, id: \.self) { index in
12+
self.labels[index].frame(width: geometry.size.width / CGFloat(self.labels.count), alignment: .center)
13+
}
14+
}
15+
} else {
16+
VStack(spacing: 0) {
17+
ForEach(0..<self.labels.count, id: \.self) { index in
18+
self.labels[index].frame(height: geometry.size.height / CGFloat(self.labels.count), alignment: .center)
19+
}
20+
}
21+
}
22+
}
23+
}
24+
25+
public init<Data, Label>(_ axis: Axis = .horizontal, data: Data, @ViewBuilder label: @escaping (Data.Element) -> Label) where Content == ForEach<Data, Data.Element.ID, Label>, Data : RandomAccessCollection, Label : View, Data.Element : Identifiable {
26+
self.axis = axis
27+
self.labels = data.map({ AnyView(label($0)) })
28+
}
29+
30+
public init<Data, ID, Label>(_ axis: Axis = .horizontal, data: Data, id: KeyPath<Data.Element, ID>, @ViewBuilder label: @escaping (Data.Element) -> Label) where Content == ForEach<Data, ID, Label>, Data : RandomAccessCollection, ID : Hashable, Label : View {
31+
self.axis = axis
32+
self.labels = data.map({ AnyView(label($0)) })
33+
}
34+
35+
public init<Label>(_ axis: Axis = .horizontal, data: Range<Int>, @ViewBuilder label: @escaping (Int) -> Label) where Content == ForEach<Range<Int>, Int, Label>, Label : View {
36+
self.axis = axis
37+
self.labels = data.map({ AnyView(label($0)) })
38+
}
39+
}

0 commit comments

Comments
 (0)