我想从数据库获取 xAxis,这是不同查询的结果,以便我的图表可以动态显示数据。
对于静态 mpchart 我对此没有问题。我在onCreate中调用了这个函数。
private void drawChart(){
getChartData();
BarDataSet barDataSet1 = new BarDataSet(sehat(), "Sehat");
BarDataSet barDataSet2 = new BarDataSet(sakit(), "Sakit");
barDataSet1.setColor(Color.BLUE);
barDataSet2.setColor(Color.RED);
chart.getXAxis().setDrawGridLines(false);
chart.getAxisLeft().setDrawGridLines(false);
chart.getDescription().setEnabled(false);
BarData barData = new BarData(barDataSet1, barDataSet2);
chart.setData(barData);
String[] area = new String[]{"UT PBRK", "POOL SLI", "UT TJR", "UT TRD", "UTR BATAKAN", "UT PBLP", "UT PBPN"};
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(area));
xAxis.setCenterAxisLabels(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setGranularity(1);
xAxis.setGranularityEnabled(true);
chart.setDragEnabled(true);
chart.setVisibleXRangeMaximum(3);
float barSpace = 0.08f;
float groupSpace = 0.54f;
barData.setBarWidth(0.15f);
chart.getXAxis().setAxisMinimum(0);
chart.getXAxis().setAxisMaximum(0+chart.getBarData().getGroupWidth(groupSpace, barSpace)*7);
chart.getAxisLeft().setAxisMinimum(0);
chart.groupBars(0, groupSpace, barSpace);
chart.invalidate();
}
private ArrayList<BarEntry> sakit() {
ArrayList<BarEntry> dataEntries = new ArrayList<>();
dataEntries.add(new BarEntry(1, 2));
dataEntries.add(new BarEntry(2, 1));
dataEntries.add(new BarEntry(3, 1));
dataEntries.add(new BarEntry(4, 4));
dataEntries.add(new BarEntry(5, 3));
dataEntries.add(new BarEntry(6, 7));
dataEntries.add(new BarEntry(7, 7));
return dataEntries;
}
我要使这条线变得动态。
String[] area = new String[]{"UT PBRK", "POOL SLI", "UT TJR", "UT TRD", "UTR BATAKAN", "UT PBLP", "UT PBPN"};
到目前为止,我已经尝试了一些我在谷歌中找到的实验。这个更接近。Log.d显示结果数据。但是当我使用 String[] 时,它不会显示在我的图表中。
繁花不似锦