Reputation: 301
I'm using MPAndroidChart(https://github.com/PhilJay/MPAndroidChart).
I made one barchart, but xAxis-2nd value is not showing.
x,y : 1 week, 3회
x,y : 2 week, 0회
x,y : 3 week, 0회
x,y : 4 week, 0회
x,y : 5 week, 0회
Those are my entry values. But like this, 2nd label is not showing in my phone.
I attached source and result screenshot.
ArrayList<BarEntry> entries = new ArrayList<>();
ArrayList<String> labels = new ArrayList<String>();
String[] labels2 = new String[ExRegList.length()];
try {
exerciseWholeCnt = ExRegList.length();
for (int i = 0; i < ExRegList.length(); i++) {
JSONObject obj = ExRegList.getJSONObject(i);
String week = obj.getString("WEEK");
int exCnt = obj.getInt("RESULT_COUNT");
labels.add(week + "주");
labels2[i] = week+"주";
entries.add(new BarEntry(i,exCnt));
}
} catch (Exception e) {
e.printStackTrace();
}
XAxis xAxis = barChart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels2));
XAxis bottomAxis = barChart.getXAxis();
bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
bottomAxis.setDrawLabels(true);
bottomAxis.setDrawGridLines(false);
bottomAxis.setDrawAxisLine(true);
YAxis left = barChart.getAxisLeft();
left.setAxisMinimum(0);
left.setAxisMaximum(7);
BarDataSet barDataSet = new BarDataSet(entries, "01");
BarData barData = new BarData(barDataSet);
YAxis rightYAxis = barChart.getAxisRight();
rightYAxis.setEnabled(false);
barChart.setData(barData);
barChart.setDescription(null);
barChart.setPinchZoom(false);
barChart.setScaleEnabled(false);
barChart.setDrawBarShadow(false);
barChart.setDrawGridBackground(false);
barChart.animateY(2000);
barChart.getLegend().setEnabled(false);
barChart.getData().setValueTextSize(10);
barChart.getBarData().setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
if (value != 0) {
DecimalFormat format = new DecimalFormat("#");
return format.format(value) + "회";
}
return "";
}
});
// barChart.getXAxis().setValueFormatter(new MyValueFormatter());
barChart.invalidate();
}
and the result is.
Upvotes: 3
Views: 7327
Reputation: 13057
Finally, found the problem after looking through source code of the library. You should call setLabelCount. After this line:
XAxis bottomAxis = barChart.getXAxis();
set count to labels of X axis:
bottomAxis.setLabelCount(entries.size());
And it will work.
Explaination: Basically, default label count is 6 (if you will not specify) and it doesn't count correctly appropriate labels. In your case you have 5 items, and formatter gets values 0 , 0.8, 1.6, 2.4, 3.2 and 4.0 - 6 values. And this method of the library gives "" value for second value:
public String getFormattedValue(float value, AxisBase axis) {
int index = Math.round(value);
if (index < 0 || index >= mValueCount || index != (int)value)
return "";
return mValues[index];
}
This is library's source code, that gives you label. And in your case it gives "" when rendering 2nd value.
Upvotes: 15
Reputation: 443
From your code I can guess that there is runtime error in the code/data
try {
exerciseWholeCnt = ExRegList.length();
for (int i = 0; i < ExRegList.length(); i++) {
JSONObject obj = ExRegList.getJSONObject(i);
String week = obj.getString("WEEK");
int exCnt = obj.getInt("RESULT_COUNT");
labels.add(week + "주");
labels2[i] = week+"주";
entries.add(new BarEntry(i,exCnt));
}
Can you check that loop runs perfectly at the second time and nothing in the log
} catch (Exception e) {
**e.printStackTrace();**
}
Upvotes: 1