Skip to content

Commit

Permalink
Allow setting selectioncolor per each dataelement (#15)
Browse files Browse the repository at this point in the history
* Update Github Actions scripts

* Allow setting selectionColor per each DataElement

Closes #14

* Fix Swiftlint warning
  • Loading branch information
marekpridal authored Nov 12, 2023
1 parent 286c10a commit a2f15cb
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 59 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/Publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ jobs:
runs-on: macOS-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Set Xcode version
run: sudo xcode-select --switch /Applications/Xcode_14.2.app

- name: Generate Xcode project
run: swift package generate-xcodeproj
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

steps:
- name: Checkout branch
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Build
run: swift build -v
11 changes: 4 additions & 7 deletions .github/workflows/swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ on: [push, pull_request]


jobs:
swiftlint:
runs-on: ubuntu-18.04

SwiftLint:
runs-on: ubuntu-latest
steps:
- name: Checkout branch
uses: actions/checkout@v1

- uses: actions/checkout@v4
- name: GitHub Action for SwiftLint with --strict
uses: norio-nomura/action-swiftlint@3.1.0
uses: norio-nomura/action-swiftlint@3.2.1
with:
args: --strict
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ Instead of adding a repository URL, you can search for a package on [GitHub](htt
import BarChartKit

let mockBarChartDataSet: BarChartView.DataSet? = BarChartView.DataSet(elements: [
BarChartView.DataSet.DataElement(date: nil, xLabel: "Jan", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor.green), BarChartView.DataSet.DataElement.Bar(value: 15000, color: UIColor.blue)]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Feb", bars: [BarChartView.DataSet.DataElement.Bar(value: 0, color: UIColor.green)]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Mar", bars: [BarChartView.DataSet.DataElement.Bar(value: 10000, color: UIColor.green), BarChartView.DataSet.DataElement.Bar(value: 5000, color: UIColor.blue)]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Apr", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor.green), BarChartView.DataSet.DataElement.Bar(value: 15000, color: UIColor.blue)]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "May", bars: [BarChartView.DataSet.DataElement.Bar(value: 32000, color: UIColor.green), BarChartView.DataSet.DataElement.Bar(value: 15000, color: UIColor.blue)]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Jun", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor.green)]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Jul", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor.green), BarChartView.DataSet.DataElement.Bar(value: 0.5555, color: UIColor.blue)])
], selectionColor: UIColor.yellow)
BarChartView.DataSet.DataElement(date: nil, xLabel: "Jan", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0)),
BarChartView.DataSet.DataElement.Bar(value: 15000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0))]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Feb", bars: [BarChartView.DataSet.DataElement.Bar(value: 0, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0))]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Mar", bars: [BarChartView.DataSet.DataElement.Bar(value: 10000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0)),
BarChartView.DataSet.DataElement.Bar(value: 5000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0))]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Apr", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0)),
BarChartView.DataSet.DataElement.Bar(value: 15000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0))]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "May", bars: [BarChartView.DataSet.DataElement.Bar(value: 32010, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0)),
BarChartView.DataSet.DataElement.Bar(value: 15000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0))]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Jun", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor(red: 214/255, green: 40/255, blue: 57/255, alpha: 1.0))]),
BarChartView.DataSet.DataElement(date: nil, xLabel: "Jul", bars: [BarChartView.DataSet.DataElement.Bar(value: 20000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor.systemGreen),
BarChartView.DataSet.DataElement.Bar(value: 10000, color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), selectionColor: UIColor.systemBlue)])
], limit: .init(color: UIColor(red: 208/255, green: 207/255, blue: 209/255, alpha: 1.0), label: "YOUR LIMIT", value: 15_010))


let barChart = BarChartView()
Expand All @@ -58,19 +63,19 @@ import BarChartKit
@State private var selectedElement: BarChart.DataSet.DataElement? = mockBarChartDataSet.elements.first

let mockBarChartDataSet: BarChart.DataSet = BarChart.DataSet(elements: [
BarChart.DataSet.DataElement(date: nil, xLabel: "Jan", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jan", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green, selectionColor: Color.yellow),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue, selectionColor: Color.yellow)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Feb", bars: [BarChart.DataSet.DataElement.Bar(value: 0, color: Color.green)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Mar", bars: [BarChart.DataSet.DataElement.Bar(value: 10000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 5000, color: Color.red)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Apr", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "May", bars: [BarChart.DataSet.DataElement.Bar(value: 42000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jun", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jul", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 0.5555, color: Color.blue)])
], selectionColor: Color.yellow)
BarChart.DataSet.DataElement(date: nil, xLabel: "May", bars: [BarChart.DataSet.DataElement.Bar(value: 42000, color: Color.green, selectionColor: Color.red),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue, selectionColor: Color.yellow)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jun", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green, selectionColor: Color.yellow)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jul", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green, selectionColor: Color.yellow),
BarChart.DataSet.DataElement.Bar(value: 0.5555, color: Color.blue, selectionColor: Color.red)])
])


