Skip to content

Commit 6022154

Browse files
committed
Tidy Up.
1 parent ebf8c34 commit 6022154

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+664
-705
lines changed

Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PackageDescription
55

66
let package = Package(
77
name: "SwiftUICharts",
8+
defaultLocalization: "en",
89
platforms: [
910
.macOS(.v11), .iOS(.v14), .watchOS(.v7), .tvOS(.v14)
1011
],

Sources/SwiftUICharts/BarChart/Extras/BarChartEnums.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ public enum ColourFrom {
2525
```
2626
case none // No overlay markers.
2727
case vertical // Vertical line from top to bottom.
28-
case full // Full width and height of view intersecting at touch location.
29-
case bottomLeading // From bottom and leading edges meeting at touch location.
30-
case bottomTrailing // From bottom and trailing edges meeting at touch location.
31-
case topLeading // From top and leading edges meeting at touch location.
32-
case topTrailing // From top and trailing edges meeting at touch location.
33-
28+
case full // Full width and height of view intersecting at a specified point.
29+
case bottomLeading // From bottom and leading edges meeting at a specified point.
30+
case bottomTrailing // From bottom and trailing edges meeting at a specified point.
31+
case topLeading // From top and leading edges meeting at a specified point.
32+
case topTrailing // From top and trailing edges meeting at a specified point.
3433
```
3534
*/
3635
public enum BarMarkerType: MarkerType {

Sources/SwiftUICharts/BarChart/Models/ChartData/BarChartData.swift

+47-40
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,7 @@ import SwiftUI
1010
/**
1111
Data for drawing and styling a standard Bar Chart.
1212

13-
# Example
14-
```
15-
static func weekOfData() -> BarChartData {
16-
17-
let data : BarDataSet =
18-
BarDataSet(dataPoints: [
19-
BarChartDataPoint(value: 20, xAxisLabel: "M", pointLabel: "Monday" , colour: .purple),
20-
BarChartDataPoint(value: 90, xAxisLabel: "T", pointLabel: "Tuesday" , colour: .blue),
21-
BarChartDataPoint(value: 100, xAxisLabel: "W", pointLabel: "Wednesday", colour: Color(.cyan)),
22-
BarChartDataPoint(value: 75, xAxisLabel: "T", pointLabel: "Thursday" , colour: .green),
23-
BarChartDataPoint(value: 160, xAxisLabel: "F", pointLabel: "Friday" , colour: .yellow),
24-
BarChartDataPoint(value: 110, xAxisLabel: "S", pointLabel: "Saturday" , colour: .orange),
25-
BarChartDataPoint(value: 90, xAxisLabel: "S", pointLabel: "Sunday" , colour: .red)
26-
],
27-
legendTitle: "Data")
28-
29-
return BarChartData(dataSets : data,
30-
metadata : ChartMetadata(title : "Test Data",
31-
subtitle: "A weeks worth"),
32-
barStyle : BarStyle(barWidth : 0.5,
33-
colourFrom: .dataPoints,
34-
colour : .blue),
35-
chartStyle: BarChartStyle(infoBoxPlacement : .floating,
36-
xAxisLabelPosition : .bottom,
37-
xAxisLabelsFrom : .dataPoint,
38-
yAxisLabelPosition : .leading,
39-
yAxisNumberOfLabels: 5))
40-
}
41-
```
13+
4214
*/
4315
public final class BarChartData: CTBarChartDataProtocol {
4416
// MARK: Properties
@@ -90,24 +62,22 @@ public final class BarChartData: CTBarChartDataProtocol {
9062
public final func getXAxisLabels() -> some View {
9163
Group {
9264
switch self.chartStyle.xAxisLabelsFrom {
93-
case .dataPoint:
94-
95-
HStack(spacing: 0) {
65+
case .dataPoint(let angle):
66+
67+
HStack(alignment: .top, spacing: 0) {
9668
ForEach(dataSets.dataPoints) { data in
9769
Spacer()
9870
.frame(minWidth: 0, maxWidth: 500)
99-
Text(data.wrappedXAxisLabel)
100-
.font(.caption)
71+
YAxisDataPointCell(chartData: self, label: data.wrappedXAxisLabel, rotationAngle: angle)
10172
.foregroundColor(self.chartStyle.xAxisLabelColour)
102-
.lineLimit(1)
103-
.minimumScaleFactor(0.5)
10473
.accessibilityLabel(Text("X Axis Label"))
10574
.accessibilityValue(Text("\(data.wrappedXAxisLabel)"))
10675
Spacer()
10776
.frame(minWidth: 0, maxWidth: 500)
10877
}
10978
}
11079

80+
11181
case .chartData:
11282

11383
if let labelArray = self.xAxisLabels {
@@ -119,7 +89,6 @@ public final class BarChartData: CTBarChartDataProtocol {
11989
.font(.caption)
12090
.foregroundColor(self.chartStyle.xAxisLabelColour)
12191
.lineLimit(1)
122-
.minimumScaleFactor(0.5)
12392
.accessibilityLabel(Text("X Axis Label"))
12493
.accessibilityValue(Text("\(data)"))
12594
Spacer()
@@ -155,7 +124,45 @@ public final class BarChartData: CTBarChartDataProtocol {
155124
return nil
156125
}
157126

158-
public typealias Set = BarDataSet
159-
public typealias DataPoint = BarChartDataPoint
160-
public typealias CTStyle = BarChartStyle
127+
public typealias Set = BarDataSet
128+
public typealias DataPoint = BarChartDataPoint
129+
public typealias CTStyle = BarChartStyle
130+
}
131+
132+
struct YAxisDataPointCell<ChartData>: View where ChartData: CTLineBarChartDataProtocol {
133+
134+
@ObservedObject var chartData : ChartData
135+
136+
private let label : String
137+
private let rotationAngle : Angle
138+
139+
init(chartData: ChartData, label: String, rotationAngle : Angle) {
140+
self.chartData = chartData
141+
self.label = label
142+
self.rotationAngle = rotationAngle
143+
}
144+
145+
@State private var width: CGFloat = 0
146+
147+
var body: some View {
148+
149+
Text(label)
150+
.font(.caption)
151+
.lineLimit(1)
152+
.overlay(
153+
GeometryReader { geo in
154+
Color.clear
155+
.onAppear {
156+
self.width = geo.frame(in: .local).width
157+
}
158+
}
159+
)
160+
.fixedSize(horizontal: true, vertical: false)
161+
.rotationEffect(rotationAngle, anchor: .center)
162+
.frame(width: 10, height: width)
163+
.onAppear {
164+
chartData.viewData.xAxisLabelHeights.append(width)
165+
}
166+
167+
}
161168
}

Sources/SwiftUICharts/BarChart/Models/ChartData/GroupedBarChartData.swift

+22-73
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,6 @@ import SwiftUI
1111
Data model for drawing and styling a Grouped Bar Chart.
1212

1313
The grouping data informs the model as to how the datapoints are linked.
14-
15-
# Example
16-
```
17-
static func makeData() -> GroupedBarChartData {
18-
19-
enum Group {
20-
case one
21-
case two
22-
case three
23-
case four
24-
25-
var data : GroupingData {
26-
switch self {
27-
case .one:
28-
return GroupingData(title: "One" , colour: .blue)
29-
case .two:
30-
return GroupingData(title: "Two" , colour: .red)
31-
case .three:
32-
return GroupingData(title: "Three", colour: .yellow)
33-
case .four:
34-
return GroupingData(title: "Four" , colour: .green)
35-
}
36-
}
37-
}
38-
39-
let groups : [GroupingData] = [Group.one.data, Group.two.data, Group.three.data, Group.four.data]
40-
41-
let data = MultiBarDataSets(dataSets: [
42-
MultiBarDataSet(dataPoints: [
43-
MultiBarChartDataPoint(value: 10, xAxisLabel: "1.1", pointLabel: "One One" , group: Group.one.data),
44-
MultiBarChartDataPoint(value: 50, xAxisLabel: "1.2", pointLabel: "One Two" , group: Group.two.data),
45-
MultiBarChartDataPoint(value: 30, xAxisLabel: "1.3", pointLabel: "One Three" , group: Group.three.data),
46-
MultiBarChartDataPoint(value: 40, xAxisLabel: "1.4", pointLabel: "One Four" , group: Group.four.data)
47-
]),
48-
49-
MultiBarDataSet(dataPoints: [
50-
MultiBarChartDataPoint(value: 20, xAxisLabel: "2.1", pointLabel: "Two One" , group: Group.one.data),
51-
MultiBarChartDataPoint(value: 60, xAxisLabel: "2.2", pointLabel: "Two Two" , group: Group.two.data),
52-
MultiBarChartDataPoint(value: 40, xAxisLabel: "2.3", pointLabel: "Two Three" , group: Group.three.data),
53-
MultiBarChartDataPoint(value: 60, xAxisLabel: "2.3", pointLabel: "Two Four" , group: Group.four.data)
54-
]),
55-
56-
MultiBarDataSet(dataPoints: [
57-
MultiBarChartDataPoint(value: 30, xAxisLabel: "3.1", pointLabel: "Three One" , group: Group.one.data),
58-
MultiBarChartDataPoint(value: 70, xAxisLabel: "3.2", pointLabel: "Three Two" , group: Group.two.data),
59-
MultiBarChartDataPoint(value: 30, xAxisLabel: "3.3", pointLabel: "Three Three", group: Group.three.data),
60-
MultiBarChartDataPoint(value: 90, xAxisLabel: "3.4", pointLabel: "Three Four" , group: Group.four.data)
61-
]),
62-
63-
MultiBarDataSet(dataPoints: [
64-
MultiBarChartDataPoint(value: 40, xAxisLabel: "4.1", pointLabel: "Four One" , group: Group.one.data),
65-
MultiBarChartDataPoint(value: 80, xAxisLabel: "4.2", pointLabel: "Four Two" , group: Group.two.data),
66-
MultiBarChartDataPoint(value: 20, xAxisLabel: "4.3", pointLabel: "Four Three" , group: Group.three.data),
67-
MultiBarChartDataPoint(value: 50, xAxisLabel: "4.3", pointLabel: "Four Four" , group: Group.four.data)
68-
])
69-
])
70-
71-
return GroupedBarChartData(dataSets : data,
72-
groups : groups,
73-
metadata : ChartMetadata(title: "Hello", subtitle: "Bob"),
74-
chartStyle : BarChartStyle(infoBoxPlacement: .floating,
75-
xAxisLabelsFrom : .dataPoint))
76-
}
7714
```
7815
*/
7916
public final class GroupedBarChartData: CTMultiBarChartDataProtocol {
@@ -130,22 +67,19 @@ public final class GroupedBarChartData: CTMultiBarChartDataProtocol {
13067

13168
// MARK: Labels
13269
public final func getXAxisLabels() -> some View {
133-
Group {
70+
VStack {
13471
switch self.chartStyle.xAxisLabelsFrom {
135-
case .dataPoint:
72+
case .dataPoint(let angle):
13673
HStack(spacing: self.groupSpacing) {
13774
ForEach(dataSets.dataSets) { dataSet in
13875
HStack(spacing: 0) {
13976
ForEach(dataSet.dataPoints) { data in
14077
Spacer()
14178
.frame(minWidth: 0, maxWidth: 500)
142-
Text(data.wrappedXAxisLabel)
143-
.font(.caption)
79+
YAxisDataPointCell(chartData: self, label: data.group.title, rotationAngle: angle)
14480
.foregroundColor(self.chartStyle.xAxisLabelColour)
145-
.lineLimit(1)
146-
.minimumScaleFactor(0.5)
147-
.accessibilityLabel(Text("XAxisLabel"))
148-
.accessibilityValue(Text("\(data.wrappedXAxisLabel)"))
81+
.accessibilityLabel(Text("X Axis Label"))
82+
.accessibilityValue(Text("\(data.group.title)"))
14983
Spacer()
15084
.frame(minWidth: 0, maxWidth: 500)
15185
}
@@ -165,17 +99,32 @@ public final class GroupedBarChartData: CTMultiBarChartDataProtocol {
16599
.font(.caption)
166100
.foregroundColor(self.chartStyle.xAxisLabelColour)
167101
.lineLimit(1)
168-
.minimumScaleFactor(0.5)
169-
.accessibilityLabel(Text("XAxisLabel"))
102+
.accessibilityLabel(Text("X Axis Label"))
170103
.accessibilityValue(Text("\(data)"))
171104
Spacer()
172105
.frame(minWidth: 0, maxWidth: 500)
173106
}
174107
}
175108
}
176109
}
110+
HStack(spacing: self.groupSpacing) {
111+
ForEach(dataSets.dataSets) { dataSet in
112+
HStack(spacing: 0) {
113+
Spacer()
114+
.frame(minWidth: 0, maxWidth: 500)
115+
YAxisDataPointCell(chartData: self, label: dataSet.setTitle, rotationAngle: .degrees(0))
116+
.foregroundColor(self.chartStyle.xAxisLabelColour)
117+
.accessibilityLabel(Text("X Axis Label"))
118+
.accessibilityValue(Text("\(dataSet.setTitle)"))
119+
Spacer()
120+
.frame(minWidth: 0, maxWidth: 500)
121+
}
122+
}
123+
}
124+
.padding(.horizontal, -4)
177125
}
178126
}
127+
179128
// MARK: Touch
180129
public final func getTouchInteraction(touchLocation: CGPoint, chartSize: CGRect) -> some View {
181130
self.markerSubView()

Sources/SwiftUICharts/BarChart/Models/ChartData/RangedBarChartData.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,14 @@ public final class RangedBarChartData: CTRangedBarChartDataProtocol {
6868
public final func getXAxisLabels() -> some View {
6969
Group {
7070
switch self.chartStyle.xAxisLabelsFrom {
71-
case .dataPoint:
71+
case .dataPoint(let angle):
7272

7373
HStack(spacing: 0) {
7474
ForEach(dataSets.dataPoints) { data in
7575
Spacer()
7676
.frame(minWidth: 0, maxWidth: 500)
77-
Text(data.wrappedXAxisLabel)
78-
.font(.caption)
77+
YAxisDataPointCell(chartData: self, label: data.wrappedXAxisLabel, rotationAngle: angle)
7978
.foregroundColor(self.chartStyle.xAxisLabelColour)
80-
.lineLimit(1)
81-
.minimumScaleFactor(0.5)
8279
.accessibilityLabel(Text("X Axis Label"))
8380
.accessibilityValue(Text("\(data.wrappedXAxisLabel)"))
8481
Spacer()
@@ -99,7 +96,6 @@ public final class RangedBarChartData: CTRangedBarChartDataProtocol {
9996
.font(.caption)
10097
.foregroundColor(self.chartStyle.xAxisLabelColour)
10198
.lineLimit(1)
102-
.minimumScaleFactor(0.5)
10399
.accessibilityLabel(Text("X Axis Label"))
104100
.accessibilityValue(Text("\(data)"))
105101
if data != labelArray[labelArray.count-1] {

Sources/SwiftUICharts/BarChart/Models/ChartData/StackedBarChartData.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,14 @@ public final class StackedBarChartData: CTMultiBarChartDataProtocol {
125125
public final func getXAxisLabels() -> some View {
126126
Group {
127127
switch self.chartStyle.xAxisLabelsFrom {
128-
case .dataPoint:
128+
case .dataPoint(let angle):
129129
HStack(spacing: 0) {
130130
ForEach(groups) { group in
131131
Spacer()
132132
.frame(minWidth: 0, maxWidth: 500)
133-
Text(group.title)
134-
.font(.caption)
133+
YAxisDataPointCell(chartData: self, label: group.title, rotationAngle: angle)
135134
.foregroundColor(self.chartStyle.xAxisLabelColour)
136135
.lineLimit(1)
137-
.minimumScaleFactor(0.5)
138136
.accessibilityLabel(Text("X Axis Label"))
139137
.accessibilityValue(Text("\(group.title)"))
140138

@@ -152,7 +150,6 @@ public final class StackedBarChartData: CTMultiBarChartDataProtocol {
152150
.font(.caption)
153151
.foregroundColor(self.chartStyle.xAxisLabelColour)
154152
.lineLimit(1)
155-
.minimumScaleFactor(0.5)
156153
.accessibilityLabel(Text("X Axis Label"))
157154
.accessibilityValue(Text("\(data)"))
158155
Spacer()

Sources/SwiftUICharts/BarChart/Models/DataSet/MultiBarDataSets.swift

+12-8
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import SwiftUI
1414
```
1515
let data = MultiBarDataSets(dataSets: [
1616
MultiBarDataSet(dataPoints: [
17-
MultiBarChartDataPoint(value: 10, xAxisLabel: "1.1", pointLabel: "One One", group: GroupingData(title: "One", colour: .blue))
17+
MultiBarChartDataPoint(value: 10, group: GroupingData(title: "One", colour: .blue))
1818
]),
1919
MultiBarDataSet(dataPoints: [
20-
MultiBarChartDataPoint(value: 20, xAxisLabel: "2.1", pointLabel: "Two One", group: GroupingData(title: "One", colour: .blue))
20+
MultiBarChartDataPoint(value: 20, group: GroupingData(title: "One", colour: .blue))
2121
])
2222
])
2323
```
@@ -39,19 +39,23 @@ public struct MultiBarDataSets: CTMultiDataSetProtocol {
3939
# Example
4040
```
4141
MultiBarDataSet(dataPoints: [
42-
MultiBarChartDataPoint(value: 10, xAxisLabel: "1.1", pointLabel: "One One", group: GroupingData(title: "One", colour: .blue)),
43-
MultiBarChartDataPoint(value: 50, xAxisLabel: "1.2", pointLabel: "One Two", group: GroupingData(title: "Two", colour: .red))
42+
MultiBarChartDataPoint(value: 10, group: GroupingData(title: "One", colour: .blue)),
43+
MultiBarChartDataPoint(value: 50, group: GroupingData(title: "Two", colour: .red))
4444
])
4545
```
4646
*/
4747
public struct MultiBarDataSet: CTMultiBarChartDataSet {
4848

49-
public let id : UUID = UUID()
50-
public var dataPoints : [MultiBarChartDataPoint]
49+
public let id : UUID = UUID()
50+
public var dataPoints : [MultiBarChartDataPoint]
51+
public var setTitle : String
5152

5253
/// Initialises a new data set for a Bar Chart.
53-
public init(dataPoints : [MultiBarChartDataPoint]) {
54-
self.dataPoints = dataPoints
54+
public init(dataPoints: [MultiBarChartDataPoint],
55+
setTitle : String = ""
56+
) {
57+
self.dataPoints = dataPoints
58+
self.setTitle = setTitle
5559
}
5660

5761
public typealias ID = UUID

0 commit comments

Comments
 (0)