简体   繁体   English

MPAndroidChart LineChart仅显示在LineData上

[英]MPAndroidChart LineChart Showing just on LineData

I am having problem creating LineChart it's just showing the last Line when I need to show all Lines can you please help? 我在创建LineChart时遇到问题,当我需要显示所有行时,它仅显示最后一行。请您能帮忙吗? here's the code: 这是代码:

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();
}

Thank you 谢谢

This actually makes sense. 这实际上是有道理的。 You are adding data to the entries list, and then add it to the DataSet correctly. 您正在将数据添加到entries列表,然后将其正确添加到DataSet。 The problem is, you are clearing the entries list every time after you add it. 问题是,您每次添加后都会清除条目列表。 you should use a separate list for each dataset. 您应该为每个数据集使用单独的列表。

replace the line: 替换行:

entries.clear();

with

List<Entry> entries = new ArrayList<>();

it's now solved by creating a Function and call it as below : 现在可以通过创建一个Function并按如下方式调用它来解决:

 **dataSets.add(createLineChart(company,company.StoreName,company.Total));**

                data = new LineData(column,dataSets);


                linechart.setData(data);
                linechart.invalidate();
                linechart.setDescription("Sales");

and this is the function: 这是函数:

 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;

}

it seems that the LineDataSet was used the last time it was called displaying just one line. 似乎最后一次使用LineDataSet时,它仅显示一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM