我正在尝试通过在Android应用程序中使用MPAndroidCharts库为动态数据集实现多条形图。我将如何从右向左滚动图表。另外,我很难为组的每个栏赋予不同的颜色。这是我的代码:
BarDataSet[] mBardata;
mBardata=new BarDataSet [jsonArray.length()];
//get data from json response
for(int i=0;i<jsonArray.length();i++) {
JSONObject obj = jsonArray.getJSONObject(i);
final AWWDepartmentStatsModel2 model = new AWWDepartmentStatsModel2(
obj.getString("departmentId"),
BigDecimal.valueOf(obj.getDouble("expenditure")).floatValue(),
BigDecimal.valueOf(obj.getDouble("revenue")).floatValue(),
BigDecimal.valueOf(obj.getDouble("profit")).floatValue(),
BigDecimal.valueOf(obj.getDouble("loss")).floatValue(),
obj.getString("month"),
obj.getString("created_at"),
obj.getString("updated_at")
);
ArrayList<BarEntry> mbarEntry=new ArrayList<>();
mbarEntry.add(new BarEntry(0,model.getRevenue()));
mbarEntry.add(new BarEntry(1,model.getExpenditure()));
mbarEntry.add(new BarEntry(2,model.getProfit()));
mBardata[i]=new BarDataSet(mbarEntry,"");
mBardata[i].setColors(new int[]{R.color.top,R.color.textcolor_black,R.color.blue},YearlyStatsByDepartment.this);
mBardata[i].setDrawValues(true);
mBardata[i].notifyDataSetChanged();
}
private void setData(){
BarData data=new BarData(mBardata);
data.setBarWidth(0.3f);
mBarChart.setData(data);
mBarChart.getData().notifyDataChanged();
mBarChart.notifyDataSetChanged();
Legend l=mBarChart.getLegend();
l.setEnabled(false);
YAxis yAxisRight = mBarChart.getAxisRight();
yAxisRight.setEnabled(false);
mBarChart.groupBars(0,groupSpace,barSpace);
mBarChart.invalidate();
}