java - 将字符串值设置为 MPAndroidChart 条形图的 xaxis 标签
问题描述
我正在尝试将条形图的标签从浮点数格式化为字符串,但它只显示一个标签,如图所示, 没有格式化程序,这就是我在 这里拥有的格式化程序内部类代码
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,而其余的正在评估为假,即使它应该是真的你的格式化代码应该是
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);
推荐阅读
- javascript - 带有推送功能的打字稿数组 foreach (Angular)
- hyperledger-fabric - Hypereldger Fabric 管理员证书和 tls 证书已过期
- html - 如何添加提交按钮,并指定表单应该转到“/action_page.php”?
- ruby - Puma 如何允许并行完成 IO 等待以提高 MRI 的吞吐量?
- kerberos - kinit 要求输入密码,尽管 pki 配置
- sql - Postgresql - 将字符串与空值进行比较
- javascript - 错误:尝试使用 nodemon 运行时找不到模块
- html - 在两个固定元素之间添加间距
- javascript - 如何在 datetimepicker 上添加多项选择功能
- asp.net-mvc - 从布局访问会话数据