6
votes

My XAxis values are not aligning. What setting should I modify?

x axis not aligned

Below is what I have in my code so far and .setLabelsToSkip won't work because I'm using version 3.0.1.

XAxis xAxis = mChart.getXAxis();
    xAxis.setTypeface(mTfLight);
    xAxis.setGranularity(1f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setDrawLabels(true);
    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTextColor(Color.WHITE);
    xAxis.setTextSize(15);

Here is my LabelFormatter class:

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

public class LabelFormatter implements IAxisValueFormatter {
private final String[] mLabels;

public LabelFormatter(String[] labels) {
    mLabels = labels;
}

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

Here is my main activity setdata method:

    private void setData() {
    ArrayList<BarEntry> entries = new ArrayList<>();
    String labels[]=new String[5];
    for (Cars c : values) {

       if (c.getX()==1){
            labels[0] = "Engine 1";
        }
        if (c.getX()==2){
            labels[1] = "Engine 2";
        }
        if (c.getX()==3){
            labels[2] = "Engine 3";
        }
        if (c.getX()==4){
            labels[3] = "Engine 4";
        }
        if (c.getX()==5){
            labels[4] = "Engine 5";
        }

        entries.add(new BarEntry((float) c.getX(), ((float) c.getY()),labels));

    }
    BarDataSet set1;

    if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
        set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
        set1.setValues(entries);

        mChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
       //set1.setValueFormatter(new LabelFormatter(labels));
       //mChart.getXAxis().setValueFormatter(new BarValueFormatter(set1));
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();

    } else {
        set1 = new BarDataSet(entries, "Engine Types");

        ArrayList<Integer> colors = new ArrayList<Integer>();

        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());

        set1.setColors(colors);


        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        mChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
        //mChart.getXAxis().setValueFormatter(new LabelFormatter(labels));
        //mChart.getXAxis().setValueFormatter(new BarValueFormatter(set1));
        BarData data = new BarData(dataSets);
        data.setValueTextSize(50f);
        data.setValueTextColor(Color.WHITE);
        data.setValueTypeface(mTfLight);



        mChart.setData(data);


    }

}

In my main activity I'm using it like this:

 mChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
1
Where is your AxisValueFormatter? Please edit to include what you are using. Maybe IndexValueFormatter? - David Rawson
@David Rawson, I included the class file I'm using for LabelFormatter and how I'm calling it in my main activity. - raptor496
So which one are you using? IndexValueFormatter or LabelFormatter? Your code shows both so it's not clear. Also, dont' use (int) value which will always floor the value. Use int index = Math.round(value); - David Rawson
I don't think I'm actually using that class. - raptor496
Using IndexAxisValueFormatter, which comes with 3.0.1 - raptor496

1 Answers

2
votes

If you want to align xAxis label to the center of each bar, make setCenterAxisLabels property to false

xAxis.setCenterAxisLabels(false);