在MPAndroidChart中设置饼图切片颜色

时间:2016-05-26 18:52:54

标签: android android-studio mpandroidchart

我需要在饼图中为每个切片定义特定的十六进制值。

我跟随wiki,但该方法似乎不适用于PieDataSet

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

显示以下错误:

Cannot resolve symbol 'green1'

Expression expected  <-- At the 'Context'

是否有其他方法可以设置饼图切片颜色?这似乎适用于折线图,但不适用于饼图。

5 个答案:

答案 0 :(得分:8)

找到了解决方法:

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

答案 1 :(得分:1)

这样您就可以使用colors.xml中的正确颜色名称:

label10

答案 2 :(得分:0)

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

答案 3 :(得分:0)

使用ContextCompat.getColor(context, R.color.green1)代替R.color.green1

科特琳的示例代码:

 pieDataSet.colors = mutableListOf(ContextCompat.getColor(context, R.color.greenGraphs),
                                   ContextCompat.getColor(context, R.color.orangeGraphs),
                                   ContextCompat.getColor(context, R.color.blueGraphs), 
                                   ContextCompat.getColor(context, R.color.purpleGraphs))

答案 4 :(得分:0)

通过这种方式,您可以使用 colors.xml 文件中的颜色

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