From 0b64edaaa1d204cef03365fddcba40b46aa47b83 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 11 Dec 2017 04:10:47 -0400 Subject: [PATCH 1/3] Moved the default value formatter It is now simply `DefaultValueFomatter()` Removed unnecessary backing ivars in `DefaultValuetFormatter` in favour of property observers Deprecated static func constructor in favour of initializer --- .../Implementations/ChartBaseDataSet.swift | 2 +- .../Formatters/DefaultValueFormatter.swift | 64 +++++++++---------- Source/Charts/Utils/ChartUtils.swift | 14 ---- 3 files changed, 31 insertions(+), 49 deletions(-) diff --git a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift index 7136f2f04b..763ab36609 100644 --- a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift +++ b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift @@ -279,7 +279,7 @@ open class ChartBaseDataSet: NSObject, IChartDataSet { if needsFormatter { - return ChartUtils.defaultValueFormatter() + return DefaultValueFormatter() } return _valueFormatter diff --git a/Source/Charts/Formatters/DefaultValueFormatter.swift b/Source/Charts/Formatters/DefaultValueFormatter.swift index aac13f1b41..a6450e9382 100644 --- a/Source/Charts/Formatters/DefaultValueFormatter.swift +++ b/Source/Charts/Formatters/DefaultValueFormatter.swift @@ -11,6 +11,7 @@ import Foundation +/// The default value formatter used for all chart components that needs a default @objc(ChartDefaultValueFormatter) open class DefaultValueFormatter: NSObject, IValueFormatter { @@ -22,69 +23,65 @@ open class DefaultValueFormatter: NSObject, IValueFormatter @objc open var block: Block? - @objc open var hasAutoDecimals: Bool = false + @objc open var hasAutoDecimals: Bool - private var _formatter: NumberFormatter? - @objc open var formatter: NumberFormatter? - { - get { return _formatter } - set - { + @objc open var formatter: NumberFormatter? { + willSet { hasAutoDecimals = false - _formatter = newValue } } - private var _decimals: Int? - open var decimals: Int? - { - get { return _decimals } - set - { - _decimals = newValue - - if let digits = newValue + open var decimals: Int? { + didSet { + if let digits = decimals { - self.formatter?.minimumFractionDigits = digits - self.formatter?.maximumFractionDigits = digits - self.formatter?.usesGroupingSeparator = true + formatter?.minimumFractionDigits = digits + formatter?.maximumFractionDigits = digits + formatter?.usesGroupingSeparator = true } } } public override init() { - super.init() - - self.formatter = NumberFormatter() + formatter = NumberFormatter() + formatter?.usesGroupingSeparator = true + decimals = 1 hasAutoDecimals = true + + super.init() } @objc public init(formatter: NumberFormatter) { - super.init() - self.formatter = formatter + hasAutoDecimals = false + + super.init() } @objc public init(decimals: Int) { - super.init() - - self.formatter = NumberFormatter() - self.formatter?.usesGroupingSeparator = true + formatter = NumberFormatter() + formatter?.usesGroupingSeparator = true self.decimals = decimals hasAutoDecimals = true + + super.init() } @objc public init(block: @escaping Block) { - super.init() - self.block = block + hasAutoDecimals = false + + super.init() } - - @objc public static func with(block: @escaping Block) -> DefaultValueFormatter? + + /// This function is deprecated - Use `init(block:)` instead. + // DEC 11, 2017 + @available(*, deprecated: 3.0, message: "Use `init(block:)` instead.") + @objc public static func with(block: @escaping Block) -> DefaultValueFormatter { return DefaultValueFormatter(block: block) } @@ -103,5 +100,4 @@ open class DefaultValueFormatter: NSObject, IValueFormatter return formatter?.string(from: NSNumber(floatLiteral: value)) ?? "" } } - } diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index d5b441e164..be74cd5171 100644 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -49,8 +49,6 @@ extension CGSize { open class ChartUtils { - private static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter() - internal class func roundToNextSignificant(number: Double) -> Double { if number.isInfinite || number.isNaN || number == 0 @@ -272,18 +270,6 @@ open class ChartUtils drawMultilineText(context: context, text: text, knownTextSize: rect.size, point: point, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians) } - private class func generateDefaultValueFormatter() -> IValueFormatter - { - let formatter = DefaultValueFormatter(decimals: 1) - return formatter - } - - /// - returns: The default value formatter used for all chart components that needs a default - open class func defaultValueFormatter() -> IValueFormatter - { - return _defaultValueFormatter - } - /// MARK: - Bridging functions internal class func bridgedObjCGetNSUIColorArray (swift array: [NSUIColor?]) -> [NSObject] From 45207aef777e03785f383d2a66478d2a725c73c9 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Sun, 21 Jan 2018 20:26:00 -0400 Subject: [PATCH 2/3] Fixes after merge. --- Source/Charts/Utils/ChartUtils.swift | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Source/Charts/Utils/ChartUtils.swift b/Source/Charts/Utils/ChartUtils.swift index b0ddba2032..df819c3619 100644 --- a/Source/Charts/Utils/ChartUtils.swift +++ b/Source/Charts/Utils/ChartUtils.swift @@ -97,23 +97,6 @@ extension CGPoint } } -open class ChartUtils -{ - private static var _defaultValueFormatter: ValueFormatter = generateDefaultValueFormatter() - - private class func generateDefaultValueFormatter() -> ValueFormatter - { - let formatter = DefaultValueFormatter(decimals: 1) - return formatter - } - - /// - returns: The default value formatter used for all chart components that needs a default - open class func defaultValueFormatter() -> ValueFormatter - { - return _defaultValueFormatter - } -} - extension CGContext { open func drawImage(_ image: NSUIImage, atCenter center: CGPoint, size: CGSize) From e1628de8d97d6695f208ef037853bf4032b028dd Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Fri, 26 Jan 2018 07:12:10 -0400 Subject: [PATCH 3/3] Merged latest 4.0.0 --- .../Implementations/ChartBaseDataSet.swift | 26 ++----------------- 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift index 61d226258c..190241efa3 100644 --- a/Source/Charts/Data/Implementations/ChartBaseDataSet.swift +++ b/Source/Charts/Data/Implementations/ChartBaseDataSet.swift @@ -270,30 +270,8 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol open var isHighlightEnabled: Bool { return highlightEnabled } /// Custom formatter that is used instead of the auto-formatter if set - open var valueFormatter: ValueFormatter? - { - get - { - if needsFormatter - { - return DefaultValueFormatter() - } - - return _valueFormatter - } - set - { - if newValue == nil { return } - - _valueFormatter = newValue - } - } - - open var needsFormatter: Bool - { - return _valueFormatter == nil - } - + open lazy var valueFormatter: ValueFormatter = DefaultValueFormatter() + /// Sets/get a single color for value text. /// Setting the color clears the colors array and adds a single color. /// Getting will return the first color in the array.