Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 520
Clay Ellis Avatar asked Aug 24 '15 07:08

Clay Ellis


4 Answers

For Charts 3 (tried on Charts 3.0.2), this works:

chartView.leftAxis.axisMinimum = 0

By default there is a rightAxis as well (at least for bar charts) and the grid won't be aligned if you don't also set:

chartView.rightAxis.axisMinimum = 0

Note: I wrote this as a comment initially but I think it should be an answer to be easier to find for people who end up here from Google.

like image 134
Vlad V Avatar answered Oct 13 '22 08:10

Vlad V


    linechart2.leftAxis.axisMinimum = chartDataline.getYMin() - 0.1

To get the graph to focus only on the y range of values, get the minimum value of the data set LineChartData, and if you want subtract a buffer for some padding.

Some persons say there use to be "linechart2.leftAxis.startAtZeroEnabled = false" but that is no longer seeing in code.

like image 36
JonesJr876 Avatar answered Sep 28 '22 07:09

JonesJr876


customAxisMin is Deprecated in latest Charts.

Use property axisMinValue instead!

like image 13
skywinder Avatar answered Oct 13 '22 09:10

skywinder


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;

like image 9
Wingzero Avatar answered Oct 13 '22 10:10

Wingzero