How to set custom color for Piechart element

坚强是说给别人听的谎言 提交于 2019-12-02 17:42:05

问题


I am using MPChart libarary in my android project. I have Json which contain label,value and color for pie chart generation. I want to set same color from json to piechart element. I followed MPChart documentation,But not found any solution for piechart color setting.


回答1:


You did not find a solution in the documentation? I suggest you take a closer look.

There are many other ways for setting colors for a DataSet:

  • setColors(int [] colors, Context c): Sets the colors that should be used fore this DataSet. Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. You can use "new int[] { R.color.red, R.color.green, ... }" to provide colors for this method. Internally, the colors are resolved using getResources().getColor(...).
  • setColors(int [] colors): Sets the colors that should be used fore this DataSet. Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. Make sure that the colors are already prepared (by calling getResources().getColor(...)) before adding them to the DataSet.
  • setColors(ArrayList<Integer> colors): Sets the colors that should be used fore this DataSet. Colors are reused as soon as the number of Entries the DataSet represents is higher than the size of the colors array. Make sure that the colors are already prepared (by calling getResources().getColor(...)) before adding them to the DataSet.
  • setColor(int color): Sets the one and ONLY color that should be used for this DataSet. Internally, this recreates the colors array and adds the specified color.



回答2:


Adding to what Philipp Jahoda answered...

You can also create an array that will contain customized colors and as many colors you want, you can add.

For example:

int [] color={ Color.rgb(100,221,23), Color.rgb(128,0,128), Color.rgb(255,136,0),
          Color.rgb(255,0,0), Color.rgb(255,127,80), Color.rgb(47,95,255)
};

to get the rgb code you can first get the hex code of the color you want and then convert it into rgb code using online converters on google.

And now you can use this color array to give color to your elements of pie chart in this way :

PieDataSet dataSet= new PieDataSet(Yvalues,"Activities");

dataSet.setColors(color);

This method will help you to set your own colors and you can give colors to as many elements of pie chart.

But if you use all these methods:

setColors(int [] colors, Context c)

setColors(int [] colors)

setColors(ArrayList<Integer> colors)

setColor(int color)

they will limit by allowing to use only five elements for pie chart and only few predefined colors.

Thankyou.



来源:https://stackoverflow.com/questions/31315588/how-to-set-custom-color-for-piechart-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!