Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Sources/SwiftUICharts/Base/Extensions/Array+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// File.swift

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array+Extension.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]
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftUICharts/Base/Style/ColorGradient.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

public struct ColorGradient {
public struct ColorGradient: Equatable {
public let startColor: Color
public let endColor: Color

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftUICharts/Charts/BarChart/BarChartRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
12 changes: 11 additions & 1 deletion Sources/SwiftUICharts/PieChart/PieChartRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}

Expand All @@ -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))

}
Expand All @@ -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])
45 changes: 45 additions & 0 deletions Tests/SwiftUIChartsTests/ArrayExtensionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// File.swift

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayExtensionTests.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)

}

Copy link

@AdrianBinDC AdrianBinDC May 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 on unit tests. That's what was sorely lacking on master. Nice to see folks throwing them in there as they write code, rather than treating them as an afterthought.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Working on my TDD skills;) Small functions like that are perfect for it.

}