IbrahimMitko
IbrahimMitko

Reputation: 1207

Setting the pie slice colors in MPAndroidChart

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.

Upvotes: 7

Views: 14960

Answers (5)

IbrahimMitko
IbrahimMitko

Reputation: 1207

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

Upvotes: 14

Akshay Rajput
Akshay Rajput

Reputation: 163

This way you can use colors from your colors.xml file

  dataSet.setColors(getResources().getColor(R.color.colorPrimaryDark),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.greenShade),
                getResources().getColor(R.color.colorPrimaryDark),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.greenShade));

Upvotes: 0

Manohar
Manohar

Reputation: 23384

use ContextCompat.getColor(context, R.color.green1) instead of R.color.green1 if not colors will not shown properly.

example code in Kotlin :

 val colorFirst = context?.let { ContextCompat.getColor(it, R.color.colorFirst) }
 val colorSecond = context?.let { ContextCompat.getColor(it, R.color.colorSecond) }
 val colorThird = context?.let { ContextCompat.getColor(it, R.color.colorThird) }

 pieDataSet.colors = mutableListOf(colorFirst, colorSecond, colorThird)

Upvotes: 6

Harvin dutta
Harvin dutta

Reputation: 101

final int[] MY_COLORS = {
        Color.  rgb(0,255,255),
        Color. rgb(65,105,225)
};

ArrayList<Integer> colors = new ArrayList<>();

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

dataSet.setColors(colors);

Upvotes: 2

Lee Hounshell
Lee Hounshell

Reputation: 862

This way you can use proper color names from colors.xml:

    final int[] pieColors = {
            BaseActivity.getAppColor(R.color.blue),
            BaseActivity.getAppColor(R.color.SandyBrown),
            BaseActivity.getAppColor(R.color.silver),
            BaseActivity.getAppColor(R.color.FireBrick),
            BaseActivity.getAppColor(R.color.gray),
            BaseActivity.getAppColor(R.color.DarkMagenta),
            BaseActivity.getAppColor(R.color.olive),
            BaseActivity.getAppColor(R.color.MidnightBlue),
            BaseActivity.getAppColor(R.color.purple),
            BaseActivity.getAppColor(R.color.DeepSkyBlue),
            BaseActivity.getAppColor(R.color.maroon),
            BaseActivity.getAppColor(R.color.HotPink),
            BaseActivity.getAppColor(R.color.teal),
            BaseActivity.getAppColor(R.color.Purple),
            BaseActivity.getAppColor(R.color.green),
            BaseActivity.getAppColor(R.color.MediumSeaGreen)
    };

    ArrayList<Integer> colors = new ArrayList<>();
    for (int color : pieColors) {
        colors.add(color);
    }

    dataSet.setColors(colors);

    ...


public static int getAppColor(int resourceId) {
    Context context = MyApplication.getMyApplicationContext();
    int color;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        color = context.getResources().getColor(resourceId, context.getTheme());
    }
    else {
        //noinspection deprecation
        color = context.getResources().getColor(resourceId);
    }
    return color;
}

Upvotes: 2

Related Questions