我在创建LineChart时遇到问题它只是在我需要显示所有行时显示最后一行你可以帮忙吗?这是代码:
Collections.addAll(labels,` column);
dataSets = new ArrayList<ILineDataSet>();
for (monthlysales company : companieslist) {
entries.clear();
for (int j = 0; j < listofcompanies.Total.size(); j++) {
entries.add(new Entry(Float.parseFloat(listofcompanies.Total.get(j)), j));
}
setComp1 = new LineDataSet(entries, company.StoreName);
setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);
setComp1.setColor(Color.BLUE);
dataSets.add(setComp1);
}
LineData data = new LineData(column,dataSets);
linechart.setData(data);
linechart.setDescription("Sales");
linechart.animateXY(5000,5000);
linechart.setPinchZoom(true);
linechart.setDoubleTapToZoomEnabled(true);
linechart.setDragDecelerationEnabled(true);
linechart.notifyDataSetChanged();
linechart.invalidate();
}
谢谢
答案 0 :(得分:1)
这实际上是有道理的。您正在将数据添加到entries
列表,然后将其正确添加到DataSet。问题是,每次添加后都要清除条目列表。您应该为每个数据集使用单独的列表。
替换行:
entries.clear();
与
List<Entry> entries = new ArrayList<>();
答案 1 :(得分:0)
现在通过创建一个Function来解决它,并按如下方式调用它:
**dataSets.add(createLineChart(company,company.StoreName,company.Total));**
data = new LineData(column,dataSets);
linechart.setData(data);
linechart.invalidate();
linechart.setDescription("Sales");
这是功能:
private LineDataSet createLineChart(monthlysales company,String storeName,List<String> listofcompanies){
// LineData data=new LineData();
ArrayList<Entry> entries= new ArrayList<Entry>();
for (int j = 0; j < listofcompanies.size(); j++) {
entries.add(new Entry(Float.parseFloat(listofcompanies.get(j)),j));
linechart.notifyDataSetChanged();
}
Random rd = new Random();
setComp1 = new LineDataSet(entries,storeName);
setComp1.setColor(Color.argb(255,rd.nextInt(256),rd.nextInt(256),rd.nextInt(256)));
// LineData data =new LineData(labels,dataset);
return setComp1;
}
似乎LineDataSet最后一次被调用时只显示了一行。