Skip to content

Commit

Permalink
Merge pull request #192 from willdale/191-barchart-didnt-show-correct…
Browse files Browse the repository at this point in the history
…ly-when-replacing-point-in-bardataset

191: Fix redrawing of bar chart when data changes
  • Loading branch information
willdale authored Apr 27, 2022
2 parents 5755f03 + 73570f3 commit 7bca99e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions Sources/SwiftUICharts/BarChart/Views/StackedBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public struct StackedBarChart<ChartData>: View where ChartData: StackedBarChartD
.clipShape(RoundedRectangleBarShape(chartData.barStyle.cornerRadius))
.scaleEffect(y: startAnimation ? divideByZeroProtection(CGFloat.self, dataSet.maxValue(), chartData.maxValue) : 0, anchor: .bottom)
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import SwiftUI
*/
internal struct BarChartBarStyleSubView<CD: BarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD

internal init(chartData: CD) {
self.chartData = chartData
Expand Down Expand Up @@ -67,7 +67,7 @@ internal struct BarChartBarStyleSubView<CD: BarChartData>: View {
*/
internal struct BarChartDataPointSubView<CD: BarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD

internal init(chartData: CD) {
self.chartData = chartData
Expand Down Expand Up @@ -119,7 +119,7 @@ internal struct BarChartDataPointSubView<CD: BarChartData>: View {

internal struct RangedBarChartBarStyleSubView<CD:RangedBarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD

internal init(chartData: CD) {
self.chartData = chartData
Expand Down Expand Up @@ -175,7 +175,7 @@ internal struct RangedBarChartBarStyleSubView<CD:RangedBarChartData>: View {
// MARK: DataPoints
internal struct RangedBarChartDataPointSubView<CD:RangedBarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD

internal init(chartData: CD) {
self.chartData = chartData
Expand Down Expand Up @@ -234,7 +234,7 @@ internal struct RangedBarChartDataPointSubView<CD:RangedBarChartData>: View {
*/
internal struct HorizontalBarChartBarStyleSubView<CD: HorizontalBarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD

internal init(chartData: CD) {
self.chartData = chartData
Expand Down Expand Up @@ -284,7 +284,7 @@ internal struct HorizontalBarChartBarStyleSubView<CD: HorizontalBarChartData>: V
*/
internal struct HorizontalBarChartDataPointSubView<CD: HorizontalBarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD

internal init(chartData: CD) {
self.chartData = chartData
Expand Down
30 changes: 19 additions & 11 deletions Sources/SwiftUICharts/BarChart/Views/SubViews/Bars.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import SwiftUI
internal struct ColourBar<CD: CTBarChartDataProtocol & GetDataProtocol,
DP: CTStandardDataPointProtocol & CTBarDataPointBaseProtocol>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let colour: Color
private let dataPoint: DP

Expand All @@ -42,6 +42,7 @@ internal struct ColourBar<CD: CTBarChartDataProtocol & GetDataProtocol,
.scaleEffect(y: startAnimation ? divideByZeroProtection(CGFloat.self, dataPoint.value, chartData.maxValue) : 0, anchor: .bottom)
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)
.background(Color(.gray).opacity(0.000000001))
.animation(.default, value: chartData.dataSets)
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
}
Expand All @@ -62,7 +63,7 @@ internal struct ColourBar<CD: CTBarChartDataProtocol & GetDataProtocol,
internal struct GradientColoursBar<CD: CTBarChartDataProtocol & GetDataProtocol,
DP: CTStandardDataPointProtocol & CTBarDataPointBaseProtocol>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let dataPoint: DP
private let colours: [Color]
private let startPoint: UnitPoint
Expand Down Expand Up @@ -91,6 +92,7 @@ internal struct GradientColoursBar<CD: CTBarChartDataProtocol & GetDataProtocol,
endPoint: endPoint))
.scaleEffect(y: startAnimation ? divideByZeroProtection(CGFloat.self, dataPoint.value, chartData.maxValue) : 0, anchor: .bottom)
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand All @@ -113,7 +115,7 @@ internal struct GradientColoursBar<CD: CTBarChartDataProtocol & GetDataProtocol,
internal struct GradientStopsBar<CD: CTBarChartDataProtocol & GetDataProtocol,
DP: CTStandardDataPointProtocol & CTBarDataPointBaseProtocol>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let dataPoint: DP
private let stops: [Gradient.Stop]
private let startPoint: UnitPoint
Expand Down Expand Up @@ -142,6 +144,7 @@ internal struct GradientStopsBar<CD: CTBarChartDataProtocol & GetDataProtocol,
endPoint: endPoint))
.scaleEffect(y: startAnimation ? divideByZeroProtection(CGFloat.self, dataPoint.value, chartData.maxValue) : 0, anchor: .bottom)
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand All @@ -160,7 +163,7 @@ internal struct GradientStopsBar<CD: CTBarChartDataProtocol & GetDataProtocol,
*/
internal struct StackElementSubView: View {

private let dataSet: StackedBarDataSet
private var dataSet: StackedBarDataSet
private let specifier: String
private let formatter: NumberFormatter?

Expand All @@ -176,7 +179,6 @@ internal struct StackElementSubView: View {

internal var body: some View {
GeometryReader { geo in

VStack(spacing: 0) {
ForEach(dataSet.dataPoints.reversed()) { dataPoint in
if dataPoint.group.colour.colourType == .colour,
Expand Down Expand Up @@ -334,7 +336,7 @@ internal struct GradientStopsPartBar: View {
// MARK: Colour
internal struct RangedBarChartColourCell<CD:RangedBarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let dataPoint: CD.SetType.DataPoint
private let colour: Color
private let barSize: CGRect
Expand All @@ -360,6 +362,7 @@ internal struct RangedBarChartColourCell<CD:RangedBarChartData>: View {
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)
.position(x: barSize.midX,
y: chartData.getBarPositionX(dataPoint: dataPoint, height: barSize.height))
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand All @@ -375,7 +378,7 @@ internal struct RangedBarChartColourCell<CD:RangedBarChartData>: View {
// MARK: Gradient
internal struct RangedBarChartColoursCell<CD:RangedBarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let dataPoint: CD.SetType.DataPoint
private let colours: [Color]
private let startPoint: UnitPoint
Expand Down Expand Up @@ -409,6 +412,7 @@ internal struct RangedBarChartColoursCell<CD:RangedBarChartData>: View {
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)
.position(x: barSize.midX,
y: chartData.getBarPositionX(dataPoint: dataPoint, height: barSize.height))
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand All @@ -424,7 +428,7 @@ internal struct RangedBarChartColoursCell<CD:RangedBarChartData>: View {
// MARK: Gradient Stops
internal struct RangedBarChartStopsCell<CD:RangedBarChartData>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let dataPoint: CD.SetType.DataPoint
private let stops: [Gradient.Stop]
private let startPoint: UnitPoint
Expand Down Expand Up @@ -458,6 +462,7 @@ internal struct RangedBarChartStopsCell<CD:RangedBarChartData>: View {
.scaleEffect(x: chartData.barStyle.barWidth, anchor: .center)
.position(x: barSize.midX,
y: chartData.getBarPositionX(dataPoint: dataPoint, height: barSize.height))
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand All @@ -483,7 +488,7 @@ internal struct RangedBarChartStopsCell<CD:RangedBarChartData>: View {
internal struct HorizontalColourBar<CD: CTBarChartDataProtocol & GetDataProtocol,
DP: CTStandardDataPointProtocol & CTBarDataPointBaseProtocol>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let colour: Color
private let dataPoint: DP

Expand All @@ -504,6 +509,7 @@ internal struct HorizontalColourBar<CD: CTBarChartDataProtocol & GetDataProtocol
.fill(colour)
.scaleEffect(x: startAnimation ? divideByZeroProtection(CGFloat.self, dataPoint.value, chartData.maxValue) : 0, anchor: .leading)
.scaleEffect(y: chartData.barStyle.barWidth, anchor: .center)
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand All @@ -526,7 +532,7 @@ internal struct HorizontalColourBar<CD: CTBarChartDataProtocol & GetDataProtocol
internal struct HorizontalGradientColoursBar<CD: CTBarChartDataProtocol & GetDataProtocol,
DP: CTStandardDataPointProtocol & CTBarDataPointBaseProtocol>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let dataPoint: DP
private let colours: [Color]
private let startPoint: UnitPoint
Expand Down Expand Up @@ -555,6 +561,7 @@ internal struct HorizontalGradientColoursBar<CD: CTBarChartDataProtocol & GetDat
endPoint: endPoint))
.scaleEffect(x: startAnimation ? divideByZeroProtection(CGFloat.self, dataPoint.value, chartData.maxValue) : 0, anchor: .leading)
.scaleEffect(y: chartData.barStyle.barWidth, anchor: .center)
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand All @@ -577,7 +584,7 @@ internal struct HorizontalGradientColoursBar<CD: CTBarChartDataProtocol & GetDat
internal struct HorizontalGradientStopsBar<CD: CTBarChartDataProtocol & GetDataProtocol,
DP: CTStandardDataPointProtocol & CTBarDataPointBaseProtocol>: View {

private let chartData: CD
@ObservedObject private var chartData: CD
private let dataPoint: DP
private let stops: [Gradient.Stop]
private let startPoint: UnitPoint
Expand Down Expand Up @@ -606,6 +613,7 @@ internal struct HorizontalGradientStopsBar<CD: CTBarChartDataProtocol & GetDataP
endPoint: endPoint))
.scaleEffect(x: startAnimation ? divideByZeroProtection(CGFloat.self, dataPoint.value, chartData.maxValue) : 0, anchor: .leading)
.scaleEffect(y: chartData.barStyle.barWidth, anchor: .center)
.animation(.default, value: chartData.dataSets)
.background(Color(.gray).opacity(0.000000001))
.animateOnAppear(using: chartData.chartStyle.globalAnimation) {
self.startAnimation = true
Expand Down

0 comments on commit 7bca99e

Please sign in to comment.