Open NabeelAhmed101 opened 4 years ago
U
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();
}
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() {
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.