diff --git a/Sources/SwiftUICharts/Base/Extensions/Array+Extension.swift b/Sources/SwiftUICharts/Base/Extensions/Array+Extension.swift new file mode 100644 index 00000000..f2898316 --- /dev/null +++ b/Sources/SwiftUICharts/Base/Extensions/Array+Extension.swift @@ -0,0 +1,22 @@ +// +// File.swift +// +// +// Created by Nicolas Savoini on 2020-05-25. +// + +import Foundation + +extension Array where Element == ColorGradient { + func rotate(for index: Int) -> ColorGradient { + if self.isEmpty { + return ColorGradient.orangeBright + } + + if self.count <= index { + return self[index % self.count] + } + + return self[index] + } +} diff --git a/Sources/SwiftUICharts/Base/Style/ColorGradient.swift b/Sources/SwiftUICharts/Base/Style/ColorGradient.swift index 827e0096..5446cd7d 100644 --- a/Sources/SwiftUICharts/Base/Style/ColorGradient.swift +++ b/Sources/SwiftUICharts/Base/Style/ColorGradient.swift @@ -1,6 +1,6 @@ import SwiftUI -public struct ColorGradient { +public struct ColorGradient: Equatable { public let startColor: Color public let endColor: Color diff --git a/Sources/SwiftUICharts/Charts/BarChart/BarChartRow.swift b/Sources/SwiftUICharts/Charts/BarChart/BarChartRow.swift index 9abe01aa..4a517597 100644 --- a/Sources/SwiftUICharts/Charts/BarChart/BarChartRow.swift +++ b/Sources/SwiftUICharts/Charts/BarChart/BarChartRow.swift @@ -23,7 +23,7 @@ public struct BarChartRow: View { index: index, width: Float(geometry.frame(in: .local).width - Constant.spacing), numberOfDataPoints: self.data.count, - gradientColor: self.style.foregroundColor.first!, + gradientColor: self.style.foregroundColor.rotate(for: index), touchLocation: self.$touchLocation) .scaleEffect(self.getScaleSize(touchLocation: self.touchLocation, index: index), anchor: .bottom) .animation(.spring()) diff --git a/Sources/SwiftUICharts/PieChart/PieChartRow.swift b/Sources/SwiftUICharts/PieChart/PieChartRow.swift index b4d8d003..f9428179 100644 --- a/Sources/SwiftUICharts/PieChart/PieChartRow.swift +++ b/Sources/SwiftUICharts/PieChart/PieChartRow.swift @@ -38,7 +38,7 @@ public struct PieChartRow: View { endDeg: self.slices[index].endDeg, index: index, backgroundColor: self.style.backgroundColor.startColor, - accentColor: self.style.foregroundColor.first! + accentColor: self.style.foregroundColor.rotate(for: index) ) } @@ -56,6 +56,12 @@ struct PieChartRow_Previews: PreviewProvider { data: [8, 23, 32, 7, 23, 43], style: defaultMultiColorChartStyle) .frame(width: 100, height: 100) + + PieChartRow( + data: [8, 23, 32, 7, 23, 43], + style: multiColorChartStyle) + . frame(width: 100, height: 100) + }.previewLayout(.fixed(width: 125, height: 125)) } @@ -64,3 +70,7 @@ struct PieChartRow_Previews: PreviewProvider { private let defaultMultiColorChartStyle = ChartStyle( backgroundColor: Color.white, foregroundColor: [ColorGradient]()) + +private let multiColorChartStyle = ChartStyle( +backgroundColor: Color.purple, +foregroundColor: [ColorGradient.greenRed, ColorGradient.whiteBlack]) diff --git a/Tests/SwiftUIChartsTests/ArrayExtensionTests.swift b/Tests/SwiftUIChartsTests/ArrayExtensionTests.swift new file mode 100644 index 00000000..35b1e304 --- /dev/null +++ b/Tests/SwiftUIChartsTests/ArrayExtensionTests.swift @@ -0,0 +1,45 @@ +// +// File.swift +// +// +// Created by Nicolas Savoini on 2020-05-25. +// + +@testable import SwiftUICharts +import XCTest + +class ArrayExtensionTests: XCTestCase { + + func testArrayRotatingIndexEmpty() { + let colors = [ColorGradient]() + XCTAssertEqual(colors.rotate(for: 0), ColorGradient.orangeBright) + } + + func testArrayRotatingIndexOneValue() { + let colors = [ColorGradient.greenRed] + + XCTAssertEqual(colors.rotate(for: 0), ColorGradient.greenRed) + XCTAssertEqual(colors.rotate(for: 1), ColorGradient.greenRed) + XCTAssertEqual(colors.rotate(for: 2), ColorGradient.greenRed) + } + + func testArrayRotatingIndexLessValues() { + let colors = [ColorGradient.greenRed, ColorGradient.whiteBlack] + + XCTAssertEqual(colors.rotate(for: 0), ColorGradient.greenRed) + XCTAssertEqual(colors.rotate(for: 1), ColorGradient.whiteBlack) + XCTAssertEqual(colors.rotate(for: 2), ColorGradient.greenRed) + XCTAssertEqual(colors.rotate(for: 3), ColorGradient.whiteBlack) + XCTAssertEqual(colors.rotate(for: 4), ColorGradient.greenRed) + } + + func testArrayRotatingIndexMoreValues() { + let colors = [ColorGradient.greenRed, ColorGradient.whiteBlack, ColorGradient.orangeBright] + + XCTAssertEqual(colors.rotate(for: 0), ColorGradient.greenRed) + XCTAssertEqual(colors.rotate(for: 1), ColorGradient.whiteBlack) + + } + +} +