17

I use MPAndroidChart to display the stock chart, but the Y line value is so close,the chart is like this enter image description here

I can use finger zoom like this way enter image description here

Which is I want the default looks like

Is there any way to set Y line max and min value or set default zoom.

I tried the lib function , but it does not work

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Urchin
  • 464
  • 1
  • 4
  • 14

7 Answers7

41

Take a look at the documentation of the YAxis. There are various methods that allow you to control the axis range.

  • setStartAtZero(boolean enabled): If this is enabled, this axis will always have it's minimum value at zero (0), no matter which kind of data the chart displays.
  • setAxisMaxValue(float max): Set a custom maximum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
  • setAxisMinValue(float min): Set a custom minimum value for this axis. If set, this value will not be calculated automatically depending on the provided data.
  • setSpaceTop(float percent): Sets the top spacing (in percent of the total axis-range) of the highest value in the chart in comparison to the highest value on the axis.
  • setSpaceBottom(float percent): Sets the bottom spacing (in percent of the total axis-range) of the lowest value in the chart in comparison to the lowest value on the axis.
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • Thx, I will try these methods – Urchin Jul 21 '15 at 17:47
  • But Why this happen, I still confuse ? The max and min value will be calculated automatically. The stock price always stable .it should not be like the first pic. I use the example code, it works perfect .But why my data does not work? Thank you so much. – Urchin Jul 21 '15 at 17:58
  • 1
    I print the max and min value. System.out.println(chart.getYMax()); System.out.println(chart.getYMin()); It is the value I want .but the chart still like one line? why ? Thank you so much. – Urchin Jul 21 '15 at 18:15
  • @Philipp Jahoda in my barchart I have set barchart.getAxisLeft().setAxisMinimum to zero if barchart.getYMin() >= 0,After doing so i need to update the chart data and then if barchart.getYMin() < 0 then how can i reset the axis minimum so that i can show negative value in chart?? – KJEjava48 Dec 17 '16 at 09:23
14

There is a kind of dependency between left and right axises. I had only right axis enabled and set

chart.getAxisRight().setStartAtZero(false);

but that didn't work till I set the same parameter to my leftYAxis, which was disabled. So I have now

 chart.getAxisLeft().setStartAtZero(false);
 chart.getAxisRight().setStartAtZero(false);

edit:

setStartAtZero is deprecated, the code recommends using setAxisMinimum instead.

 * This method is deprecated.
 * Use setAxisMinimum(...) / setAxisMaximum(...) instead.

You can find more on that in the documentation.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Lanitka
  • 922
  • 1
  • 10
  • 20
  • That's what i wanted, Thanks a lot. You saved my time – ajinkya gaurkar Feb 08 '16 at 06:37
  • `chart.getAxisLeft().setStartAtZero(false);` worked for me ... thanks,But now what happens is minValue is 98 it is setting 98 as starting, I want it to be stareted from 95, if minValue is 106 it should be 105 or 100; if minValue is 0.25 then 0.20 & 3.49 then 3.45 ... likewise ... Means starting value should be lesser a bit then the minValue... – Bhuro Sep 14 '16 at 09:52
  • and also on pinch zoom it looks weird as the values are nearby ex: 0.2349,0.2351,0.2334,0.2354,0.2156,0.3002,0.4545,0.1895,0.19010,0.2995,0.2901... so it will display the Yaxis as 0.18, 0.19, 0.21, 0.23, 0.23, 0.23, 0.23, 0.25, 0.29, 0.29, 0.30, ... How to handle this? with your provided solution – Bhuro Sep 14 '16 at 09:57
  • This does not work for me at all on version 2.1.6. What versions are you on? – Sealer_05 Feb 10 '17 at 04:40
  • @Mark_1955 the project is on an old version (my gradle doesn't say anything about it). I cloned it about a year ago and since then hasn't pull changes. – Lanitka Feb 13 '17 at 09:53
  • 1
    Deprecated methods – Ebru Güngör Aug 04 '17 at 11:43
4

You can also use the

mChart.setVisibleYRangeMaximum(150, AxisDependency.LEFT);

method to set highest visible value on y axis.

Himanshu Rathore
  • 108
  • 3
  • 11
1

use setScaleMinima() and chart.moveViewToY(max), YAxis.AxisDependency.LEFT); is the best solution.

Urchin
  • 464
  • 1
  • 4
  • 14
0

please try this , i am not sure that what your want.when i face minim zoom level in MPAndroidChart i used this method like this,

mChart.setPinchZoom(true); mChart.setScaleMinima(200,200); //for min and max value for zoom i Hope to be ok for your problem

yemonkyaw
  • 52
  • 2
  • 6
0

None of the other answers worked for me (I'm not sure why). However, I made it work using:

ratingTracker.getAxisLeft().mAxisMaximum = max;
ratingTracker.getAxisLeft().mAxisMinimum = min;

Hope this works for you!

null_awe
  • 483
  • 6
  • 17
0

Mp Android Line chart in this fix the Y-Axis Minimum and maximum value so your graph line value start middle of the graph chart.

use this function so your problem will solve

yAxis.setAxisMinimum();

yAxis.setAxisMaximum();

in this code you can put any value you want and your graph Y-axis line Minimum and Maximum value

Try below line of code :

YAxis yAxis = chart.getAxisLeft();

yAxis.setEnabled(false);

yAxis.setAxisMinimum(-400);

yAxis.setAxisMaximum(400);