Bug Fix: Bar Chart with [0] crashed (#110)
This commit is contained in:
@@ -10,6 +10,9 @@ public struct BarChart: ChartType {
|
||||
struct BarChart_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group {
|
||||
BarChart().makeChart(
|
||||
configuration: .init(data: [0]),
|
||||
style: .init(backgroundColor: .white, foregroundColor: ColorGradient.redBlack))
|
||||
Group {
|
||||
BarChart().makeChart(
|
||||
configuration: .init(data: [1, 2, 3, 5, 1]),
|
||||
|
||||
@@ -31,6 +31,7 @@ public struct BarChartCell: View {
|
||||
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()))
|
||||
|
||||
@@ -11,7 +11,10 @@ public struct BarChartRow: View {
|
||||
var style: ChartStyle
|
||||
|
||||
var maxValue: Double {
|
||||
data.max() ?? 0
|
||||
guard let max = data.max() else {
|
||||
return 1
|
||||
}
|
||||
return max != 0 ? max : 1
|
||||
}
|
||||
|
||||
public var body: some View {
|
||||
@@ -59,6 +62,7 @@ public struct BarChartRow: View {
|
||||
struct BarChartRow_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group {
|
||||
BarChartRow(data: [0], style: styleGreenRed)
|
||||
Group {
|
||||
BarChartRow(data: [1, 2, 3], style: styleGreenRed)
|
||||
BarChartRow(data: [1, 2, 3], style: styleGreenRedWhiteBlack)
|
||||
|
||||
@@ -11,6 +11,9 @@ public struct LineChart: ChartType {
|
||||
struct LineChart_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group {
|
||||
LineChart().makeChart(
|
||||
configuration: .init(data: [0]),
|
||||
style: .init(backgroundColor: .white, foregroundColor: ColorGradient(.black)))
|
||||
Group {
|
||||
LineChart().makeChart(
|
||||
configuration: .init(data: [1, 2, 3, 5, 1]),
|
||||
|
||||
@@ -17,6 +17,10 @@ public struct PieChart: ChartType {
|
||||
struct PieChart_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
Group {
|
||||
PieChart().makeChart(
|
||||
configuration: .init(data: [0]),
|
||||
style: styleOneColor)
|
||||
|
||||
Group {
|
||||
PieChart().makeChart(
|
||||
configuration: .init(data: [56, 78, 53, 65, 54]),
|
||||
|
||||
@@ -12,7 +12,6 @@ struct PieSlice: Identifiable {
|
||||
var startDeg: Double
|
||||
var endDeg: Double
|
||||
var value: Double
|
||||
//var normalizedValue: Double
|
||||
}
|
||||
|
||||
public struct PieChartCell: View {
|
||||
@@ -47,7 +46,7 @@ public struct PieChartCell: View {
|
||||
Group {
|
||||
path
|
||||
.fill(self.accentColor.linearGradient(from: .bottom, to: .top))
|
||||
.overlay(path.stroke(self.backgroundColor, lineWidth: 2))
|
||||
.overlay(path.stroke(self.backgroundColor, lineWidth: (startDeg == 0 && endDeg == 0 ? 0 : 2)))
|
||||
.scaleEffect(self.show ? 1 : 0)
|
||||
.animation(Animation.spring().delay(Double(self.index) * 0.04))
|
||||
.onAppear {
|
||||
@@ -97,6 +96,16 @@ struct PieChartCell_Previews: PreviewProvider {
|
||||
rect: geometry.frame(in: .local),
|
||||
startDeg: 185.0,
|
||||
endDeg: 290.0,
|
||||
index: 1,
|
||||
backgroundColor: Color.purple,
|
||||
accentColor: ColorGradient(.purple))
|
||||
}.frame(width: 100, height: 100)
|
||||
|
||||
GeometryReader { geometry in
|
||||
PieChartCell(
|
||||
rect: geometry.frame(in: .local),
|
||||
startDeg: 0,
|
||||
endDeg: 0,
|
||||
index: 0,
|
||||
backgroundColor: Color.purple,
|
||||
accentColor: ColorGradient(.purple))
|
||||
|
||||
@@ -15,10 +15,10 @@ public struct PieChartRow: View {
|
||||
var slices: [PieSlice] {
|
||||
var tempSlices: [PieSlice] = []
|
||||
var lastEndDeg: Double = 0
|
||||
let maxValue = data.reduce(0, +)
|
||||
let maxValue: Double = data.reduce(0, +)
|
||||
|
||||
for slice in data {
|
||||
let normalized: Double = Double(slice)/Double(maxValue)
|
||||
let normalized: Double = Double(slice) / (maxValue == 0 ? 1 : maxValue)
|
||||
let startDeg = lastEndDeg
|
||||
let endDeg = lastEndDeg + (normalized * 360)
|
||||
lastEndDeg = endDeg
|
||||
@@ -55,12 +55,17 @@ struct PieChartRow_Previews: PreviewProvider {
|
||||
PieChartRow(
|
||||
data: [8, 23, 32, 7, 23, 43],
|
||||
style: defaultMultiColorChartStyle)
|
||||
.frame(width: 100, height: 100)
|
||||
.frame(width: 100, height: 100)
|
||||
|
||||
PieChartRow(
|
||||
data: [8, 23, 32, 7, 23, 43],
|
||||
style: multiColorChartStyle)
|
||||
. frame(width: 100, height: 100)
|
||||
.frame(width: 100, height: 100)
|
||||
|
||||
PieChartRow(
|
||||
data: [0],
|
||||
style: multiColorChartStyle)
|
||||
.frame(width: 100, height: 100)
|
||||
|
||||
}.previewLayout(.fixed(width: 125, height: 125))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user