Skip to content

Commit

Permalink
Merge pull request #131 from willdale/feature/add-number-formatter-fo…
Browse files Browse the repository at this point in the history
…r-y-axis-labels

Add number formatter for Y axis labels.
  • Loading branch information
willdale authored Nov 6, 2021
2 parents 9f2fc83 + e1fc96e commit 21f4db1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public struct ChartViewData {
*/
var yAxisSpecifier: String = "%.0f"

/// Optional number formatter for the y axis labels when they are `.numeric`.
var yAxisNumberFormatter: NumberFormatter? = nil

/**
Width of the y axis title label once
it has been rotated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ extension CTLineBarChartDataProtocol where Self: GetDataProtocol {
They are either auto calculated numbers
or array of strings.
*/
internal var labelsArray: [String] { self.generateYLabels(self.viewData.yAxisSpecifier) }
internal var labelsArray: [String] {
self.generateYLabels(self.viewData.yAxisSpecifier,
numberFormatter: self.viewData.yAxisNumberFormatter)
}

/**
Labels to display on the Y axis
Expand All @@ -89,14 +92,29 @@ extension CTLineBarChartDataProtocol where Self: GetDataProtocol {
- specifier: Decimal precision of the labels.
- Returns: Array of labels.
*/
private func generateYLabels(_ specifier: String) -> [String] {
private func generateYLabels(_ specifier: String, numberFormatter: NumberFormatter?) -> [String] {
switch self.chartStyle.yAxisLabelType {
case .numeric:
let dataRange: Double = self.range
let minValue: Double = self.minValue
let range: Double = dataRange / Double(self.chartStyle.yAxisNumberOfLabels-1)
let firstLabel = [String(format: specifier, minValue)]
let otherLabels = (1...self.chartStyle.yAxisNumberOfLabels-1).map { String(format: specifier, minValue + range * Double($0)) }
let firstLabel: [String] = {
if let formatter = numberFormatter,
let formattedNumber = formatter.string(from: NSNumber(value:minValue)) {
return [formattedNumber]
} else {
return [String(format: specifier, minValue)]
}
}()
let otherLabels: [String] = (1...self.chartStyle.yAxisNumberOfLabels-1).map {
let value = minValue + range * Double($0)
if let formatter = numberFormatter,
let formattedNumber = formatter.string(from: NSNumber(value: value)) {
return formattedNumber
} else {
return String(format: specifier, value)
}
}
let labels = firstLabel + otherLabels
return labels
case .custom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ internal struct YAxisLabels<T>: ViewModifier where T: CTLineBarChartDataProtocol
internal init(
chartData: T,
specifier: String,
formatter: NumberFormatter?,
colourIndicator: AxisColour
) {
self.chartData = chartData
self.specifier = specifier
self.colourIndicator = colourIndicator
chartData.viewData.hasYAxisLabels = true
chartData.viewData.yAxisSpecifier = specifier
chartData.viewData.yAxisNumberFormatter = formatter
}

internal func body(content: Content) -> some View {
Expand Down Expand Up @@ -81,8 +83,9 @@ extension View {
public func yAxisLabels<T: CTLineBarChartDataProtocol>(
chartData: T,
specifier: String = "%.0f",
formatter: NumberFormatter? = nil,
colourIndicator: AxisColour = .none
) -> some View {
self.modifier(YAxisLabels(chartData: chartData, specifier: specifier, colourIndicator: colourIndicator))
self.modifier(YAxisLabels(chartData: chartData, specifier: specifier, formatter: formatter, colourIndicator: colourIndicator))
}
}

0 comments on commit 21f4db1

Please sign in to comment.