16

I use MPAndroidChart for my app like this:

enter image description here

but I can not add tag like that

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kemp
  • 201
  • 1
  • 2
  • 5
  • What about showing what you have _tried_ so far? – SOFe Oct 09 '16 at 17:22
  • I don't think you can "group" the existing axis labels. I do think that the API has a method to change the labels, though. See http://stackoverflow.com/questions/27061803/how-to-set-values-for-xlabels-and-ylabels-in-mpandroidchart – OneCricketeer Oct 09 '16 at 21:36
  • I had showed the label, but the position of label is wrong,because the getFormattedValue always return 0,4,8,12,16,20; – kemp Oct 10 '16 at 02:44

4 Answers4

30

Do you need your tag instead of those values?

If so, then here it goes the method to do so.

Add your XAxis labels to an ArrayList

    final ArrayList<String> xLabel = new ArrayList<>();
    xLabel.add("9");
    xLabel.add("15");
    xLabel.add("21");
    xLabel.add("27");
    xLabel.add("33");  

    // or use some other logic to save your data in list. For ex. 
    for(i=1; i<50; i+=2)
    { 
       xLabel.add(""+3*i);
    }

then use this label in the setValueFormatter.
Ex:

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return xLabel.get((int)value);
        }
    });

Result:

enter image description here

Prabs
  • 4,923
  • 6
  • 38
  • 59
5

You can override AxisValueFormatter

i.e.:

xAxis.setValueFormatter(new AxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return "YOUR_TEXT"; // here you can map your values or pass it as empty string
            }

            @Override
            public int getDecimalDigits() {
                return 0; //show only integer
            }
        });

You can pick center value of the group to map the group name, others are empty. that would be the easiest way.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • 2
    thank you for your answer .but the return value always is 0,4,8,12,16,20 when i used getFormattedValue(float value, AxisBase axis) and my center value is 3,9,15,21。and i will make my label position is wrong – kemp Oct 10 '16 at 02:35
  • I have a [question](https://stackoverflow.com/q/67379636/6854117) related to it can you please see it? – Moeez May 04 '21 at 20:30
4

I tried this version: implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0':

public class BarChartActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_chart2);

        BarChart chart = (BarChart) findViewById(R.id.barchart);

        int[] numArr = {1, 2, 3, 4, 5, 6};
        List<BarEntry> entries = new ArrayList<BarEntry>();
        for (int num : numArr) {
            entries.add(new BarEntry(num, num));
        }
        BarDataSet dataSet = new BarDataSet(entries, "Numbers");
        BarData data = new BarData(dataSet);

        ValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart);
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setLabelCount(7);
        xAxis.setValueFormatter(xAxisFormatter);

        chart.setData(data);
        chart.invalidate();
    }

    public class DayAxisValueFormatter extends ValueFormatter {
        private final BarLineChartBase<?> chart;
        public DayAxisValueFormatter(BarLineChartBase<?> chart) {
            this.chart = chart;
        }
        @Override
        public String getFormattedValue(float value) {
            return "your text " + value;
        }
    }
}

Here is the output:

double-beep
  • 5,031
  • 17
  • 33
  • 41
Infomaster
  • 793
  • 7
  • 8
0

I find another solution to this problem. try to add these params

float groupSpace = 0.06f;
float barSpace = 0.02f;
mChart.setData(data);
mChart.groupBars(0f, groupSpace, barSpace);
Ghizlane Lotfi
  • 550
  • 1
  • 10
  • 21
  • I have a [question](https://stackoverflow.com/q/67379636/6854117) related to it can you please see it? – Moeez May 04 '21 at 20:31