Skip to content

Commit

Permalink
Finally unified the two confusing values axisMinimum and customAxisMi…
Browse files Browse the repository at this point in the history
…n/ax
  • Loading branch information
danielgindi committed Mar 30, 2016
1 parent f1a84d6 commit 6cdb595
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 133 deletions.
70 changes: 11 additions & 59 deletions Charts/Classes/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar

if (_leftAxis.isEnabled)
{
_leftYAxisRenderer?.computeAxis(yMin: _leftAxis.axisMinimum, yMax: _leftAxis.axisMaximum)
_leftYAxisRenderer?.computeAxis(yMin: _leftAxis._axisMinimum, yMax: _leftAxis._axisMaximum)
}
if (_rightAxis.isEnabled)
{
_rightYAxisRenderer?.computeAxis(yMin: _rightAxis.axisMinimum, yMax: _rightAxis.axisMaximum)
_rightYAxisRenderer?.computeAxis(yMin: _rightAxis._axisMinimum, yMax: _rightAxis._axisMaximum)
}

_xAxisRenderer?.renderAxisLine(context: context)
Expand Down Expand Up @@ -283,8 +283,8 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar

internal func prepareValuePxMatrix()
{
_rightAxisTransformer.prepareMatrixValuePx(chartXMin: _chartXMin, deltaX: _deltaX, deltaY: CGFloat(_rightAxis.axisRange), chartYMin: _rightAxis.axisMinimum)
_leftAxisTransformer.prepareMatrixValuePx(chartXMin: _chartXMin, deltaX: _deltaX, deltaY: CGFloat(_leftAxis.axisRange), chartYMin: _leftAxis.axisMinimum)
_rightAxisTransformer.prepareMatrixValuePx(chartXMin: _chartXMin, deltaX: _deltaX, deltaY: CGFloat(_rightAxis.axisRange), chartYMin: _rightAxis._axisMinimum)
_leftAxisTransformer.prepareMatrixValuePx(chartXMin: _chartXMin, deltaX: _deltaX, deltaY: CGFloat(_leftAxis.axisRange), chartYMin: _leftAxis._axisMinimum)
}

internal func prepareOffsetMatrix()
Expand All @@ -300,8 +300,8 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
_leftAxis?._defaultValueFormatter = _defaultValueFormatter
_rightAxis?._defaultValueFormatter = _defaultValueFormatter

_leftYAxisRenderer?.computeAxis(yMin: _leftAxis.axisMinimum, yMax: _leftAxis.axisMaximum)
_rightYAxisRenderer?.computeAxis(yMin: _rightAxis.axisMinimum, yMax: _rightAxis.axisMaximum)
_leftYAxisRenderer?.computeAxis(yMin: _leftAxis._axisMinimum, yMax: _leftAxis._axisMaximum)
_rightYAxisRenderer?.computeAxis(yMin: _rightAxis._axisMinimum, yMax: _rightAxis._axisMaximum)

if let data = _data
{
Expand All @@ -325,60 +325,12 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar
_data?.calcMinMax(start: lowestVisibleXIndex, end: highestVisibleXIndex)
}

var minLeft = !isnan(_leftAxis.customAxisMin)
? _leftAxis.customAxisMin
: _data?.getYMin(.Left) ?? 0.0
var maxLeft = !isnan(_leftAxis.customAxisMax)
? _leftAxis.customAxisMax
: _data?.getYMax(.Left) ?? 0.0
var minRight = !isnan(_rightAxis.customAxisMin)
? _rightAxis.customAxisMin
: _data?.getYMin(.Right) ?? 0.0
var maxRight = !isnan(_rightAxis.customAxisMax)
? _rightAxis.customAxisMax
: _data?.getYMax(.Right) ?? 0.0

let leftRange = abs(maxLeft - minLeft)
let rightRange = abs(maxRight - minRight)

// in case all values are equal
if (leftRange == 0.0)
{
maxLeft = maxLeft + 1.0
minLeft = minLeft - 1.0
}

if (rightRange == 0.0)
{
maxRight = maxRight + 1.0
minRight = minRight - 1.0
}

