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

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

Related

MPAndroidChart - scale linechart

So I'm using MPAndroidChart to draw a linechart using data from a Firebase Real-time Database.
This is how it shows:
The first point is 2100 and the rest of the points are around 5-6, but because is not scaled, it shows them at 0. How can I scale the chart to show them correctly or can I plot the last 10 points?
From what you write, I understand that all the points are displayed correctly, but the chart is not very useful because of the outlier. You could zoom in to the lower part of the chart, e.g.:
chart.zoom(1f, 10f, 0f, 0f)
This zooms in by a factor of 10 on the Y axis and leaves the X axis as is.
Another solution would be a logarithmic scale, but this seems not to be supported by MpAndroidChart.

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 Programmatically Change YAxis Range

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();

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 Scatterchart in ListView

I am using the MPAndroidChart library.
I have multiple scattercharts in a ListView.
Every chart contains 365 xvalues (every day of the year).
The yvalues vary from 1 to 5.
The height of the charts is 150dp.
I would like to be able to zoom in, so I can see every single day.
But when I zoom in, the yvalues get out of range of the chart.
Is there a way to keep the yvalues within range of the chart?
I tried it with the next settings:
holder.chart.setTouchEnabled(true);
holder.chart.setDragEnabled(true);
holder.chart.setScaleEnabled(false);
holder.chart.setScaleMinima(3f, 0f);
holder.chart.centerViewPort(xIndex, 3);
But when I drag from left to right or right to left, values just disappear from the chart, even when in range.
Has anyone any idea how to accomplish this?
This is your issue:
holder.chart.setScaleMinima(3f, 0f);
You are setting the scale value of the y-axis to zero. By doing that, you practically zoom out the chart into infinity. If you do not want to manipulate the zoom/scale of the y-axis, set the y-scale to 1f. Like this:
holder.chart.setScaleMinima(3f, 1f);
Try to invalidate the chart after centerViewPort, worked for me.
holder.chart.invalidate();

Categories

Resources