Clay Ellis Clay Ellis - 5 months ago 43
iOS Question

iOS Charts - set minimum y axis range

I'm using the fabulous iOS Charts library (https://github.com/danielgindi/ios-charts) and I'm having a hard time finding anything to support this behavior in the documentation. I'd like to force the y-axis to always display a range from 0-35. Currently, if I only have one data entry in at say (0, 9) the y-axis will display its range as 0-9. However when I add another entry at (1, 32) suddenly I can see the top of the graph at 35.

There are three methods that look promising (because they're related) but they aren't quite providing the type of behavior that I'm looking for.

1:

Chart.setVisibleYRangeMaximum
(if only it were
Minimum
...)

2:
Chart.setVisibleXRangeMaximum
(useless again...)

3:
Chart.setVisibleXRangeMinimum
(now where's your
YRange
cousin?!)

So anyways, that's where I'm at. I've read documentation but to no avail. Help me SO?

Answer

Take a look at below properties. Should be able to solve your problem.

/// 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. 
/// Do not forget to set startAtZeroEnabled = false if you use this method.
/// Otherwise, the axis-minimum value will still be forced to 0.
public var customAxisMin = Double.NaN

/// Set 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

/// if true, the y-label entries will always start at zero
public var startAtZeroEnabled = true

For example:

_chartView.leftAxis.customAxisMax = 35;
_chartView.leftAxis.customAxisMin = 0;

or _chartView.leftAxis.startAtZeroEnabled = YES;