How to set String value of xAxis in MPAndroidChart?

Megi Fernanda picture Megi Fernanda · Jul 26, 2017 · Viewed 16.1k times · Source

I want to make Line Chart Graph, but i have problem to show value string in xAxis, im used Library Github from MPAndroidChart to LineChart. Please help me how to add String Value and actually to much question i want ask

private void drawLineChartLine(){

        private float[] yDataL = {40, 60, 70, 80};
        private String[] xDataL = {"Week 1", "Week 1" , "Week 3" , "Week 4"};


        ArrayList<Entry> yEntrys = new ArrayList<>();

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

        for(int i = 0; i < yDataL.length; i++){
            yEntrys.add(new Entry(yDataL[i] ,i));
        }

        for(int i = 1; i < xDataL.length; i++){
            xEntrys.add(xDataL[i]);
        }

        //create the data set
        LineDataSet lineDataset = new LineDataSet(yEntrys, "assa");

        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);


        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xEntrys.get((int) value);
            }
        });

        LineData lineData = new LineData(lineDataset);
        lineChart.setData(lineData);
        lineChart.invalidate();
}

i got error

Invalid index 40, size is 6

in Code

xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xEntrys.get((int) value);
            }
        });

Answer

Arjun G picture Arjun G · Feb 19, 2019
public class IndexAxisValueFormatter extends ValueFormatter
{
    private String[] mValues = new String[] {};
private int mValueCount = 0;

/**
 * An empty constructor.
 * Use `setValues` to set the axis labels.
 */
public IndexAxisValueFormatter() {
}

/**
 * Constructor that specifies axis labels.
 *
 * @param values The values string array
 */
public IndexAxisValueFormatter(String[] values) {
    if (values != null)
        setValues(values);
}

/**
 * Constructor that specifies axis labels.
 *
 * @param values The values string array
 */
public IndexAxisValueFormatter(Collection<String> values) {
    if (values != null)
        setValues(values.toArray(new String[values.size()]));
}

@Override
public String getFormattedValue(float value, AxisBase axisBase) {
    int index = Math.round(value);

    if (index < 0 || index >= mValueCount || index != (int)value)
        return "";

    return mValues[index];
}

public String[] getValues()
{
    return mValues;
}

public void setValues(String[] values)
{
    if (values == null)
        values = new String[] {};

    this.mValues = values;
    this.mValueCount = values.length;
}
}

Add the below code in the method where you are trying to display Bar Chart.

final String[] weekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // Your List / array with String Values For X-axis Labels

// Set the value formatter 
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(weekdays));

It Solved! Happy Coding.