PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.64k stars 9.02k forks source link

How to add data dynamically in Dataset and legend in MPAndroidChart? #4727

Open NabeelAhmed101 opened 4 years ago

NabeelAhmed101 commented 4 years ago

I'm using ROOM database, in that database I've a table called IncomeExpense. And in that table I've two columns called Category and Amount. I get the data from table by GROUP BY Category.

@Query("SELECT SUM(amount) as amount,category from IncomeExpense GROUP BY category") LiveData<List> getCategoryAmount();

The above query return me the LiveData of amount and category. I want to show that amount in DataSet pie chart and the category in legend.

The color of pie chart and legend should be select dynamically also according to new entry.

`private void addDataSet() {

    ArrayList<Integer> amount = new ArrayList<>();
    for (int i=0; i<ieCategoryModels1.size(); i++)
    {
        amount.add(ieCategoryModels1.get(i).getAmount());
    }
    ArrayList<PieEntry> yEntrys = new ArrayList<>();
    yEntrys.add(amount);

    //create the data set
    PieDataSet pieDataSet = new PieDataSet(yEntrys, "");
    pieDataSet.setValueTextSize(12);
    pieDataSet.setValueTextColor(Color.WHITE);

    //add colors to dataset
    ArrayList<Integer> colors = new ArrayList<>();
    ArrayList<Integer> colors1 = new ArrayList<>();
    colors.add(Color.rgb(0, 100, 0));
    colors.add(Color.rgb(245, 199, 0));
    colors.add(Color.rgb(193, 37, 82));
    colors.add(Color.rgb(106, 150, 31));
    colors.add(Color.rgb(179, 100, 53));
    colors.add(Color.rgb(255, 102, 0));

    pieDataSet.setColors(colors);
    Legend l = piechartC.getLegend();
    l.setEnabled(true);
    List<String> stringList = new ArrayList<>();
    for(int i=0;i<ieCategoryModels1.size();i++) {
        colors1.add(colors.get(i));
    }

    for(int i=0;i<ieCategoryModels1.size();i++) {
        stringList.add(ieCategoryModels1.get(i).getCategory());
    }
    l.setCustom(colors1, stringList);

    //create pie data object
    PieData pieData = new PieData(pieDataSet);
    pieData.setValueFormatter(new PercentFormatter());
    piechartC.setData(pieData);
    piechartC.invalidate();
}`

This is how I tried to show the data in pie chart but its not working. I don't know how to dynamically set the data of amount group by category in another chart.

please help me with this as I'm new in android.

U953 commented 4 years ago

U

sahruday commented 4 years ago

Hope this works private void addDataSet() {

//ArrayList<Integer> amount = new ArrayList<>();
ArrayList<PieEntry> yEntrys = new ArrayList<PieEntry>();
for (int i=0; i<ieCategoryModels1.size(); i++) yEntrys.add(PieEntry(ieCategoryModels1.get(i).getAmount(),ieCategoryModels1.get(i).getCategory()));

//create the data set
PieDataSet pieDataSet = new PieDataSet(yEntrys, "");
pieDataSet.setValueTextSize(12);
pieDataSet.setValueTextColor(Color.WHITE);
pieDataSet.setSliceSpace(1);

//add colors to dataset
ArrayList<Integer> colors = new ArrayList<>();
//ArrayList<Integer> colors1 = new ArrayList<>(); // No need for new set, it will iterate itself
colors.add(Color.rgb(0, 100, 0));
colors.add(Color.rgb(245, 199, 0));
colors.add(Color.rgb(193, 37, 82));
colors.add(Color.rgb(106, 150, 31));
colors.add(Color.rgb(179, 100, 53));
colors.add(Color.rgb(255, 102, 0));

pieDataSet.setColors(colors);
Legend l = piechartC.getLegend();
l.setEnabled(true);

//create pie data object
PieData pieData = new PieData(pieDataSet);
pieData.setValueFormatter(new PercentFormatter());
piechartC.setData(pieData);
piechartC.invalidate();

}