Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...