Skip to content

Commit

Permalink
Feature to allow forcing YAxis label-count
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Jul 26, 2015
1 parent d538322 commit edd9c2e
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 65 deletions.
35 changes: 24 additions & 11 deletions Charts/Classes/Components/ChartYAxis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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; }
Expand Down
70 changes: 49 additions & 21 deletions Charts/Classes/Renderers/ChartYAxisRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down
94 changes: 61 additions & 33 deletions Charts/Classes/Renderers/ChartYAxisRendererRadarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit edd9c2e

Please sign in to comment.