Restore rotating index for multicolor (#102)

This commit is contained in:
nicolas
2020-05-26 17:04:51 -04:00
committed by GitHub
parent b5beab55e6
commit 99b952fcf4
5 changed files with 80 additions and 3 deletions
@@ -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]
}
}
@@ -1,6 +1,6 @@
import SwiftUI
public struct ColorGradient {
public struct ColorGradient: Equatable {
public let startColor: Color
public let endColor: Color
@@ -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())
@@ -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])
@@ -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)
}
}