From edd9c2ec6d433ba9599d4939080a690dc199ca86 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Sun, 26 Jul 2015 22:53:26 +0300 Subject: [PATCH] Feature to allow forcing YAxis label-count --- Charts/Classes/Components/ChartYAxis.swift | 35 ++++--- .../Renderers/ChartYAxisRenderer.swift | 70 +++++++++----- .../ChartYAxisRendererRadarChart.swift | 94 ++++++++++++------- 3 files changed, 134 insertions(+), 65 deletions(-) diff --git a/Charts/Classes/Components/ChartYAxis.swift b/Charts/Classes/Components/ChartYAxis.swift index 069a767450..8544053b4c 100644 --- a/Charts/Classes/Components/ChartYAxis.swift +++ b/Charts/Classes/Components/ChartYAxis.swift @@ -53,6 +53,9 @@ public class ChartYAxis: ChartAxisBase /// if true, the y-label entries will always start at zero public var startAtZeroEnabled = true + /// if true, the set number of y-labels will be forced + public var forceLabelsEnabled = true + /// the formatter used to customly format the y-labels public var valueFormatter: NSNumberFormatter? @@ -127,6 +130,22 @@ public class ChartYAxis: ChartAxisBase return _axisDependency } + public func setLabelCount(count: Int, force: Bool) + { + _labelCount = count + + if (_labelCount > 25) + { + _labelCount = 25 + } + if (_labelCount < 2) + { + _labelCount = 2 + } + + forceLabelsEnabled = force + } + /// the number of label entries the y-axis should have /// max = 25, /// min = 2, @@ -140,16 +159,7 @@ public class ChartYAxis: ChartAxisBase } set { - _labelCount = newValue - - if (_labelCount > 25) - { - _labelCount = 25 - } - if (_labelCount < 2) - { - _labelCount = 2 - } + setLabelCount(newValue, force: false); } } @@ -224,7 +234,10 @@ public class ChartYAxis: ChartAxisBase public var isInverted: Bool { return inverted; } public var isStartAtZeroEnabled: Bool { return startAtZeroEnabled; } - + + /// :returns: true if focing the y-label count is enabled. Default: false + public var isForceLabelsEnabled: Bool { return forceLabelsEnabled } + public var isShowOnlyMinMaxEnabled: Bool { return showOnlyMinMaxEnabled; } public var isDrawTopYLabelEntryEnabled: Bool { return drawTopYLabelEntryEnabled; } diff --git a/Charts/Classes/Renderers/ChartYAxisRenderer.swift b/Charts/Classes/Renderers/ChartYAxisRenderer.swift index 981d4efa9b..b09bbcfcea 100644 --- a/Charts/Classes/Renderers/ChartYAxisRenderer.swift +++ b/Charts/Classes/Renderers/ChartYAxisRenderer.swift @@ -78,37 +78,65 @@ public class ChartYAxisRenderer: ChartAxisRendererBase interval = floor(10.0 * intervalMagnitude) } - // if the labels should only show min and max - if (_yAxis.isShowOnlyMinMaxEnabled) + // force label count + if _yAxis.isForceLabelsEnabled { - _yAxis.entries = [yMin, yMax] - } - else - { - var first = ceil(Double(yMin) / interval) * interval - var last = ChartUtils.nextUp(floor(Double(yMax) / interval) * interval) + let step = Double(range) / Double(labelCount - 1) - var f: Double - var i: Int - var n = 0 - for (f = first; f <= last; f += interval) + if _yAxis.entries.count < labelCount { - ++n + // Ensure stops contains at least numStops elements. + _yAxis.entries.removeAll(keepCapacity: true) } - - if (_yAxis.entries.count < n) + else { - // Ensure stops contains at least numStops elements. - _yAxis.entries = [Double](count: n, repeatedValue: 0.0) + _yAxis.entries = [Double]() + _yAxis.entries.reserveCapacity(labelCount) } - else if (_yAxis.entries.count > n) + + var v = yMin + + for (var i = 0; i < labelCount; i++) { - _yAxis.entries.removeRange(n..<_yAxis.entries.count) + _yAxis.entries.append(v) + v += step } - for (f = first, i = 0; i < n; f += interval, ++i) + } else { + // no forced count + + // if the labels should only show min and max + if (_yAxis.isShowOnlyMinMaxEnabled) + { + _yAxis.entries = [yMin, yMax] + } + else { - _yAxis.entries[i] = Double(f) + var first = ceil(Double(yMin) / interval) * interval + var last = ChartUtils.nextUp(floor(Double(yMax) / interval) * interval) + + var f: Double + var i: Int + var n = 0 + for (f = first; f <= last; f += interval) + { + ++n + } + + if (_yAxis.entries.count < n) + { + // Ensure stops contains at least numStops elements. + _yAxis.entries = [Double](count: n, repeatedValue: 0.0) + } + else if (_yAxis.entries.count > n) + { + _yAxis.entries.removeRange(n..<_yAxis.entries.count) + } + + for (f = first, i = 0; i < n; f += interval, ++i) + { + _yAxis.entries[i] = Double(f) + } } } } diff --git a/Charts/Classes/Renderers/ChartYAxisRendererRadarChart.swift b/Charts/Classes/Renderers/ChartYAxisRendererRadarChart.swift index 7ec1a3cf98..d034824ba1 100644 --- a/Charts/Classes/Renderers/ChartYAxisRendererRadarChart.swift +++ b/Charts/Classes/Renderers/ChartYAxisRendererRadarChart.swift @@ -54,52 +54,80 @@ public class ChartYAxisRendererRadarChart: ChartYAxisRenderer interval = floor(10 * intervalMagnitude) } - // clean old values - if (_yAxis.entries.count > 0) + // force label count + if _yAxis.isForceLabelsEnabled { - _yAxis.entries.removeAll(keepCapacity: false) - } - - // if the labels should only show min and max - if (_yAxis.isShowOnlyMinMaxEnabled) - { - _yAxis.entries = [Double]() - _yAxis.entries.append(yMin) - _yAxis.entries.append(yMax) - } - else - { - var first = ceil(Double(yMin) / interval) * interval + let step = Double(range) / Double(labelCount - 1) - if (first == 0.0) - { // Fix for IEEE negative zero case (Where value == -0.0, and 0.0 == -0.0) - first = 0.0 + if _yAxis.entries.count < labelCount + { + // Ensure stops contains at least numStops elements. + _yAxis.entries.removeAll(keepCapacity: true) + } + else + { + _yAxis.entries = [Double]() + _yAxis.entries.reserveCapacity(labelCount) } - var last = ChartUtils.nextUp(floor(Double(yMax) / interval) * interval) + var v = yMin - var f: Double - var i: Int - var n = 0 - for (f = first; f <= last; f += interval) + for (var i = 0; i < labelCount; i++) { - ++n + _yAxis.entries.append(v) + v += step } - if (isnan(_yAxis.customAxisMax)) + } else { + // no forced count + + // clean old values + if (_yAxis.entries.count > 0) { - n += 1 + _yAxis.entries.removeAll(keepCapacity: false) } - - if (_yAxis.entries.count < n) + + // if the labels should only show min and max + if (_yAxis.isShowOnlyMinMaxEnabled) { - // Ensure stops contains at least numStops elements. - _yAxis.entries = [Double](count: n, repeatedValue: 0.0) + _yAxis.entries = [Double]() + _yAxis.entries.append(yMin) + _yAxis.entries.append(yMax) } - - for (f = first, i = 0; i < n; f += interval, ++i) + else { - _yAxis.entries[i] = Double(f) + var first = ceil(Double(yMin) / interval) * interval + + if (first == 0.0) + { // Fix for IEEE negative zero case (Where value == -0.0, and 0.0 == -0.0) + first = 0.0 + } + + var last = ChartUtils.nextUp(floor(Double(yMax) / interval) * interval) + + var f: Double + var i: Int + var n = 0 + for (f = first; f <= last; f += interval) + { + ++n + } + + if (isnan(_yAxis.customAxisMax)) + { + n += 1 + } + + if (_yAxis.entries.count < n) + { + // Ensure stops contains at least numStops elements. + _yAxis.entries = [Double](count: n, repeatedValue: 0.0) + } + + for (f = first, i = 0; i < n; f += interval, ++i) + { + _yAxis.entries[i] = Double(f) + } } }