Setting the pie slice colors in MPAndroidChart Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Setting the pie slice colors in MPAndroidChart without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I need to define specific hex values for each slice in my pie chart.

I’m following the wiki but the method doesn’t seem to be working for PieDataSet

PieDataSet dataSet = new PieDataSet(entries, "Fuel");
dataSet.setColors(new int[] { R.color.green1, R.color.green2, R.color.green3, R.color.green4 }, Context);

These errors are shown:

Cannot resolve symbol 'green1'

Expression expected  <-- At the 'Context'

Is there an alternate way to set the pie slice color? This seems to work for Line charts but not for pie.

Answer

Found a work around:

final int[] MY_COLORS = {Color.rgb(192,0,0), Color.rgb(255,0,0), Color.rgb(255,192,0),
                                Color.rgb(127,127,127), Color.rgb(146,208,80), Color.rgb(0,176,80), Color.rgb(79,129,189)};
                        ArrayList<Integer> colors = new ArrayList<Integer>();

                        for(int c: MY_COLORS) colors.add(c);

                        dataSet.setColors(colors);
We are here to answer your question about Setting the pie slice colors in MPAndroidChart - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji