Skip to content

Commit

Permalink
Fix, again.
Browse files Browse the repository at this point in the history
  • Loading branch information
willdale committed May 11, 2021
1 parent fd956d9 commit f0fcd18
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,16 @@ public final class LineChartData: CTLineChartDataProtocol {
GeometryReader { geo in
ZStack {
ForEach(self.dataSets.dataPoints.indices) { i in

Text(self.dataSets.dataPoints[i].wrappedXAxisLabel)
.font(self.chartStyle.xAxisLabelFont)
.foregroundColor(self.chartStyle.xAxisLabelColour)
.lineLimit(1)
.overlay(
GeometryReader { geo in
Color.clear
.onAppear {
if angle == .degrees(0) {
self.viewData.xAxisLabelHeights.append(geo.frame(in: .local).height)
} else {
self.viewData.xAxisLabelHeights.append(geo.frame(in: .local).width)
}
}
}
)
.fixedSize(horizontal: true, vertical: false)
.rotationEffect(angle, anchor: .center)
.accessibilityLabel(Text("X Axis Label"))
.accessibilityValue(Text(self.dataSets.dataPoints[i].wrappedXAxisLabel))

.frame(width: self.getXSection(dataSet: self.dataSets, chartSize: self.viewData.chartSize),
height: self.viewData.xAxisLabelHeights.max() ?? 0)
.offset(x: CGFloat(i) * (geo.frame(in: .local).width / CGFloat(self.dataSets.dataPoints.count - 1)),
y: 0)

if let label = self.dataSets.dataPoints[i].xAxisLabel {
if label != "" {
TempText(chartData: self, label: label, rotation: angle)

.frame(width: min(self.getXSection(dataSet: self.dataSets, chartSize: self.viewData.chartSize), self.viewData.xAxislabelWidths.min() ?? 0),
height: self.viewData.xAxisLabelHeights.max() ?? 0)
.offset(x: CGFloat(i) * (geo.frame(in: .local).width / CGFloat(self.dataSets.dataPoints.count - 1)),
y: 0)
}
}
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions Sources/SwiftUICharts/SharedLineAndBar/Views/RotatedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,67 @@ internal struct HorizontalRotatedText<ChartData>: View where ChartData: CTLineBa
.accessibilityValue(Text(label))
}
}




internal struct TempText<ChartData>: View where ChartData: CTLineBarChartDataProtocol {

@ObservedObject private var chartData: ChartData
private let label: String
private let rotation: Angle

/**
Initialises a new instance of RotatedText.

A view that displays text with rotation.

- Parameters:
- chartData: Chart Data must conform to `CTLineBarChartDataProtocol`.
- label: The string to show in the `Text` view.
- rotation: The angle to rotate the `Text` view by.
*/
internal init(
chartData: ChartData,
label: String,
rotation: Angle
) {
self.chartData = chartData
self.label = label
self.rotation = rotation
}

@State private var finalFrame: CGRect = .zero

internal var body: some View {
Text(label)
.font(chartData.chartStyle.xAxisLabelFont)
.foregroundColor(chartData.chartStyle.xAxisLabelColour)
.lineLimit(1)
.overlay(
GeometryReader { geo in
Color.clear
.onAppear {
if rotation == .degrees(0) {
chartData.viewData.xAxisLabelHeights.append(geo.frame(in: .local).height)
} else {
chartData.viewData.xAxisLabelHeights.append(geo.frame(in: .local).width)
}

if rotation == .degrees(0) || rotation == .radians(0) {
chartData.viewData.xAxislabelWidths.append(geo.frame(in: .local).width)
} else {
chartData.viewData.xAxislabelWidths.append(geo.frame(in: .local).height)
}
}
}
)
.fixedSize(horizontal: true, vertical: false)
.rotationEffect(rotation, anchor: .center)

.frame(width: rotation == .degrees(0) || rotation == .radians(0) ? finalFrame.width : finalFrame.height,
height: rotation == .degrees(0) || rotation == .radians(0) ? finalFrame.height : finalFrame.width)
.accessibilityLabel(Text("X Axis Label"))
.accessibilityValue(Text(label))
}
}

0 comments on commit f0fcd18

Please sign in to comment.