Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the x-axis label with MPAndroidChart

I use MPAndroidChart for my app like this:

enter image description here

but I can not add tag like that

like image 383
kemp Avatar asked Oct 09 '16 15:10

kemp


People also ask

Which function is used to set x-axis labels?

Description. xlabel( txt ) labels the x-axis of the current axes or standalone visualization. Reissuing the xlabel command replaces the old label with the new label. xlabel( target , txt ) adds the label to the specified target object.

How do you stagger x-axis labels?

Access the Stagger option in one of the following ways: Ribbon: On the Format tab, in the Labels group, open the Axes drop-down menu, point to the axis that you are working with, and select Stagger Labels. Right-Click Menu: Right-click an axis label on the chart, point to Stagger and select On.

How do you label the x-axis and Y axis?

The proper form for a graph title is "y-axis variable vs. x-axis variable." For example, if you were comparing the the amount of fertilizer to how much a plant grew, the amount of fertilizer would be the independent, or x-axis variable and the growth would be the dependent, or y-axis variable.


2 Answers

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

like image 158
Prabs Avatar answered Oct 02 '22 05:10

Prabs


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.

like image 31
Maher Abuthraa Avatar answered Oct 02 '22 04:10

Maher Abuthraa