let topSpaceLeft = leftRange * Double(_leftAxis.spaceTop)
let topSpaceRight = rightRange * Double(_rightAxis.spaceTop)
let bottomSpaceLeft = leftRange * Double(_leftAxis.spaceBottom)
let bottomSpaceRight = rightRange * Double(_rightAxis.spaceBottom)

_chartXMax = Double((_data?.xVals.count ?? 0) - 1)
_deltaX = CGFloat(abs(_chartXMax - _chartXMin))

// Use the values as they are
_leftAxis.axisMinimum = !isnan(_leftAxis.customAxisMin)
? _leftAxis.customAxisMin
: (minLeft - bottomSpaceLeft)
_leftAxis.axisMaximum = !isnan(_leftAxis.customAxisMax)
? _leftAxis.customAxisMax
: (maxLeft + topSpaceLeft)

_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)
_rightAxis.axisRange = abs(_rightAxis.axisMaximum - _rightAxis.axisMinimum)
// calculate axis range (min / max) according to provided data
_leftAxis.calcMinMax(min: _data?.getYMin(.Left) ?? 0.0, max: _data?.getYMax(.Left) ?? 0.0)
_rightAxis.calcMinMax(min: _data?.getYMin(.Right) ?? 0.0, max: _data?.getYMax(.Right) ?? 0.0)
}

internal override func calculateOffsets()
Expand Down Expand Up @@ -1818,12 +1770,12 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar

public override var chartYMax: Double
{
return max(leftAxis.axisMaximum, rightAxis.axisMaximum)
return max(leftAxis._axisMaximum, rightAxis._axisMaximum)
}

public override var chartYMin: Double
{
return min(leftAxis.axisMinimum, rightAxis.axisMinimum)
return min(leftAxis._axisMinimum, rightAxis._axisMinimum)
}

/// - returns: true if either the left or the right or both axes are inverted.
Expand Down
4 changes: 2 additions & 2 deletions Charts/Classes/Charts/HorizontalBarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public class HorizontalBarChartView: BarChartView

internal override func prepareValuePxMatrix()
{
_rightAxisTransformer.prepareMatrixValuePx(chartXMin: _rightAxis.axisMinimum, deltaX: CGFloat(_rightAxis.axisRange), deltaY: _deltaX, chartYMin: _chartXMin)
_leftAxisTransformer.prepareMatrixValuePx(chartXMin: _leftAxis.axisMinimum, deltaX: CGFloat(_leftAxis.axisRange), deltaY: _deltaX, chartYMin: _chartXMin)
_rightAxisTransformer.prepareMatrixValuePx(chartXMin: _rightAxis._axisMinimum, deltaX: CGFloat(_rightAxis.axisRange), deltaY: _deltaX, chartYMin: _chartXMin)
_leftAxisTransformer.prepareMatrixValuePx(chartXMin: _leftAxis._axisMinimum, deltaX: CGFloat(_leftAxis.axisRange), deltaY: _deltaX, chartYMin: _chartXMin)
}

internal override func calcModulus()
Expand Down
31 changes: 4 additions & 27 deletions Charts/Classes/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,10 @@ public class RadarChartView: PieRadarChartViewBase
super.calcMinMax()
guard let data = _data else { return }

let minLeft = !isnan(_yAxis.customAxisMin)
? _yAxis.customAxisMin
: data.getYMin(.Left)
let maxLeft = !isnan(_yAxis.customAxisMax)
? _yAxis.customAxisMax
: data.getYMax(.Left)

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

let leftRange = CGFloat(abs(maxLeft - minLeft))

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

// 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)
_yAxis.calcMinMax(min: data.getYMin(.Left), max: data.getYMax(.Left))
}

public override func getMarkerPosition(entry entry: ChartDataEntry, highlight: ChartHighlight) -> CGPoint
Expand All @@ -125,7 +102,7 @@ public class RadarChartView: PieRadarChartViewBase

_yAxis?._defaultValueFormatter = _defaultValueFormatter

_yAxisRenderer?.computeAxis(yMin: _yAxis.axisMinimum, yMax: _yAxis.axisMaximum)
_yAxisRenderer?.computeAxis(yMin: _yAxis._axisMinimum, yMax: _yAxis._axisMaximum)
_xAxisRenderer?.computeAxis(xValAverageLength: data?.xValAverageLength ?? 0, xValues: data?.xVals ?? [])

