From 69d12af46ce4068109f8bce951eb4b6a07cf9f43 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Mon, 13 Nov 2017 16:38:46 -0800 Subject: [PATCH 1/2] Minor changes to Formatter logic --- .../DefaultAxisValueFormatter.swift | 18 ++---- .../Formatters/DefaultFillFormatter.swift | 55 +++++-------------- .../Formatters/DefaultValueFormatter.swift | 11 +--- .../Formatters/IndexAxisValueFormatter.swift | 7 +-- 4 files changed, 22 insertions(+), 69 deletions(-) diff --git a/Source/Charts/Formatters/DefaultAxisValueFormatter.swift b/Source/Charts/Formatters/DefaultAxisValueFormatter.swift index dc46a00caa..b4f9c584fc 100644 --- a/Source/Charts/Formatters/DefaultAxisValueFormatter.swift +++ b/Source/Charts/Formatters/DefaultAxisValueFormatter.swift @@ -22,7 +22,7 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter @objc open var hasAutoDecimals: Bool = false - fileprivate var _formatter: NumberFormatter? + private var _formatter: NumberFormatter? @objc open var formatter: NumberFormatter? { get { return _formatter } @@ -32,8 +32,9 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter _formatter = newValue } } - - fileprivate var _decimals: Int? + + // TODO: Documentation. Especially the nil case + private var _decimals: Int? open var decimals: Int? { get { return _decimals } @@ -90,14 +91,7 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter open func stringForValue(_ value: Double, axis: AxisBase?) -> String { - if block != nil - { - return block!(value, axis) - } - else - { - return formatter?.string(from: NSNumber(floatLiteral: value)) ?? "" - } + return block?(value, axis) ?? + (formatter?.string(from: NSNumber(floatLiteral: value)) ?? "") } - } diff --git a/Source/Charts/Formatters/DefaultFillFormatter.swift b/Source/Charts/Formatters/DefaultFillFormatter.swift index 9fd2af4d39..f9e368b3c7 100644 --- a/Source/Charts/Formatters/DefaultFillFormatter.swift +++ b/Source/Charts/Formatters/DefaultFillFormatter.swift @@ -26,10 +26,7 @@ open class DefaultFillFormatter: NSObject, IFillFormatter @objc open var block: Block? - public override init() - { - - } + public override init() { } @objc public init(block: @escaping Block) { @@ -45,47 +42,21 @@ open class DefaultFillFormatter: NSObject, IFillFormatter dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat { - if block != nil + guard block == nil else { return block!(dataSet, dataProvider) } + var fillMin = CGFloat(0.0) + + if dataSet.yMax > 0.0 && dataSet.yMin < 0.0 { - return block!(dataSet, dataProvider) + fillMin = 0.0 } - else + else if let data = dataProvider.data { - var fillMin = CGFloat(0.0) - - if dataSet.yMax > 0.0 && dataSet.yMin < 0.0 - { - fillMin = 0.0 - } - else - { - if let data = dataProvider.data - { - var max: Double, min: Double - - if data.yMax > 0.0 - { - max = 0.0 - } - else - { - max = dataProvider.chartYMax - } - - if data.yMin < 0.0 - { - min = 0.0 - } - else - { - min = dataProvider.chartYMin - } - - fillMin = CGFloat(dataSet.yMin >= 0.0 ? min : max) - } - } - - return fillMin + let max = data.yMax > 0.0 ? 0.0 : dataProvider.chartYMax + let min = data.yMin < 0.0 ? 0.0 : dataProvider.chartYMin + + fillMin = CGFloat(dataSet.yMin >= 0.0 ? min : max) } + + return fillMin } } diff --git a/Source/Charts/Formatters/DefaultValueFormatter.swift b/Source/Charts/Formatters/DefaultValueFormatter.swift index ce812b6556..568a6a7dd5 100644 --- a/Source/Charts/Formatters/DefaultValueFormatter.swift +++ b/Source/Charts/Formatters/DefaultValueFormatter.swift @@ -94,14 +94,7 @@ open class DefaultValueFormatter: NSObject, IValueFormatter dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String { - if block != nil - { - return block!(value, entry, dataSetIndex, viewPortHandler) - } - else - { - return formatter?.string(from: NSNumber(floatLiteral: value)) ?? "" - } + return block?(value, entry, dataSetIndex, viewPortHandler) ?? + (formatter?.string(from: NSNumber(floatLiteral: value)) ?? "") } - } diff --git a/Source/Charts/Formatters/IndexAxisValueFormatter.swift b/Source/Charts/Formatters/IndexAxisValueFormatter.swift index 88b937a197..9655123984 100644 --- a/Source/Charts/Formatters/IndexAxisValueFormatter.swift +++ b/Source/Charts/Formatters/IndexAxisValueFormatter.swift @@ -53,12 +53,7 @@ open class IndexAxisValueFormatter: NSObject, IAxisValueFormatter axis: AxisBase?) -> String { let index = Int(value.rounded()) - - if index < 0 || index >= _valueCount || index != Int(value) - { - return "" - } - + guard values.indices.contains(index), index != Int(value) else { return "" } return _values[index] } } From 9ea3c208ba979697299b7b5aff1861052168b9f1 Mon Sep 17 00:00:00 2001 From: Jacob Christie Date: Tue, 14 Nov 2017 23:34:13 -0800 Subject: [PATCH 2/2] Fixes for PR --- Source/Charts/Formatters/DefaultAxisValueFormatter.swift | 7 +++++-- Source/Charts/Formatters/DefaultFillFormatter.swift | 2 +- Source/Charts/Formatters/DefaultValueFormatter.swift | 7 +++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Source/Charts/Formatters/DefaultAxisValueFormatter.swift b/Source/Charts/Formatters/DefaultAxisValueFormatter.swift index b4f9c584fc..85193d0c46 100644 --- a/Source/Charts/Formatters/DefaultAxisValueFormatter.swift +++ b/Source/Charts/Formatters/DefaultAxisValueFormatter.swift @@ -91,7 +91,10 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter open func stringForValue(_ value: Double, axis: AxisBase?) -> String { - return block?(value, axis) ?? - (formatter?.string(from: NSNumber(floatLiteral: value)) ?? "") + if let block = block { + return block(value, axis) + } else { + return formatter?.string(from: NSNumber(floatLiteral: value)) ?? "" + } } } diff --git a/Source/Charts/Formatters/DefaultFillFormatter.swift b/Source/Charts/Formatters/DefaultFillFormatter.swift index f9e368b3c7..ea9c2a7465 100644 --- a/Source/Charts/Formatters/DefaultFillFormatter.swift +++ b/Source/Charts/Formatters/DefaultFillFormatter.swift @@ -43,7 +43,7 @@ open class DefaultFillFormatter: NSObject, IFillFormatter dataProvider: LineChartDataProvider) -> CGFloat { guard block == nil else { return block!(dataSet, dataProvider) } - var fillMin = CGFloat(0.0) + var fillMin: CGFloat = 0.0 if dataSet.yMax > 0.0 && dataSet.yMin < 0.0 { diff --git a/Source/Charts/Formatters/DefaultValueFormatter.swift b/Source/Charts/Formatters/DefaultValueFormatter.swift index 568a6a7dd5..0e238bf7a7 100644 --- a/Source/Charts/Formatters/DefaultValueFormatter.swift +++ b/Source/Charts/Formatters/DefaultValueFormatter.swift @@ -94,7 +94,10 @@ open class DefaultValueFormatter: NSObject, IValueFormatter dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String { - return block?(value, entry, dataSetIndex, viewPortHandler) ?? - (formatter?.string(from: NSNumber(floatLiteral: value)) ?? "") + if let block = block { + return block(value, entry, dataSetIndex, viewPortHandler) + } else { + return formatter?.string(from: NSNumber(floatLiteral: value)) ?? "" + } } }