我正在尝试绘制条形图,并希望在条形图上设置标签。但是最后一个条目的标签没有显示。以下是我设置图表的方式。机器arraylist包含标签,我将其复制到字符串数组mac []中。
String[] mac = new String[machines.size()];;
for(int i=0;i<machines.size();++i)
{
mac[i] = machines.get(i);
}
BarChart chart1 = (BarChart) findViewById(R.id.chart1);
BarDataSet bds2 = new BarDataSet(barEntries, "Target");
BarDataSet bds1 = new BarDataSet(barEntries1,"Prepared");
bds1.setColor(Color.parseColor("#F44336"));
BarData ddata1 = new BarData(bds1,bds2);
chart1.setScaleEnabled(true);
chart1.setPinchZoom(true);
chart1.setData(ddata1);
XAxis xa1 = chart1.getXAxis();
xa1.setValueFormatter(new MyAxisValueFormatter(mac));
xa1.setCenterAxisLabels(true);
chart1.setTouchEnabled(true);
chart1.setDragEnabled(true);
chart1.setScaleEnabled(true);
float groupSpace = 0.0f;
float barSpace = 0.0f;
chart1.fitScreen();
ddata1.setBarWidth(barWidth);
xa1.setLabelCount(machines.size(),true);
chart1.groupBars( 0.0f,groupSpace, barSpace);
chart1.setFitBars(true); // make the x-axis fit exactly all bars
chart1.setVisibility(View.VISIBLE);
xa1.setGranularity(1f);
xa1.setAxisMinimum(0);
xa1.setGranularityEnabled(true);
xa1.setCenterAxisLabels(true);
chart1.invalidate();
这是我的MyAxisValueFormatter类
public class MyAxisValueFormatter implements IAxisValueFormatter
{
private String [] m;
public MyAxisValueFormatter(String[] arr)
{
this.m = arr;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return m[(int) value];
}
@Override
public int getDecimalDigits() {
return 0;
}
}
现在,我非常确定机器arraylist的大小为12,但是图形上的标签数量仅为11,即,最后的标签未显示。我尝试在日志中的MyAxisValueFormatter类的getFormattedValue函数中显示 value 变量。知道了根据文档,“值”表示标签在轴(x或y)上的位置。
日志为:
07-24 21:21:28.299 26339-26339/com.example.quickstart I/flag: -0.5
07-24 21:21:28.300 26339-26339/com.example.quickstart I/flag: 0.5909091
1.6818182
2.7727275
3.8636365
4.9545455
6.0454545
7.1363635
8.227273
9.318182
10.409091
11.5
07-24 21:21:28.304 26339-26339/com.example.quickstart I/flag: 0.0
1.0681819
2.1363637
3.2045455
4.2727275
5.3409095
07-24 21:21:28.305 26339-26339/com.example.quickstart I/flag: 6.4090915
7.4772735
8.545455
9.613637
10.681819
11.750001
07-24 21:21:28.310 26339-26339/com.example.quickstart I/flag: 0.0
1.0681819
2.1363637
3.2045455
4.2727275
5.3409095
6.4090915
7.4772735
8.545455
9.613637
07-24 21:21:28.311 26339-26339/com.example.quickstart I/flag: 10.681819
11.750001
07-24 21:21:28.313 26339-26339/com.example.quickstart I/flag: 0.0
1.0681819
07-24 21:21:28.314 26339-26339/com.example.quickstart I/flag: 2.1363637
07-24 21:21:28.315 26339-26339/com.example.quickstart I/flag: 3.2045455
4.2727275
5.3409095
07-24 21:21:28.316 26339-26339/com.example.quickstart I/flag: 6.4090915
7.4772735
07-24 21:21:28.317 26339-26339/com.example.quickstart I/flag: 8.545455
9.613637
07-24 21:21:28.318 26339-26339/com.example.quickstart I/flag: 10.681819
07-24 21:21:28.344 26339-26339/com.example.quickstart I/flag: 0.0
07-24 21:21:28.345 26339-26339/com.example.quickstart I/flag: 1.0681819
2.1363637
3.2045455
4.2727275
5.3409095
6.4090915
7.4772735
8.545455
9.613637
10.681819
11.750001
07-24 21:21:28.348 26339-26339/com.example.quickstart I/flag: 0.0
1.0681819
07-24 21:21:28.349 26339-26339/com.example.quickstart I/flag: 2.1363637
3.2045455
4.2727275
07-24 21:21:28.350 26339-26339/com.example.quickstart I/flag: 5.3409095
6.4090915
7.4772735
07-24 21:21:28.351 26339-26339/com.example.quickstart I/flag: 8.545455
9.613637
10.681819
现在我不明白为什么 11.750001 不能打印在最后一行。以及为什么x的值会从0到1.0681819而不是1一样增加0.6左右,我可能犯了一个小错误,这使我在轴上定位的标签变得混乱。我想要的是显示所有标签。请调查一下。任何帮助表示赞赏。
这是我当前正在运行的应用程序的一个变形。如您所见,只有11个标签,而应该有12个标签。
编辑:
barentries值是FTP12之后的值:
07-24 21:56:50.612 1755-1982/com.example.quickstart I/FTP12: 0000002500
07-24 21:56:50.615 1755-1982/com.example.quickstart I/FTP12: 0000007350
07-24 21:56:50.619 1755-1982/com.example.quickstart I/FTP12: 0000004500
07-24 21:56:50.622 1755-1982/com.example.quickstart I/FTP12: 0000007000
07-24 21:56:50.624 1755-1982/com.example.quickstart I/FTP12: 0000007000
07-24 21:56:50.626 1755-1982/com.example.quickstart I/FTP12: 0000003500
07-24 21:56:50.628 1755-1982/com.example.quickstart I/FTP12: 0000009000
07-24 21:56:50.631 1755-1982/com.example.quickstart I/FTP12: 0000007151
07-24 21:56:50.633 1755-1982/com.example.quickstart I/FTP12: 0000004378
07-24 21:56:50.635 1755-1982/com.example.quickstart I/FTP12: 0000006810
07-24 21:56:50.637 1755-1982/com.example.quickstart I/FTP12: 0000003405
07-24 21:56:50.639 1755-1982/com.example.quickstart I/FTP12: 0000008756
barentries1的值是barentries1之后的值:
07-24 21:59:20.008 3331-3435/com.example.quickstart I/barentries1: 000000000001504
07-24 21:59:20.013 3331-3435/com.example.quickstart I/barentries1: 000000000002842
07-24 21:59:20.017 3331-3435/com.example.quickstart I/barentries1: 000000000001796
07-24 21:59:20.020 3331-3435/com.example.quickstart I/barentries1: 000000000003658
07-24 21:59:20.023 3331-3435/com.example.quickstart I/barentries1: 000000000001965
07-24 21:59:20.025 3331-3435/com.example.quickstart I/barentries1: 000000000000796
07-24 21:59:20.027 3331-3435/com.example.quickstart I/barentries1: 000000000007631
07-24 21:59:20.029 3331-3435/com.example.quickstart I/barentries1: 000000000006323
07-24 21:59:20.032 3331-3435/com.example.quickstart I/barentries1: 000000000000496
07-24 21:59:20.033 3331-3435/com.example.quickstart I/barentries1: 000000000003531
07-24 21:59:20.035 3331-3435/com.example.quickstart I/barentries1: 000000000002313
07-24 21:59:20.037 3331-3435/com.example.quickstart I/barentries1: 000000000000954
07-24 21:59:20.040 3331-3435/com.example.quickstart I/barentries1: 000000000005071
机器值是机器之后的值:
07-24 22:00:14.244 3639-3709/com.example.quickstart I/machines: TEMP-2
07-24 22:00:14.249 3639-3709/com.example.quickstart I/machines: TEMP-3
07-24 22:00:14.254 3639-3709/com.example.quickstart I/machines: TEMP-1
07-24 22:00:14.257 3639-3709/com.example.quickstart I/machines: BENTLER-1
07-24 22:00:14.263 3639-3709/com.example.quickstart I/machines: BENTLER-2
07-24 22:00:14.265 3639-3709/com.example.quickstart I/machines: DEMAN
07-24 22:00:14.267 3639-3709/com.example.quickstart I/machines: ANNE-LOSE
07-24 22:00:14.269 3639-3709/com.example.quickstart I/machines: TEMP-3
07-24 22:00:14.272 3639-3709/com.example.quickstart I/machines: ANNEALD-1
07-24 22:00:14.274 3639-3709/com.example.quickstart I/machines: TEMP-1
07-24 22:00:14.276 3639-3709/com.example.quickstart I/machines: BENTLER-1
07-24 22:00:14.278 3639-3709/com.example.quickstart I/machines: DEMAN
07-24 22:00:14.280 3639-3709/com.example.quickstart I/machines: ANNE-LOSE