5

chart demo

I want the x-axis labelling to be as shown in the above picture. I have tried using the AxisValueFormatter interface but what it does is repeat the label 'jh ow' 3 times from 0 to 1, while missing out some of the labels shown here.

enter image description here

This is the output I am getting. While the shape of the graph is correct, the labels are not.

I am sharing the graph data so that anyone interested can try and make the graph as shown. Any help would be great, thank you.

Using MPAndroid is not important. Any help from any other library is fine too.

The data are:

"lables" : ["jh ow", "n ih l", "b iy", "hh iy r", "s uw n"]


"values":[[{"x":"0.00","y":"0.00"},{"x":"0.99","y":"0.00"}],
   [{"x":"0.99","y":"1.00"},{"x":"1.19","y":"0.50"}]
,  [{"x":"1.19","y":"0.50"},{"x":"1.37","y":"0.50"}]
,   [{"x":"1.37","y":"0.50"},{"x":"1.59","y":"0.50"}]
,   [{"x":"1.59","y":"0.50"},{"x":"2.12","y":"1.00"}]]
Aryan Raj
  • 134
  • 10

2 Answers2

0

You just make a simple list of string like this in activity page;

final ArrayList<String> xVals= new ArrayList<>();
    xVals.add("jh ow");
    xVals.add("n ih l");
    xVals.add("b iy");
    xVals.add("hh iy r");
    xVals.add("s uw n");

and Then you do this;

xAxis.setValueFormatter(new MyXAxisDateArrayFormatter(xVals));

your MyXAxisDateArrayFormatter.java

public class MyXAxisDateArrayFormatter implements IAxisValueFormatter {

    private String[] mValues;

    public MyXAxisDateArrayFormatter(String[] values) {
        this.mValues = values;
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        // "value" represents the position of the label on the axis (x or y)
        return mValues[(int) value];
    }

}

It worked on me

kksal55
  • 500
  • 7
  • 14
  • You have used xAxis.setValueFormatter(new MyXAxisDateArrayFormatter(xVals)); but have defined MyAxisValueFormatter. Is this intentional or am I missing something? – Aryan Raj Nov 27 '18 at 11:39
  • Still not able to get the correct output. I have added the screenshot in the question edit, take a look. Thanks for your help btw. – Aryan Raj Nov 27 '18 at 12:22
0

Nevermind, I was able to achieve this by using an older version of MPAndroidChart library and it worked like a charm.

Aryan Raj
  • 134
  • 10