有没有可用于简单地为分组条形图创建数据集的格式?在网上的文档和示例中找不到任何参考。 我的输出将产生3套Barchart。 第一张图将有4组。第二和第三将有3组。 每组有5套。 (我无法在此处附加照片。不知道为什么它会不断失败) 这是其他网站的链接 https://ibb.co/mTNs1K
目前我是这样做的。
我的JSON
{
"data1": {
"val1": "2, 2, 2, 4",
"val2": "5, 1, 1, 3",
"val3": "3, 2, 3, 2",
"val4": "1, 2, 3, 4",
"val5": "7, 1, 1, 1"
},
"data2": {
"val1": "2, 6, 2",
"val2": "5, 4, 1",
"val3": "3, 4, 3",
"val4": "1, 6, 3",
"val5": "7, 2, 1"
},
"data3": {
"val1": "6, 2, 2",
"val2": "8, 1, 1",
"val3": "5, 2, 3",
"val4": "5, 2, 3",
"val5": "8, 1, 1"
}
}
我的Java
float[] val1 = StrToFloat(val_a1);
float[] val2 = StrToFloat(val_a2);
float[] val3 = StrToFloat(val_a3);
float[] val4 = StrToFloat(val_a4);
float[] val5 = StrToFloat(val_a5);
ArrayList<BarEntry> barOne = new ArrayList<>();
ArrayList<BarEntry> barTwo = new ArrayList<>();
ArrayList<BarEntry> barThree = new ArrayList<>();
ArrayList<BarEntry> barFour = new ArrayList<>();
ArrayList<BarEntry> barFive = new ArrayList<>();
for (int i = 0; i < val1.length; i++) {
barOne.add(new BarEntry(i, val1[i]));
barTwo.add(new BarEntry(i, val2[i]));
barThree.add(new BarEntry(i, val3[i]));
barFour.add(new BarEntry(i, val4[i]));
barFive.add(new BarEntry(i, val5[i]));
}
BarDataSet set1 = new BarDataSet(barOne, "Worst");
set1.setColor(Color.parseColor("#5DADE2"));
BarDataSet set2 = new BarDataSet(barTwo, "Bad");
set2.setColor(Color.parseColor("#EC7063"));
BarDataSet set3 = new BarDataSet(barThree, "Normal");
set3.setColor(Color.parseColor("#F9E79F"));
BarDataSet set4 = new BarDataSet(barFour, "Good");
set4.setColor(Color.parseColor("#48C9B0"));
BarDataSet set5 = new BarDataSet(barFive, "Very Good");
set5.setColor(Color.parseColor("#AF7AC5"));
set1.setHighlightEnabled(false);
set2.setHighlightEnabled(false);
set3.setHighlightEnabled(false);
set4.setHighlightEnabled(false);
set5.setHighlightEnabled(false);
set1.setDrawValues(false);
set2.setDrawValues(false);
set3.setDrawValues(false);
set4.setDrawValues(false);
set5.setDrawValues(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
dataSets.add(set4);
dataSets.add(set5);
BarData data = new BarData(dataSets);
float groupSpace = 0.4f;
float barSpace = 0.02f;
float barWidth = 0.1f;
// (barSpace + barWidth) * 2 + groupSpace = 1
data.setBarWidth(barWidth);
mChart.setDrawBarShadow(false);
mChart.getDescription().setEnabled(false);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
// empty labels so that the names are spread evenly
String[] labels = {"", "Objective", "Content", "Presentation", "Assistant", ""};
IAxisValueFormatter xAxisFormatter = new LabelFormatter(mChart, labels);
XAxis xAxis = mChart.getXAxis();
xAxis.setCenterAxisLabels(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f); // only intervals of 1 day
xAxis.setTextColor(Color.BLACK);
xAxis.setTextSize(12);
xAxis.setAxisLineColor(Color.WHITE);
xAxis.setAxisMinimum(1f);
xAxis.setValueFormatter(xAxisFormatter);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTextColor(Color.BLACK);
leftAxis.setTextSize(10);
leftAxis.setAxisLineColor(Color.WHITE);
leftAxis.setDrawGridLines(false);
leftAxis.setGranularity(1);
//leftAxis.setLabelCount(8, true);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
mChart.getAxisRight().setEnabled(false);
mChart.getLegend().setEnabled(true);
// so that the entire chart is shown when scrolled from right to left
xAxis.setAxisMaximum(labels.length - 1.1f);
mChart.setData(data);
mChart.setScaleEnabled(false);
mChart.setVisibleXRangeMaximum(4f);
mChart.groupBars(1f, groupSpace, barSpace);
mChart.invalidate();
legend = mChart.getLegend();
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setWordWrapEnabled(true);
我的功能
private float[] StrToFloat (String val) {
String[] parts = val.split(",");
float[] fval = new float[parts.length];
for (int i = 0; i < parts.length; ++i) {
float number = Float.parseFloat(parts[i]);
float rounded = (int) Math.round(number * 1000) / 1000f;
fval[i] = rounded;
}
return fval;
}
这仅适用于data1。我必须对data2和data3重复此方法。 我认为有一种更好的方法来处理此类数据。