if let data = _data, legend = _legend where !legend.isLegendCustom
Expand Down Expand Up @@ -252,10 +229,10 @@ public class RadarChartView: PieRadarChartViewBase
}

/// - returns: the maximum value this chart can display on it's y-axis.
public override var chartYMax: Double { return _yAxis.axisMaximum; }
public override var chartYMax: Double { return _yAxis._axisMaximum; }

/// - returns: the minimum value this chart can display on it's y-axis.
public override var chartYMin: Double { return _yAxis.axisMinimum; }
public override var chartYMin: Double { return _yAxis._axisMinimum; }

/// - returns: the range of y-values this chart can display.
public var yRange: Double { return _yAxis.axisRange}
Expand Down
99 changes: 84 additions & 15 deletions Charts/Classes/Components/ChartYAxis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ public class ChartYAxis: ChartAxisBase
{
get
{
return customAxisMin == 0.0
return isAxisMinCustom && _axisMinimum == 0.0
}
set
{
if newValue
{
customAxisMin = 0.0
axisMinValue = 0.0
}
else
{
Expand All @@ -75,6 +75,41 @@ public class ChartYAxis: ChartAxisBase
}
}

/// The minimum value for this axis.
/// If set, this value will not be calculated automatically depending on the provided data.
/// Use `resetCustomAxisMin()` to undo this.
public var axisMinValue: Double
{
get
{
return _axisMinimum
}
set
{
customAxisMin = true
_axisMinimum = newValue
}
}

/// The maximum value for this axis.
/// If set, this value will not be calculated automatically depending on the provided data.
/// Use `resetCustomAxisMin()` to undo this.
public var axisMaxValue: Double
{
get
{
return _axisMaximum
}
set
{
customAxisMax = true
_axisMaximum = newValue
}
}

public var isAxisMinCustom: Bool { return customAxisMin }
public var isAxisMaxCustom: Bool { return customAxisMax }

/// if true, the set number of y-labels will be forced
public var forceLabelsEnabled = false

Expand All @@ -101,24 +136,20 @@ public class ChartYAxis: ChartAxisBase
/// the formatter used to customly format the y-labels
internal var _defaultValueFormatter = NSNumberFormatter()

/// A custom minimum value for this axis.
/// If set, this value will not be calculated automatically depending on the provided data.
/// Use `resetCustomAxisMin()` to undo this.
public var customAxisMin = Double.NaN

/// A custom maximum value for this axis.
/// If set, this value will not be calculated automatically depending on the provided data.
/// Use `resetCustomAxisMax()` to undo this.
public var customAxisMax = Double.NaN
/// Flag indicating that the axis-min value has been customized
private var customAxisMin: Bool = false

/// Flag indicating that the axis-max value has been customized
private var customAxisMax: Bool = false

/// axis space from the largest value to the top in percent of the total axis range
public var spaceTop = CGFloat(0.1)

/// axis space from the smallest value to the bottom in percent of the total axis range
public var spaceBottom = CGFloat(0.1)

public var axisMaximum = Double(0)
public var axisMinimum = Double(0)
public var _axisMaximum = Double(0)
public var _axisMinimum = Double(0)

/// the total range of values this axis covers
public var axisRange = Double(0)
Expand Down Expand Up @@ -217,13 +248,13 @@ public class ChartYAxis: ChartAxisBase
/// By calling this method, any custom minimum value that has been previously set is reseted, and the calculation is done automatically.
public func resetCustomAxisMin()
{
customAxisMin = Double.NaN
customAxisMin = false
}

/// By calling this method, any custom maximum value that has been previously set is reseted, and the calculation is done automatically.
public func resetCustomAxisMax()
{
customAxisMax = Double.NaN
customAxisMax = false
}

public func requiredSize() -> CGSize
Expand Down Expand Up @@ -294,4 +325,42 @@ public class ChartYAxis: ChartAxisBase
public var isShowOnlyMinMaxEnabled: Bool { return showOnlyMinMaxEnabled; }

public var isDrawTopYLabelEntryEnabled: Bool { return drawTopYLabelEntryEnabled; }

/// Calculates the minimum, maximum and range values of the YAxis with the given minimum and maximum values from the chart data.
/// - parameter dataMin: the y-min value according to chart data
/// - parameter dataMax: the y-max value according to chart
public func calcMinMax(min dataMin: Double, max dataMax: Double)
{
// if custom, use value as is, else use data value
var min = customAxisMin ? _axisMinimum : dataMin
var max = customAxisMax ? _axisMaximum : dataMax

// temporary range (before calculations)
let range = abs(max - min)

// in case all values are equal
if range == 0.0
{
max = max + 1.0
min = min - 1.0
}

// bottom-space only effects non-custom min
if !customAxisMin
{
let bottomSpace = range / 100.0 * Double(spaceBottom)
_axisMinimum = min - bottomSpace
}

// top-space only effects non-custom max
if !customAxisMax
{
let topSpace = range / 100.0 * Double(spaceTop)
_axisMaximum = max + topSpace
}

// calc actual range
axisRange = abs(_axisMaximum - _axisMinimum)
}

}
10 changes: 5 additions & 5 deletions Charts/Classes/Renderers/ChartYAxisRendererRadarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public class ChartYAxisRendererRadarChart: ChartYAxisRenderer
n += 1
}

