Albert Albert - 6 months ago 135
Java Question

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? 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

Answer

it's now solved by creating a Function and call it as below :

 **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.