Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force BarChart Y axis labels to be integers?

I've created a BarChart using MPAndroidChart and I'm entering the data dynamically. This means that I need my Y axis to also be determined dynamically. All of my data is represented as integers, however the Y axis is sometimes displaying the legend as decimal values with 1 decimal point.

I've attempted to use a ValueFormatter to round the values, but the problem is that sometimes the Y values aren't at integer value locations (ex. 0.8,1.6,2.4,etc). Therefore, if I just edit these to be integers they won't be in the right location.

Is there anyway that I can force the BarChart to only display values at Integer locations? Its ok if it skips some, in fact I want it to when the values get large.

Edit: When I said that the integers aren't in the right locations, I meant that once I modify what each label displays they aren't 'correct'. In my example I have 5 bars displaying the values 3,4,2,3,2. The first image is the 'default', the second is the image once I've edited the value formatter using:

myBarChart.getYLabels().setFormatter(new ValueFormatter()
{
    @Override
    public String getFormattedValue(float v)
    {
        return ((int) v)+"";
    }
});

enter image description hereenter image description here

As we can see from these images, my integer values are not where they should be (and there are two '2s'). In this example I'd except it to display the values 0,1,2,3,4. If I have a lot more data I'm hoping it would be smart enough to only show what values I have room for, so for examples if the data contains values of 0-50 it would show something like 0,10,20,30,40,50 or possibly 0,15,30,45, etc.

like image 561
Fozefy Avatar asked Feb 26 '15 21:02

Fozefy


People also ask

How do you display only integers in python?

Make a new list for only integers tick on X-axis. Use math. floor() and math. ceil() to remove the decimals and include only integers in the list.


2 Answers

Allright, now I see your problem. THe problem is that the YAxis (YLabels) determines the number of digits and thus the steps between each label automatically.

Unfortunately it's currently not possible to customize how the axis computes itself or set custom labels.

I am considering to add such a feature in the future, where the user can self define which values are drawn as the axis labels.

Just a hint:

The mEntries array that holds all the axis labels is public. So in general, you could modify it after it has been created. However, I am absolutely not sure how the axis will behave if you manually modify it.

But maybe its worth a try :-) https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartLib/src/com/github/mikephil/charting/components/YAxis.java

like image 60
Philipp Jahoda Avatar answered Oct 01 '22 17:10

Philipp Jahoda


After looking around with no solution available, I've decided to look into the javadoc and found out about this method: setGranularity(float). To force the YAxis to always display integers (or any interval you want), you just need to call this:

yAxisLeft.setGranularity(1.0f);
yAxisLeft.setGranularityEnabled(true); // Required to enable granularity

However, if the min and max values of the chart is too close, then the chart will not honor setLabelCount(), unless when is forced (which will make the labels in decimal again), so you need to call this after setting data:

private void calculateMinMax(BarLineChartBase chart, int labelCount) {
    float maxValue = chart.getData().getYMax();
    float minValue = chart.getData().getYMin();

    if ((maxValue - minValue) < labelCount) {
        float diff = labelCount - (maxValue - minValue);
        maxValue = maxValue + diff;
        chart.getAxisLeft().setAxisMaximum(maxValue);
        chart.getAxisLeft().setAxisMinimum(minValue);
    }
}

And that's it!

like image 40
NoImNotNull Avatar answered Oct 01 '22 16:10

NoImNotNull