if (isnan(yAxis.customAxisMax))
if !yAxis.isAxisMaxCustom
{
n += 1
}
Expand All @@ -147,10 +147,10 @@ public class ChartYAxisRendererRadarChart: ChartYAxisRenderer
{
// If startAtZero is disabled, and the first label is lower that the axis minimum,
// Then adjust the axis minimum
yAxis.axisMinimum = yAxis.entries[0]
yAxis._axisMinimum = yAxis.entries[0]
}
yAxis.axisMaximum = yAxis.entries[yAxis.entryCount - 1]
yAxis.axisRange = abs(yAxis.axisMaximum - yAxis.axisMinimum)
yAxis._axisMaximum = yAxis.entries[yAxis.entryCount - 1]
yAxis.axisRange = abs(yAxis._axisMaximum - yAxis._axisMinimum)
}

public override func renderAxisLabels(context context: CGContext)
Expand Down Expand Up @@ -182,7 +182,7 @@ public class ChartYAxisRendererRadarChart: ChartYAxisRenderer
break
}

let r = CGFloat(yAxis.entries[j] - yAxis.axisMinimum) * factor
let r = CGFloat(yAxis.entries[j] - yAxis._axisMinimum) * factor

let p = ChartUtils.getPosition(center: center, dist: r, angle: chart.rotationAngle)

Expand Down
4 changes: 2 additions & 2 deletions ChartsDemo/Classes/Demos/BarChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ - (void)viewDidLoad
leftAxis.valueFormatter.positiveSuffix = @" $";
leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
leftAxis.spaceTop = 0.15;
leftAxis.customAxisMin = 0.0; // this replaces startAtZero = YES
leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES

ChartYAxis *rightAxis = _chartView.rightAxis;
rightAxis.enabled = YES;
Expand All @@ -78,7 +78,7 @@ - (void)viewDidLoad
rightAxis.labelCount = 8;
rightAxis.valueFormatter = leftAxis.valueFormatter;
rightAxis.spaceTop = 0.15;
rightAxis.customAxisMin = 0.0; // this replaces startAtZero = YES
rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES

_chartView.legend.position = ChartLegendPositionBelowChartLeft;
_chartView.legend.form = ChartLegendFormSquare;
Expand Down
2 changes: 1 addition & 1 deletion ChartsDemo/Classes/Demos/BubbleChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ - (void)viewDidLoad
yl.labelFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:10.f];
yl.spaceTop = 0.3;
yl.spaceBottom = 0.3;
yl.customAxisMin = 0.0; // this replaces startAtZero = YES
yl.axisMinValue = 0.0; // this replaces startAtZero = YES

_chartView.rightAxis.enabled = NO;

Expand Down
Loading

0 comments on commit 6cdb595

Please sign in to comment.