Java 将字符串值设置为MPAndroidChart条形图的xaxis标签
我试图将条形图的标签从float格式化为string,但它只显示一个标签,如图所示 没有格式化程序,这就是我所拥有的 这里是格式化程序内部类代码Java 将字符串值设置为MPAndroidChart条形图的xaxis标签,java,android,mpandroidchart,Java,Android,Mpandroidchart,我试图将条形图的标签从float格式化为string,但它只显示一个标签,如图所示 没有格式化程序,这就是我所拥有的 这里是格式化程序内部类代码 private static class MyFormatter extends ValueFormatter { public String getAxisLabel(float value, AxisBase axis) { if (value == 1) { return "J
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());
}
if(response.body()!=null){
List barEntries=new ArrayList();
for(MonthlySales MonthlySales:response.body()){
添加(新的BarEntry(monthlySales.getMonth(),monthlySales.getTotal());
}
BarDataSet数据集=新的BarDataSet(barEntries,“月销售额”);
setColors(ColorTemplate.colorial_COLORS);
BarData数据=新的BarData(数据集);
//数据。立根宽度(10f);
chart.setVisibility(View.VISIBLE);
图表.动画制作(2000年,2000年);
图表.设置数据(数据);
图表.设置栏(正确);
Description Description=新的Description();
description.setText(“每月销售额”);
图表.设置说明(说明);
chart.invalidate();
XAxis XAxis=chart.getXAxis();
//xAxis.setLabelCount(12,真);
设置位置(xAxis.XAxisPosition.BOTTOM);
//setTypeface();
xAxis.setDrawGridLines(false);
//setValueFormatter(新的MyFormatter());
}
请注意,从数据库返回的月份是一个整数1-12,但它显示的是5月到8月这四个月的浮动
我可能做错了什么?如何使用此库实现我的目标?您需要做的是使用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);