MPAndroidChart Programmatically Change YAxis Range - android

Is there a way to programmatically change the YAxis Range of a chart in MPAndroidChart?
For instance, given the YAxis displays 0,10, I want to programmatically set the YAxis to -5,+5, but I want to do this several times during the course of an application.

From the documentation, I think you should give a try with a combination of :
setVisibleYRangeMaximum(float maxYRange, AxisDependency axis): Sets the size of the area (range on the y-axis) that should be maximum visible at once. You also need to provide the axis this constraint should apply to.
moveViewToY(float yValue, AxisDependency axis): Centers the viewport to the specified y-value on the provided y-axis (left or right).
Code for your example:
yourChart.setVisibleYRangeMaximum(10, YAxis.AxisDependency.LEFT);
yourChart.moveViewToY(0, YAxis.AxisDependency.LEFT);
yourChart.invalidate();

Related

How to set x-axis label for every y-value in MPAndroidChart?

I want to set a label in the x-axis for every y-value I have in my LineChart. I want to obtain this result:
Can someone help me in finding the correct way of doing this in MPAndroidChart?
You may be looking for granularity - see the wiki page for the axis and the javadoc.
Calling like this:
mChart.getXAxis().setGranularity(0.3f);
mChart.getXAxis().setGranularityEnabled(true);
will ensure that 0.3 is the minimum interval on the axis when you are zoomed in. Apart from that you may have to tweak the the scale of the x-axis by calling:
mChart.getXAxis().setAxisMinValue(10.01f); //experiment with these values
mChart.getXAxis().setAxisMaxValue(10.14f);
mChart.getXAxis().setLabelCount(5);

MPAndroidChart: How to create dual axis?

I am using MPAndroidChart for drawing line chart.
I need to draw a Dual YAxis Line chart (i.e. with 2 Y Axis, one on left and other on right). But when I draw the graph it is being drawn from left. It takes into consideration the Left YAxis values rather than the Right YAxis values.
I am drawing Weights(kg) on right side and Heights(ft) on the left side.
As weights will be in terms of 40s, 50s etc and heights in terms of 5, 6 etc... The Line being drawn for Height takes left reference, which has 50s, 60s and hence never comes up.
Please let me know how to direct to draw considering the right Y Axis for Height rather than left Y Axis.
You can just use the setAxisDependency function in order to let a DataSet depend on a given axis. In your case it should be set to right:
LineDataSet set = new LineDataSet(data, "Your Label");
set.setAxisDependency(YAxis.AxisDependency.RIGHT); // plot this set against the right axis

How to create space between x-axis labels in MPAndroidChart?

I am using MPAndroidChart library.
I'm trying to make spaces between labels in the XAxis of my line chart.
I have tried this but nothing changes:
xAxis.setSpaceBetweenLabels(someInt);
A bit late, but you can increase the zoom level using zoom(scaleX, scaleY, x, y) method. Increase the scaleX value and you will start seeing spaces between labels on the x axis. A good estimation is dividing the number of labels by 10. So if you have 500 labels, use zoom(50f, 0.5f, 1f, 1f).
xAxis.labelCount = 3
//replace the integer with whatever you need
You can use xAxis.setLabelCount(some int);
or
chart.setVisibleXRangeMaximum(some float);
which sets the size of the area (range on the x-axis) that should be minimum visible at once.
You can read it also in the documentation
https://github.com/PhilJay/MPAndroidChart/wiki/The-Axis
and for the View Port https://github.com/PhilJay/MPAndroidChart/wiki/Modifying-the-Viewport

MPAndroid candlestick combined with volume bar chart height issue

I need suggestion on how to limit the MpAndroid CandleStick chart combined with volume bar chart at the bottom so that volume bar max height can be limit to below 1/3 of chart area Yaxis . From the chart attached , this is somewhere Y Max at 3million region viewport in the left Axis. The current combined chart is ruin by the volume bar chart.
Thanks
setVisibleXRangeMaximum(float maxXRange): Sets the size of the area (range on the x-axis) that should be maximum visible at once. If this is e.g. set to 10, no more than 10 values on the x-axis can be viewed at once without scrolling.
setVisibleXRangeMinimum(float minXRange): Sets the size of the area (range on the x-axis) that should be minimum visible at once. If this is e.g. set to 10, it is not possible to zoom in further than 10 values on the x-axis.
setVisibleYRangeMaximum(float maxYRange, AxisDependency axis): Sets the size of the area (range on the y-axis) that should be maximum visible at once. You also need to provide the axis this constraint should apply to.
For more info Refer this
Thanks for your comment ,I do not think setting XrangeMax or Min will help , it only limit the x-axis down to certain visible display range instead of showing the full range of the dataset points . The chart shown above has about more than 4000 data points , I have narrowed the visible x-range using set chart.setVisibleXRangeMaximum(100) and also chart.moveViewToX(3000) to optimise performance. What I need is some functional procedure to set the max Y-axis (eg left Y-axis) at certain specific offset position , so that the lower portion of the chart could be restricted to the volume bar chart by clipping the specific rectangle viewports. I am not sure if that is possible , narrowing the YVsibleYrange is not what I have in mind. What I would like to have is
That is possible. All you need to do is plot the data against two different axes (as you are already doing), and then set the top space for each axis separately.
Customizing the axis range
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.
More information in the documentation.
To workaround , I have coded the Y axis to be at 1/x1 of Y viewport height to mark the volume bar top at YMAX in the multiple of x1 where x1 variable can be ={3, 4 or 5} float ymax=barDataSet.getYMax(); float ymaxd=(float)Math.floor (ymax*x1); leftAxis.setLabelCount(x1,false); leftAxis.setAxisMaxValue(ymaxd);

MPAndroidChart values intersection with legend & removing y value from above the bar

How can i solve the problem of the values intersecting with the legend while maintaning the same position for the legend, or is it not possible?
Also how can i remove the value from above the bar
I have tried using
mChart.setDrawValueAboveBar(false);
mChart.setDrawValuesForWholeStack(false);
But they are no good
You can increase the space between the largest value and the top of the graph:
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setSpaceTop(40f); // e.g. 40% space
What this does is setting 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.
As for removing the y-values that are drawn by the bars: https://github.com/PhilJay/MPAndroidChart/wiki/The-ChartData-class
Call setDrawValues(false) on your BarData or BarDataSet object.

Categories

Resources