PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.6k stars 9.02k forks source link

Line Chart setLabelCount() causing misalignment #4653

Open ghost opened 5 years ago

ghost commented 5 years ago

I am using MPAndroidChart line chart in an android app where the x-axis is days in a month with a minimum of 1 and a maximum of the number of days in a given month and the y-axis is the data.

So for August xAxis.setAxisMinimum(1) and xAxis.setAxisMaximum(31).

Now the issue is I need to set the x-axis label count to 15 and also have the first x-axis value be 1 and the last value be 31, but setLabelCount(15, true) causes the line points to be misaligned either too far to the left or too far to the right.

Using setLabelCount(15), not using force true boolean, results in the points being aligned but then the xaxis values start at 2 and end at 30.

I am aware that the documentation states: setLabelCount(int count, boolean force): Sets the number of labels for the y-axis. Be aware that this number is not fixed (if force == false) and can only be approximated. If force is enabled (true), then the exact specified label-count is drawn – this can lead to uneven numbers on the axis.

but I have found no other way to set the number of labels on the x-axis, and cannot find a work around to use setLabelCount(15,true) and align the points of the line graph.

PhanTuanKiet commented 5 years ago

You should use a custom IndexAxisValueFormatter to set XAxis label, it more flexible than just use setLabelCount Reference : https://stackoverflow.com/questions/45320457/how-to-set-string-value-of-xaxis-in-mpandroidchart