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

58 top of bar charts have deformation #201

Merged
merged 3 commits into from
May 20, 2022
Merged
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
31 changes: 31 additions & 0 deletions Sources/SwiftUICharts/BarChart/Extras/BarLayout.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// BarLayout.swift
//
//
// Created by Will Dale on 20/05/2022.
//

import CoreGraphics.CGGeometry

internal struct BarLayout {
internal static func barWidth(_ totalWidth: CGFloat, _ widthFactor: CGFloat) -> CGFloat {
return totalWidth * widthFactor
}

internal static func barHeight(_ totalHeight: CGFloat, _ value: Double, _ maxValue: Double) -> CGFloat {
return totalHeight * divideByZeroProtection(CGFloat.self, value, maxValue)
}

internal static func barOffset(_ section: CGSize, _ widthFactor: CGFloat, _ value: Double, _ maxValue: Double) -> CGSize {
return CGSize(width: barXOffset(section.width, widthFactor),
height: barYOffset(section.height, value, maxValue))
}

internal static func barXOffset(_ total: CGFloat, _ factor: CGFloat) -> CGFloat {
return (-(total * factor) / 2) + (total / 2)
}

internal static func barYOffset(_ total: CGFloat, _ value: Double, _ maxValue: Double) -> CGFloat {
return total - (total * divideByZeroProtection(CGFloat.self, value, maxValue))
}
}
1 change: 0 additions & 1 deletion Sources/SwiftUICharts/BarChart/Views/GroupedBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,4 @@ internal struct GroupedBarCell<ChartData>: View where ChartData: GroupedBarChart
}
}
}

}
28 changes: 26 additions & 2 deletions Sources/SwiftUICharts/BarChart/Views/StackedBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ public struct StackedBarChart<ChartData>: View where ChartData: StackedBarChartD
if chartData.isGreaterThanTwo() {
HStack(alignment: .bottom, spacing: 0) {
ForEach(chartData.dataSets.dataSets) { dataSet in
GeometryReader { geo in
StackElementSubView(dataSet: dataSet,
specifier: chartData.infoView.touchSpecifier,
formatter: chartData.infoView.touchFormatter)
.clipShape(RoundedRectangleBarShape(chartData.barStyle.cornerRadius))
.scaleEffect(y: animationValue(dataSet.maxValue(), chartData.maxValue), anchor: .bottom)
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)

.frame(width: BarLayout.barWidth(geo.size.width, chartData.barStyle.barWidth))
.frame(height: frameAnimationValue(dataSet.maxValue(), height: geo.size.height))
.offset(offsetAnimationValue(dataSet.maxValue(), size: geo.size))

.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(disabled: chartData.disableAnimation, using: chartData.chartStyle.globalAnimation) {
Expand All @@ -75,6 +79,7 @@ public struct StackedBarChart<ChartData>: View where ChartData: StackedBarChartD
self.startAnimation = false
}
.accessibilityLabel(LocalizedStringKey(chartData.metadata.title))
}
}
}
.layoutNotifier(timer)
Expand All @@ -89,4 +94,23 @@ public struct StackedBarChart<ChartData>: View where ChartData: StackedBarChartD
return startAnimation ? value : 0
}
}

func frameAnimationValue(_ value: Double, height: CGFloat) -> CGFloat {
let value = BarLayout.barHeight(height, value, chartData.maxValue)
if chartData.disableAnimation {
return value
} else {
return startAnimation ? value : 0
}
}

func offsetAnimationValue(_ value: Double, size: CGSize) -> CGSize {
let value = BarLayout.barOffset(size, chartData.barStyle.barWidth, value, chartData.maxValue)
let zero = BarLayout.barOffset(size, chartData.barStyle.barWidth, 0, 0)
if chartData.disableAnimation {
return value
} else {
return startAnimation ? value : zero
}
}
}
Loading