BarChart(dataSet: mockBarChartDataSet, selectedElement: $selectedElement)
Expand Down
31 changes: 16 additions & 15 deletions Sources/BarChartKit/SwiftUI/BarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public struct BarChart: View {
public let value: Double
/// Default color for the bar in not selected state
public let color: Color
/// Color used when element is selected
public let selectionColor: Color?

/// Create a single bar to be displayed in the BarChart
/// - Parameters:
/// - value: Any floating point number to be represented in the bar
/// - color: Default color for the bar in not selected state
public init(value: Double, color: Color) {
/// - selectionColor: Color used when element is selected
public init(value: Double, color: Color, selectionColor: Color? = nil) {
self.value = value
self.color = color
self.selectionColor = selectionColor
}
}

Expand All @@ -41,15 +45,12 @@ public struct BarChart: View {
}

public let elements: [DataElement]
public let selectionColor: Color?

/// Create a set of data to be displayed in the BarChart
/// - Parameters:
/// - elements: Data to be displayed
/// - selectionColor: Color of the bars when in selected state
public init(elements: [BarChart.DataSet.DataElement], selectionColor: Color?) {
public init(elements: [BarChart.DataSet.DataElement]) {
self.elements = elements
self.selectionColor = selectionColor
}
}

Expand All @@ -72,7 +73,7 @@ public struct BarChart: View {
Rectangle()
.frame(width: barWidth, height: self.height(for: bar, viewHeight: geometry.size.height, maxValue: self.maxDataSetValue))
.cornerRadius(barWidth / 2, antialiased: false)
.foregroundColor(self.selectedElement == element ? self.dataSet.selectionColor ?? bar.color : bar.color)
.foregroundColor(self.selectedElement == element ? bar.selectionColor ?? bar.color : bar.color)
}
.frame(maxWidth: .infinity, maxHeight: geometry.size.height, alignment: .bottom)
}
Expand Down Expand Up @@ -139,23 +140,23 @@ extension BarChart.DataSet.DataElement.Bar: Equatable {
#if DEBUG
// swiftlint:disable all
fileprivate var mockBarChartDataSet: BarChart.DataSet = BarChart.DataSet(elements: [
BarChart.DataSet.DataElement(date: nil, xLabel: "Jan", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jan", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green, selectionColor: Color.yellow),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue, selectionColor: Color.yellow)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Feb", bars: [BarChart.DataSet.DataElement.Bar(value: 0, color: Color.green)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Mar", bars: [BarChart.DataSet.DataElement.Bar(value: 10000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 5000, color: Color.red)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Apr", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "May", bars: [BarChart.DataSet.DataElement.Bar(value: 42000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jun", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jul", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green),
BarChart.DataSet.DataElement.Bar(value: 0.5555, color: Color.blue)])
], selectionColor: Color.yellow)
BarChart.DataSet.DataElement(date: nil, xLabel: "May", bars: [BarChart.DataSet.DataElement.Bar(value: 42000, color: Color.green, selectionColor: Color.red),
BarChart.DataSet.DataElement.Bar(value: 15000, color: Color.blue, selectionColor: Color.yellow)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jun", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green, selectionColor: Color.yellow)]),
BarChart.DataSet.DataElement(date: nil, xLabel: "Jul", bars: [BarChart.DataSet.DataElement.Bar(value: 20000, color: Color.green, selectionColor: Color.yellow),
BarChart.DataSet.DataElement.Bar(value: 0.5555, color: Color.blue, selectionColor: Color.red)])
])
// swiftlint:enable all

struct ParentView: View {
@State private var selectedElement: BarChart.DataSet.DataElement? = mockBarChartDataSet.elements.first
@State private var selectedElement: BarChart.DataSet.DataElement? = mockBarChartDataSet.elements.safe(at: 4)

var body: some View {
VStack(spacing: 10) {
Expand Down
Loading

0 comments on commit a2f15cb

Please sign in to comment.