Chrystian
Chrystian

Reputation: 1047

Scale X Axis MPAndroidCharts

I have a simple line chart. I do not know how to make the x axis values to be properly scaled like the y axis values. Regardless of the values I input into the graph, the distance between the x values is always equidistant.

Edited by adding the entire line chart code.

    setData();

    Legend l = mChart.getLegend();


    l.setForm(Legend.LegendForm.LINE);

    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.removeAllLimitLines(); 

    leftAxis.enableGridDashedLine(10f, 10f, 0f);
    leftAxis.setDrawZeroLine(false);


    leftAxis.setDrawLimitLinesBehindData(true);

    mChart.getAxisRight().setEnabled(false);

    mChart.animateX(2000, Easing.EasingOption.EaseInOutQuart);


    mChart.invalidate();

    return view;
}

private ArrayList<String> setXAxisValues(){
    ArrayList<String> xVals = new ArrayList<String>();
    xVals.add(String.valueOf(x1));

    xVals.add(String.valueOf(x2));
    xVals.add(String.valueOf(x3));
    return xVals;
}

private ArrayList<Entry> setYAxisValues(){
    ArrayList<Entry> yVals = new ArrayList<Entry>();
    yVals.add(new Entry(0, y1));
    yVals.add(new Entry(1, y2));
    yVals.add(new Entry(2, y3));

    return yVals;
}

private void setData() {
    ArrayList<String> xVals = setXAxisValues();

    ArrayList<Entry> yVals = setYAxisValues();

    LineDataSet set1;

    set1 = new LineDataSet(yVals, yTitle);

    set1.setFillAlpha(110);

    ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
    dataSets.add(set1); // add the datasets
    LineData data = new LineData(dataSets);


    String[] values = new String[] {String.valueOf(x1), String.valueOf(x2), String.valueOf(x3)};
    XAxis xAxis = mChart.getXAxis();
   xAxis.setGranularity(1f);
    mChart.setScaleEnabled(true);

    xAxis.setValueFormatter(new MyXAxisValueFormatted(values));

    mChart.getLegend().setTextColor(Color.WHITE);
    mChart.getXAxis().setTextColor(Color.WHITE);
    mChart.getAxisLeft().setTextColor(Color.WHITE);
    mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    mChart.setData(data);

}

This is the XAxisValueFormatted class that may be the source of the problem:

public class MyXAxisValueFormatted implements  IAxisValueFormatter{
    private String[] mValues;
    public  MyXAxisValueFormatted(String[] values){
        this.mValues = values;
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        return mValues[(int) value];
    }
}

enter image description here

Upvotes: 1

Views: 417

Answers (1)

Misha Akopov
Misha Akopov

Reputation: 13027

Here is solution, but first, explanation:

Your x axis values have no idea about "3", "9" and "100", because they are all strings. But it knows 0 1 and 2 (x values) from here:

 yVals.add(new Entry(0, y1));
 yVals.add(new Entry(1, y2));
 yVals.add(new Entry(2, y3));

And those values have the same interval, that's why your formatter gives same separations(spaces) between strings 3, 9 and 100.

Solution is very simple, forget(delete class) about value IAxisValueFormatter, just initialize your Entry-s like this:

    yVals.add(new Entry(3, 2));
    yVals.add(new Entry(9, 1));
    yVals.add(new Entry(100f, -90f));

And everything will be done automatically, here what I got from your code and values:

enter image description here

Upvotes: 1

Related Questions