1

I want to compare grades in my Android app using MPandroidchart.

I made this same graph in my website. But don't know if it's possible in Android too.

This is what I want to do:

enter image description here

final ArrayList<String> labelsSub = new ArrayList<>();

labelsSub.add("CSE215");
labelsSub.add("CSE215");
labelsSub.add("MAT120");
labelsSub.add("MAT120");

List<BarEntry> entriesSub = new ArrayList<>();
entriesSub.add(new BarEntry(0f, "A"));
entriesSub.add(new BarEntry(1f, "A-"));
entriesSub.add(new BarEntry(2f, "A"));
entriesSub.add(new BarEntry(3f, "B"));
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Tahmid
  • 315
  • 3
  • 12
  • Check my same Answer on setting string value in xAxis [How to set String value i](https://stackoverflow.com/a/56945823/9725562) – Anshul1507 Jul 09 '19 at 05:26

3 Answers3

1

You can easily do this by using the following code:

XAxis xAxis = lineChart.getXAxis();
xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            switch ((int)value){
                //write your logic here
                case 0:
                    return "CHE1";
                case 1:
                    return "CHE2";
                case 2:
                    return "CHE3";
                case 3:
                    return "CHE4;
                default:
                    return "CHE5";
            }
        }
    });
pratiked
  • 1,712
  • 1
  • 11
  • 18
0

Here's a solution I found, hope it helps other.

If you want to set custom text in graph values, then use this.

public class GradeValueChart implements IValueFormatter {

    private DecimalFormat mFormat;

    public GradeValueChart() {
        mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {


        String val = Utils.getGpaGrade(value);


        return val + " "; // e.g. append any text
    }
}

Then set formatter in Dataset.

    setSub.setValueFormatter(new GradeValueChart());
Tahmid
  • 315
  • 3
  • 12
0

Use array of string for formatting xvalue on x-axis.

ArrayList<String> chnlList=new ArrayList<>();
xAxis.setValueFormatter(new IAxisValueFormatter() {
    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        return chnlList.get((int) value);
    }
});
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Rahul Singh
  • 320
  • 4
  • 7