18

It is necessary for my application to have a label on each bar of the bar chart. Is there a way to do this with MPAndroidChart? I could not find a way to do this on the project wiki/javadocs.

If there isn't a way to do this is there another software that will allow me to?

enter image description here

Matt
  • 1,017
  • 2
  • 11
  • 27
  • See below answer [Adding Axis Titles to MpAndroidChart](https://stackoverflow.com/a/56456415/5157605) – omid Jun 05 '19 at 08:11

4 Answers4

55

Updated Answer (MPAndroidChart v3.0.1)

Being such a commonly used feature, v3.0.1 of the library added the IndexAxisValueFormatter class exactly for this purpose, so it's just one line of code now:

mBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));

The ProTip from the original answer below still applies.

Original Answer (MPAndroidChart v3.0.0)

With v3.0.0 of the library there is no direct way of setting labels for the bars, but there's a rather decent workaround that uses the ValueFormatter interface.

Create a new formatter like this:

public class LabelFormatter implements IAxisValueFormatter {
    private final String[] mLabels;

    public LabelFormatter(String[] labels) {
        mLabels = labels;
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        return mLabels[(int) value];
    }
}

Then set this formatter to your x-axis (assuming you've already created a String[] containing the labels):

mBarChart.getXAxis().setValueFormatter(new LabelFormatter(labels));

ProTip: if you want to remove the extra labels appearing when zooming into the bar chart, you can use the granularity feature:

XAxis xAxis = mBarChart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
TR4Android
  • 3,200
  • 16
  • 21
  • can you provide me some decent source code on how to show multiple bar graph with labels on the xAxis – A.s.ALI Oct 17 '16 at 05:53
  • you proTip saved my day. Thanks! @TR4Android – Max Droid Jan 28 '17 at 07:57
  • Thanks for keeping your answer up to date. This question gets asked a lot so we should redirect duplicates here – David Rawson Feb 11 '17 at 03:37
  • My X axis values are appearing at the top of the chart's X axis, I want it to show in the bottom X axis. Any suggestions? – neo Mar 25 '17 at 14:46
  • @neo Try `mBarChart.getXAxis().setPosition()`. For more information check the javadocs [here](https://jitpack.io/com/github/PhilJay/MPAndroidChart/v3.0.2/javadoc/com/github/mikephil/charting/components/XAxis.html#setPosition-com.github.mikephil.charting.components.XAxis.XAxisPosition-). – TR4Android Mar 26 '17 at 02:34
  • @TR4Android would the above code still work for labels that are String values? – neo Mar 27 '17 at 11:11
  • @neo Actually, that code works exclusively with string values. – TR4Android Mar 27 '17 at 23:09
  • 1
    The above code sets the column label at the top of the chart. How would I go about adding an extra label into the center of each bar? – Lee Hounshell Apr 16 '17 at 19:15
  • @TR4Android , can you help me on this?https://stackoverflow.com/questions/47690661/mpandroid-group-bar-chart-is-not-showing – SARATH V Dec 15 '17 at 07:57
  • Thanks, although this does work but now it's skipping one label after each item in the arraylist of string, unless i zoom horizontally. – eC Droid Dec 19 '17 at 12:57
  • I have even tried to reduce the text size of x-axis labels by binding.barChart.getXAxis().setTextSize(8); but the issue still persists. – eC Droid Dec 19 '17 at 13:01
  • @eCDroid Have you tried forcing the labels to all display by setting `barChart.getXAxis().setLabelCount(labels.length, true)`? – TR4Android Dec 19 '17 at 21:59
  • Tried but now the labels messed up and still doesn't show all. – eC Droid Dec 20 '17 at 05:05
  • Okay now barChart.getXAxis().setLabelCount(labels.length, false) with passing false as force, the labels does display. Another problem, the labels are far from the bottom of the bars how to move them closer. Thanks – eC Droid Dec 20 '17 at 05:14
  • 1
    Is there a way to make the labels appear for every single bar? I'm using the 3.0.1 version, but it is only showing labels along the Xaxis for every other bar... I already tried .setLabelCount(labels.length), both with true and false options. – Jeff Padgett Aug 25 '18 at 17:36
10

you can set the column label above by adding this line

xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
Muneerah Rashed
  • 313
  • 3
  • 13
7

For version of implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

you can set labels with below snippet.

final ArrayList<String> xAxisLabel = new ArrayList<>();
    xAxisLabel.add("Sun");
    xAxisLabel.add("Mon");
    xAxisLabel.add("Tue");
    xAxisLabel.add("Wed");
    xAxisLabel.add("Thu");
    xAxisLabel.add("Fri");
    xAxisLabel.add("Sat");


    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);

    ValueFormatter formatter = new ValueFormatter() {


        @Override
        public String getFormattedValue(float value) {
            return xAxisLabel.get((int) value);
        }
    };

    xAxis.setGranularity(1f); // minimum axis-step (interval) is 1
    xAxis.setValueFormatter(formatter);
4

You can use IndexAxisValueFormatter to make things easier.

final String[] labels = new String[] {"Dummy", "Jan", "Feb", "March", "April", "May",
    "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
KikiYu
  • 3,087
  • 1
  • 13
  • 14