Skip to content

Commit

Permalink
Improvements to startAtZero logic (Fixes issue #300)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Aug 19, 2015
1 parent 2068a5c commit 45daf28
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
45 changes: 34 additions & 11 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,38 +319,61 @@ public class BarLineChartViewBase: ChartViewBase, UIGestureRecognizerDelegate
_chartXMax = Double(_data.xVals.count - 1)
_deltaX = CGFloat(abs(_chartXMax - _chartXMin))

_leftAxis.axisMaximum = !isnan(_leftAxis.customAxisMax) ? _leftAxis.customAxisMax : (maxLeft + topSpaceLeft)
_rightAxis.axisMaximum = !isnan(_rightAxis.customAxisMax) ? _rightAxis.customAxisMax : (maxRight + topSpaceRight)
_leftAxis.axisMinimum = !isnan(_leftAxis.customAxisMin) ? _leftAxis.customAxisMin : (minLeft - bottomSpaceLeft)
_rightAxis.axisMinimum = !isnan(_rightAxis.customAxisMin) ? _rightAxis.customAxisMin : (minRight - bottomSpaceRight)
// Consider sticking one of the edges of the axis to zero (0.0)

// consider starting at zero (0)
if (_leftAxis.isStartAtZeroEnabled)
if _leftAxis.isStartAtZeroEnabled
{
if _leftAxis.axisMinimum < 0.0 && _leftAxis.axisMaximum < 0.0
if minLeft < 0.0 && maxLeft < 0.0
{
// If the values are all negative, let's stay in the negative zone
_leftAxis.axisMinimum = min(0.0, !isnan(_leftAxis.customAxisMin) ? _leftAxis.customAxisMin : (minLeft - bottomSpaceLeft))
_leftAxis.axisMaximum = 0.0
}
else if _leftAxis.axisMinimum >= 0.0
else if minLeft >= 0.0
{
// We have positive values only, stay in the positive zone
_leftAxis.axisMinimum = 0.0
_leftAxis.axisMaximum = max(0.0, !isnan(_leftAxis.customAxisMax) ? _leftAxis.customAxisMax : (maxLeft + topSpaceLeft))
}
else
{
// Stick the minimum to 0.0 or less, and maximum to 0.0 or more (startAtZero for negative/positive at the same time)
_leftAxis.axisMinimum = min(0.0, !isnan(_leftAxis.customAxisMin) ? _leftAxis.customAxisMin : (minLeft - bottomSpaceLeft))
_leftAxis.axisMaximum = max(0.0, !isnan(_leftAxis.customAxisMax) ? _leftAxis.customAxisMax : (maxLeft + topSpaceLeft))
}
}
else
{
// Use the values as they are
_leftAxis.axisMinimum = !isnan(_leftAxis.customAxisMin) ? _leftAxis.customAxisMin : (minLeft - bottomSpaceLeft)
_leftAxis.axisMaximum = !isnan(_leftAxis.customAxisMax) ? _leftAxis.customAxisMax : (maxLeft + topSpaceLeft)
}

if (_rightAxis.isStartAtZeroEnabled)
if _rightAxis.isStartAtZeroEnabled
{
if _rightAxis.axisMinimum < 0.0 && _rightAxis.axisMaximum < 0.0
if minRight < 0.0 && maxRight < 0.0
{
// If the values are all negative, let's stay in the negative zone
_rightAxis.axisMinimum = min(0.0, !isnan(_rightAxis.customAxisMin) ? _rightAxis.customAxisMin : (minRight - bottomSpaceRight))
_rightAxis.axisMaximum = 0.0
}
else if _rightAxis.axisMinimum >= 0.0
else if minRight >= 0.0
{
// We have positive values only, stay in the positive zone
_rightAxis.axisMinimum = 0.0
_rightAxis.axisMaximum = max(0.0, !isnan(_rightAxis.customAxisMax) ? _rightAxis.customAxisMax : (maxRight + topSpaceRight))
}
else
{
// Stick the minimum to 0.0 or less, and maximum to 0.0 or more (startAtZero for negative/positive at the same time)
_rightAxis.axisMinimum = min(0.0, !isnan(_rightAxis.customAxisMin) ? _rightAxis.customAxisMin : (minRight - bottomSpaceRight))
_rightAxis.axisMaximum = max(0.0, !isnan(_rightAxis.customAxisMax) ? _rightAxis.customAxisMax : (maxRight + topSpaceRight))
}
}
else
{
_rightAxis.axisMinimum = !isnan(_rightAxis.customAxisMin) ? _rightAxis.customAxisMin : (minRight - bottomSpaceRight)
_rightAxis.axisMaximum = !isnan(_rightAxis.customAxisMax) ? _rightAxis.customAxisMax : (maxRight + topSpaceRight)
}

_leftAxis.axisRange = abs(_leftAxis.axisMaximum - _leftAxis.axisMinimum)
Expand Down
41 changes: 31 additions & 10 deletions Charts/Classes/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,42 @@ public class RadarChartView: PieRadarChartViewBase

var leftRange = CGFloat(abs(maxLeft - (_yAxis.isStartAtZeroEnabled ? 0.0 : minLeft)))

var topSpaceLeft = leftRange * _yAxis.spaceTop
var bottomSpaceLeft = leftRange * _yAxis.spaceBottom
var topSpaceLeft = Double(leftRange * _yAxis.spaceTop)
var bottomSpaceLeft = Double(leftRange * _yAxis.spaceBottom)

_chartXMax = Double(_data.xVals.count) - 1.0
_deltaX = CGFloat(abs(_chartXMax - _chartXMin))
// Consider sticking one of the edges of the axis to zero (0.0)

_yAxis.axisMaximum = !isnan(_yAxis.customAxisMax) ? _yAxis.customAxisMax : maxLeft + Double(topSpaceLeft)
_yAxis.axisMinimum = !isnan(_yAxis.customAxisMin) ? _yAxis.customAxisMin : minLeft - Double(bottomSpaceLeft)

// consider starting at zero (0)
if (_yAxis.isStartAtZeroEnabled)
if _yAxis.isStartAtZeroEnabled
{
if minLeft < 0.0 && maxLeft < 0.0
{
// If the values are all negative, let's stay in the negative zone
_yAxis.axisMinimum = min(0.0, !isnan(_yAxis.customAxisMin) ? _yAxis.customAxisMin : (minLeft - bottomSpaceLeft))
_yAxis.axisMaximum = 0.0
}
else if minLeft >= 0.0
{
// We have positive values only, stay in the positive zone
_yAxis.axisMinimum = 0.0
_yAxis.axisMaximum = max(0.0, !isnan(_yAxis.customAxisMax) ? _yAxis.customAxisMax : (maxLeft + topSpaceLeft))
}
else
{
// Stick the minimum to 0.0 or less, and maximum to 0.0 or more (startAtZero for negative/positive at the same time)
_yAxis.axisMinimum = min(0.0, !isnan(_yAxis.customAxisMin) ? _yAxis.customAxisMin : (minLeft - bottomSpaceLeft))
_yAxis.axisMaximum = max(0.0, !isnan(_yAxis.customAxisMax) ? _yAxis.customAxisMax : (maxLeft + topSpaceLeft))
}
}
else
{
_yAxis.axisMinimum = min(0.0, _yAxis.axisMinimum)
// Use the values as they are
_yAxis.axisMinimum = !isnan(_yAxis.customAxisMin) ? _yAxis.customAxisMin : (minLeft - bottomSpaceLeft)
_yAxis.axisMaximum = !isnan(_yAxis.customAxisMax) ? _yAxis.customAxisMax : (maxLeft + topSpaceLeft)
}

_chartXMax = Double(_data.xVals.count) - 1.0
_deltaX = CGFloat(abs(_chartXMax - _chartXMin))

_yAxis.axisRange = abs(_yAxis.axisMaximum - _yAxis.axisMinimum)
}

Expand Down

0 comments on commit 45daf28

Please sign in to comment.