Skip to content

Commit 3a15db1

Browse files
committed
Added currency identifier for more flexibility
1 parent 23d3843 commit 3a15db1

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

Sources/CheesyChart/Extensions/Double+Extensions.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ extension Double {
1414
/// ```
1515
/// Convert 1234.56 to $1,234.56
1616
/// ```
17-
private var currencyFormatter: NumberFormatter {
17+
private func currencyFormatter(currencyIdentifier: String) -> NumberFormatter {
1818
let formatter = NumberFormatter()
1919
formatter.usesGroupingSeparator = true
2020
formatter.numberStyle = .currency
21+
formatter.locale = Locale(identifier: currencyIdentifier)
2122
formatter.minimumFractionDigits = 2
2223
formatter.maximumFractionDigits = 2
2324
return formatter
@@ -27,9 +28,9 @@ extension Double {
2728
/// ```
2829
/// Convert 1234.56 to "$1,234.56"
2930
/// ```
30-
func asCurrencyWithTwoDecimals() -> String {
31+
func asCurrencyWithTwoDecimals(currencyIdentifier: String) -> String {
3132
let number = NSNumber(value: self)
32-
return currencyFormatter.string(from: number) ?? "$0.00"
33+
return currencyFormatter(currencyIdentifier: currencyIdentifier).string(from: number) ?? "$0.00"
3334
}
3435

3536
/// Converts a Double into string representation

Sources/CheesyChart/HelperViews/ChartPriceLabelView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct ChartPriceLabelView: View {
5757
/// Only nesessary if we are using a custom view with a binding to track the draged label price. This is important if the user creates multiple buttons to switch different chart data. It could happen that some data has more chart data. If so the app crashes. To avoid it we are checking if the vm.point is higher that the chart data count. If yes, we asign a blank String otherwise the normal price.
5858
/// - Returns: Formatted price as a String
5959
private func checkInput() -> String {
60-
return vm.point > setup.data.count ? "" : setup.data[vm.point].asCurrencyWithTwoDecimals()
60+
return vm.point > setup.data.count ? "" : setup.data[vm.point].asCurrencyWithTwoDecimals(currencyIdentifier: setup.currencyIdentifier)
6161
}
6262
}
6363

Sources/CheesyChart/HelperViews/Header/PriceLabelView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct PriceLabelView: View {
1717

1818
// MARK: - Body
1919
var body: some View {
20-
Text(setup.data[vm.point].asCurrencyWithTwoDecimals())
20+
Text(setup.data[vm.point].asCurrencyWithTwoDecimals(currencyIdentifier: setup.currencyIdentifier))
2121
.foregroundColor(setup.chartHeaderFontColor)
2222
.font(.footnote)
2323
}

Sources/CheesyChart/Main/CheesyChart.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public struct CheesyChart: View {
6868
}
6969

7070
private func handleGesture(value: DragGesture.Value) {
71+
print(value.location.x)
7172
vm.hide = true
7273
vm.touchLocation = value.location
7374
/// Calculates each width of one price

Sources/CheesyChart/Setup/SetupChart.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class SetupChart {
1313
// MARK: - Properties
1414
public var name: String
1515
public var data: [Double]
16+
public var currencyIdentifier: String
1617
public var image: UIImage?
1718

1819
public var showChartHeader: Bool
@@ -56,6 +57,7 @@ public class SetupChart {
5657
public init(
5758
name: String = "",
5859
data: [Double] = [],
60+
currencyIdentifier: String = "en_US",
5961
image: UIImage? = UIImage(systemName: "bitcoinsign.circle.fill"),
6062

6163
showChartHeader: Bool = false,
@@ -95,6 +97,7 @@ public class SetupChart {
9597
) {
9698
self.name = name
9799
self.data = data
100+
self.currencyIdentifier = currencyIdentifier
98101
self.image = image
99102

100103
self.showChartHeader = showChartHeader

0 commit comments

Comments
 (0)