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));
If you want to align xAxis label to the center of each bar, make setCenterAxisLabels property to false
xAxis.setCenterAxisLabels(false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With