How to set the x-axis label with MPAndroidChart

后端 未结 4 1214

I use MPAndroidChart for my app like this:

but I can not add tag like that

4条回答
  •  别那么骄傲
    2020-12-17 17:06

    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 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:

提交回复
热议问题