Reputation: 305
Can anyone help me with why my alignment is off for the xAxis. I want the points to be aligned with each of the respective text labels (the days of the week)
Here is the picture of what the chart looks like: GRAPH PICTURE
and the code that produced it
private void setupChart() {
final ArrayList<Entry> points = new ArrayList<>();
//Add the values into the graph
points.add(new Entry(1, 17));
points.add(new Entry(2, 14));
points.add(new Entry(3, 10));
points.add(new Entry(4, 2));
points.add(new Entry(5, 20));
//Format the dataset to look nice
LineDataSet lineDataSet = new LineDataSet(points, "Experiments");
lineDataSet.setColor(Color.MAGENTA);
lineDataSet.setCircleColor(Color.GREEN);
lineDataSet.setCircleRadius(5);
lineDataSet.setValueTextSize(10);
lineDataSet.setLineWidth(3);
//Add the LineDataSet to the DataSets
final ArrayList<ILineDataSet> dataSets = new ArrayList<>();
dataSets.add(lineDataSet);
final LineData lineData = new LineData(dataSets);
chart.setData(lineData);
//Format the chart nicely
chart.setDrawGridBackground(false);
chart.setDrawBorders(false);
chart.getAxisRight().setEnabled(false);
chart.getLegend().setEnabled(false);
chart.getDescription().setEnabled(false);
chart.setExtraBottomOffset(10);
chart.setPinchZoom(false);
//Get the XAxis of the grid
XAxis xaxis = chart.getXAxis();
//Format the xAxis
final String[] dateStrings = new String[]{"Mon", "Tue", "Wed", "THU", "Fri"};
xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xaxis.setLabelCount(5);
xaxis.setLabelRotationAngle(-8);
xaxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return dateStrings[(int) value];
}
});
}
I am using com.github.PhilJay:MPAndroidChart:v3.0.2 in my dependencies
Upvotes: 1
Views: 2181
Reputation: 198
Use below code.
xAxis.setLabelCount(5, true);
And, you can find implementation code on Library file.
public void setLabelCount(int count, boolean force) {
setLabelCount(count);
mForceLabels = force;
}
The "force" parameter determines whether to recalculate the label position.
Upvotes: 1
Reputation: 3189
Remove this line :
xaxis.setLabelCount(5);
Add below lines:
XAxis xAxis = chart.getXAxis();
xAxis.setCenterAxisLabels(true);
Upvotes: 0