将字符串值设置为 MPAndroidChart 条形图的 xaxis 标签
Setting string values as xaxis labels of an MPAndroidChart bar graph
我正在尝试将条形图的标签从浮点数格式化为字符串,但它只显示一个标签,如此处所示
没有格式化程序,这就是我所拥有的
这里是格式化程序内部 class 代码
private static class MyFormatter extends ValueFormatter {
public String getAxisLabel(float value, AxisBase axis) {
if (value == 1) {
return "JAN"; //make it a string and return
} else if (value == 2) {
return "FEB"; //make it a string and return
} else if (value == 3) {
return "MAR"; //make it a string and return
} else if (value == 4) {
return "APR"; //make it a string and return
} else if (value == 5) {
return "MAY"; //make it a string and return
} else if (value == 6) {
return "JUN"; //make it a string and return
} else if (value == 7) {
return "JUL"; //make it a string and return
} else if (value == 8) {
return "AUG"; //make it a string and return
} else if (value == 9) {
return "SEP"; //make it a string and return
} else if (value == 10) {
return "OCT"; //make it a string and return
} else if (value == 11) {
return "NOV"; //make it a string and return
} else if (value == 12) {
return "DEC"; //make it a string and return
} else {
return ""; // return empty for other values where you don't want to print anything on the X Axis
}
}
}
这是我将值添加到数据集并最终格式化标签的方式
if (response.body() != null) {
List<BarEntry> barEntries = new ArrayList<>();
for (MonthlySales monthlySales : response.body()) {
barEntries.add(new BarEntry(monthlySales.getMonth(), monthlySales.getTotal()));
}
BarDataSet dataSet = new BarDataSet(barEntries, "Monthly Sales");
dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData data = new BarData(dataSet);
//data.setBarWidth(10f);
chart.setVisibility(View.VISIBLE);
chart.animateXY(2000, 2000);
chart.setData(data);
chart.setFitBars(true);
Description description = new Description();
description.setText("Sales per month");
chart.setDescription(description);
chart.invalidate();
XAxis xAxis = chart.getXAxis();
//xAxis.setLabelCount(12, true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//xAxis.setTypeface();
xAxis.setDrawGridLines(false);
//xAxis.setValueFormatter(new MyFormatter());
}
请注意,从数据库返回的月份是 1-12 的整数,但它显示的是五月到八月这四个月的浮点数。
我可能做错了什么,我怎样才能用这个库实现我的目标?
您需要做的是使用 Math.round() 将浮点值转换为整数,这将确保检查准确地评估为真,因为目前它们仅在 7.0 时评估为真,因为等于 7,而其余的计算结果为 false,即使它应该为 true
因此,您的格式代码应该是
private static class MyFormatter extends ValueFormatter {
public String getAxisLabel(float value, AxisBase axis) {
if (Math.round(value) == 1) {
return "JAN"; //make it a string and return
} else if (Math.round(value) == 2) {
return "FEB"; //make it a string and return
} else if (Math.round(value) == 3) {
return "MAR"; //make it a string and return
} else if (Math.round(value) == 4) {
return "APR"; //make it a string and return
} else if (Math.round(value) == 5) {
return "MAY"; //make it a string and return
} else if (Math.round(value) == 6) {
return "JUN"; //make it a string and return
} else if (Math.round(value) == 7) {
return "JUL"; //make it a string and return
} else if (Math.round(value) == 8) {
return "AUG"; //make it a string and return
} else if (Math.round(value) == 9) {
return "SEP"; //make it a string and return
} else if (Math.round(value) == 10) {
return "OCT"; //make it a string and return
} else if (Math.round(value) == 11) {
return "NOV"; //make it a string and return
} else if (Math.round(value) == 12) {
return "DEC"; //make it a string and return
} else {
return ""; // return empty for other values where you don't want to print anything on the X Axis
}
}
}
还请记住将粒度功能添加到您的 x 轴,以便在图表针对大屏幕缩放时仅显示一个标签。
要使用粒度功能,请将其添加到您的图表创建代码中
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
我正在尝试将条形图的标签从浮点数格式化为字符串,但它只显示一个标签,如此处所示
private static class MyFormatter extends ValueFormatter {
public String getAxisLabel(float value, AxisBase axis) {
if (value == 1) {
return "JAN"; //make it a string and return
} else if (value == 2) {
return "FEB"; //make it a string and return
} else if (value == 3) {
return "MAR"; //make it a string and return
} else if (value == 4) {
return "APR"; //make it a string and return
} else if (value == 5) {
return "MAY"; //make it a string and return
} else if (value == 6) {
return "JUN"; //make it a string and return
} else if (value == 7) {
return "JUL"; //make it a string and return
} else if (value == 8) {
return "AUG"; //make it a string and return
} else if (value == 9) {
return "SEP"; //make it a string and return
} else if (value == 10) {
return "OCT"; //make it a string and return
} else if (value == 11) {
return "NOV"; //make it a string and return
} else if (value == 12) {
return "DEC"; //make it a string and return
} else {
return ""; // return empty for other values where you don't want to print anything on the X Axis
}
}
}
这是我将值添加到数据集并最终格式化标签的方式
if (response.body() != null) {
List<BarEntry> barEntries = new ArrayList<>();
for (MonthlySales monthlySales : response.body()) {
barEntries.add(new BarEntry(monthlySales.getMonth(), monthlySales.getTotal()));
}
BarDataSet dataSet = new BarDataSet(barEntries, "Monthly Sales");
dataSet.setColors(ColorTemplate.COLORFUL_COLORS);
BarData data = new BarData(dataSet);
//data.setBarWidth(10f);
chart.setVisibility(View.VISIBLE);
chart.animateXY(2000, 2000);
chart.setData(data);
chart.setFitBars(true);
Description description = new Description();
description.setText("Sales per month");
chart.setDescription(description);
chart.invalidate();
XAxis xAxis = chart.getXAxis();
//xAxis.setLabelCount(12, true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//xAxis.setTypeface();
xAxis.setDrawGridLines(false);
//xAxis.setValueFormatter(new MyFormatter());
}
请注意,从数据库返回的月份是 1-12 的整数,但它显示的是五月到八月这四个月的浮点数。
我可能做错了什么,我怎样才能用这个库实现我的目标?
您需要做的是使用 Math.round() 将浮点值转换为整数,这将确保检查准确地评估为真,因为目前它们仅在 7.0 时评估为真,因为等于 7,而其余的计算结果为 false,即使它应该为 true 因此,您的格式代码应该是
private static class MyFormatter extends ValueFormatter {
public String getAxisLabel(float value, AxisBase axis) {
if (Math.round(value) == 1) {
return "JAN"; //make it a string and return
} else if (Math.round(value) == 2) {
return "FEB"; //make it a string and return
} else if (Math.round(value) == 3) {
return "MAR"; //make it a string and return
} else if (Math.round(value) == 4) {
return "APR"; //make it a string and return
} else if (Math.round(value) == 5) {
return "MAY"; //make it a string and return
} else if (Math.round(value) == 6) {
return "JUN"; //make it a string and return
} else if (Math.round(value) == 7) {
return "JUL"; //make it a string and return
} else if (Math.round(value) == 8) {
return "AUG"; //make it a string and return
} else if (Math.round(value) == 9) {
return "SEP"; //make it a string and return
} else if (Math.round(value) == 10) {
return "OCT"; //make it a string and return
} else if (Math.round(value) == 11) {
return "NOV"; //make it a string and return
} else if (Math.round(value) == 12) {
return "DEC"; //make it a string and return
} else {
return ""; // return empty for other values where you don't want to print anything on the X Axis
}
}
}
还请记住将粒度功能添加到您的 x 轴,以便在图表针对大屏幕缩放时仅显示一个标签。
要使用粒度功能,请将其添加到您的图表创建代码中
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);