我想从数据库获取 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[] 时,它不会显示在我的图表中。
1 回答
繁花不似锦
TA贡献1851条经验 获得超4个赞
仔细看下面的代码
for(Data get : data){
Log.d("testing", get.getArea());
area = new String[]{get.getArea()};
}
区域 = new String[]{get.getArea()}; for 循环中的方法,因此每次都会正确更新区域变量,因此您最终只能在区域变量内获得一个值,因此请在代码中进行如下所述的更改
Int i = 0;
String[] area = new String[data.size()];
for(Data get : data){
Log.d("testing", get.getArea());
area[i] = get.getArea();
i++;
}
我希望它对你有帮助谢谢
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报
0/150
提交
取消