-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating a data structure which propagets changes in data to the char…
…ts (#114) * Creating a data structure which propagets changes in data to the charts * Fixed appearing animation
- Loading branch information
Showing
12 changed files
with
140 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import SwiftUI | ||
|
||
struct AnyChartType: ChartType { | ||
private let chartMaker: (ChartType.Configuration, ChartType.Style) -> AnyView | ||
private let chartMaker: (ChartType.Data, ChartType.Style) -> AnyView | ||
|
||
init<S: ChartType>(_ type: S) { | ||
self.chartMaker = type.makeTypeErasedBody | ||
} | ||
|
||
func makeChart(configuration: ChartType.Configuration, style: ChartType.Style) -> AnyView { | ||
self.chartMaker(configuration, style) | ||
func makeChart(data: ChartType.Data, style: ChartType.Style) -> AnyView { | ||
self.chartMaker(data, style) | ||
} | ||
} | ||
|
||
fileprivate extension ChartType { | ||
func makeTypeErasedBody(configuration: ChartType.Configuration, style: ChartType.Style) -> AnyView { | ||
AnyView(makeChart(configuration: configuration, style: style)) | ||
func makeTypeErasedBody(data: ChartType.Data, style: ChartType.Style) -> AnyView { | ||
AnyView(makeChart(data: data, style: style)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import SwiftUI | ||
|
||
public class ChartData: ObservableObject { | ||
@Published public var data: [Double] = [] | ||
|
||
public init(_ data: [Double]) { | ||
self.data = data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
Sources/SwiftUICharts/Base/Chart/ChartTypeConfiguration.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,67 @@ | ||
import SwiftUI | ||
|
||
public struct BarChartCell: View { | ||
@State var value: Double | ||
@State var index: Int = 0 | ||
@State var width: Float | ||
@State var numberOfDataPoints: Int | ||
var value: Double | ||
var index: Int = 0 | ||
var width: Float | ||
var numberOfDataPoints: Int | ||
var gradientColor: ColorGradient | ||
var touchLocation: CGFloat | ||
|
||
var cellWidth: Double { | ||
return Double(width)/(Double(numberOfDataPoints) * 1.5) | ||
} | ||
|
||
@State var scaleValue: Double = 0 | ||
@Binding var touchLocation: CGFloat | ||
|
||
@State var firstDisplay: Bool = true | ||
|
||
public init( value: Double, | ||
index: Int = 0, | ||
width: Float, | ||
numberOfDataPoints: Int, | ||
gradientColor: ColorGradient, | ||
touchLocation: CGFloat) { | ||
self.value = value | ||
self.index = index | ||
self.width = width | ||
self.numberOfDataPoints = numberOfDataPoints | ||
self.gradientColor = gradientColor | ||
self.touchLocation = touchLocation | ||
} | ||
|
||
public var body: some View { | ||
ZStack { | ||
RoundedRectangle(cornerRadius: 4) | ||
.fill(gradientColor.linearGradient(from: .bottom, to: .top)) | ||
} | ||
.frame(width: CGFloat(self.cellWidth)) | ||
.scaleEffect(CGSize(width: 1, height: self.scaleValue), anchor: .bottom) | ||
.onAppear { | ||
self.scaleValue = self.value | ||
} | ||
.animation(Animation.spring().delay(self.touchLocation < 0 ? Double(self.index) * 0.04 : 0)) | ||
} | ||
.frame(width: CGFloat(self.cellWidth)) | ||
.scaleEffect(CGSize(width: 1, height: self.firstDisplay ? 0.0 : self.value), anchor: .bottom) | ||
.onAppear { | ||
self.firstDisplay = false | ||
} | ||
.onDisappear { | ||
self.firstDisplay = true | ||
} | ||
.transition(.slide) | ||
.animation(Animation.spring().delay(self.touchLocation < 0 || !firstDisplay ? Double(self.index) * 0.04 : 0)) | ||
} | ||
} | ||
|
||
struct BarChartCell_Previews: PreviewProvider { | ||
static var previews: some View { | ||
Group { | ||
BarChartCell(value: 0, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.greenRed, touchLocation: .constant(CGFloat())) | ||
Group { | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.greenRed, touchLocation: .constant(CGFloat())) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.whiteBlack, touchLocation: .constant(CGFloat())) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient(.purple), touchLocation: .constant(CGFloat())) | ||
BarChartCell(value: 0, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.greenRed, touchLocation: CGFloat()) | ||
|
||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.greenRed, touchLocation: CGFloat()) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.whiteBlack, touchLocation: CGFloat()) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient(.purple), touchLocation: CGFloat()) | ||
} | ||
|
||
Group { | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.greenRed, touchLocation: .constant(CGFloat())) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.whiteBlack, touchLocation: .constant(CGFloat())) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient(.purple), touchLocation: .constant(CGFloat())) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.greenRed, touchLocation: CGFloat()) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient.whiteBlack, touchLocation: CGFloat()) | ||
BarChartCell(value: 1, width: 50, numberOfDataPoints: 1, gradientColor: ColorGradient(.purple), touchLocation: CGFloat()) | ||
}.environment(\.colorScheme, .dark) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.