| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- //
- // DefaultAxisValueFormatter.swift
- // Charts
- //
- // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
- // A port of MPAndroidChart for iOS
- // Licensed under Apache License 2.0
- //
- // https://github.com/danielgindi/Charts
- //
- import Foundation
- @objc(ChartDefaultAxisValueFormatter)
- open class DefaultAxisValueFormatter: NSObject, AxisValueFormatter
- {
- public typealias Block = (
- _ value: Double,
- _ axis: AxisBase?) -> String
-
- @objc open var block: Block?
-
- @objc open var hasAutoDecimals: Bool = false
-
- private var _formatter: NumberFormatter?
- @objc open var formatter: NumberFormatter?
- {
- get { return _formatter }
- set
- {
- hasAutoDecimals = false
- _formatter = newValue
- }
- }
- // TODO: Documentation. Especially the nil case
- private var _decimals: Int?
- open var decimals: Int?
- {
- get { return _decimals }
- set
- {
- _decimals = newValue
-
- if let digits = newValue
- {
- self.formatter?.minimumFractionDigits = digits
- self.formatter?.maximumFractionDigits = digits
- self.formatter?.usesGroupingSeparator = true
- }
- }
- }
-
- public override init()
- {
- super.init()
-
- self.formatter = NumberFormatter()
- hasAutoDecimals = true
- }
-
- @objc public init(formatter: NumberFormatter)
- {
- super.init()
-
- self.formatter = formatter
- }
-
- @objc public init(decimals: Int)
- {
- super.init()
-
- self.formatter = NumberFormatter()
- self.formatter?.usesGroupingSeparator = true
- self.decimals = decimals
- hasAutoDecimals = true
- }
-
- @objc public init(block: @escaping Block)
- {
- super.init()
-
- self.block = block
- }
-
- @objc public static func with(block: @escaping Block) -> DefaultAxisValueFormatter?
- {
- return DefaultAxisValueFormatter(block: block)
- }
-
- open func stringForValue(_ value: Double,
- axis: AxisBase?) -> String
- {
- if let block = block {
- return block(value, axis)
- } else {
- return formatter?.string(from: NSNumber(floatLiteral: value)) ?? ""
- }
- }
- }
|