How to set custom color for Piechart element

前端 未结 2 1670
一整个雨季
一整个雨季 2021-01-28 18:19

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 eleme

相关标签:
2条回答
  • 2021-01-28 18:28

    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.

    0 讨论(0)
  • 2021-01-28 18:43

    You did not find a solution in the documentation? Have a look here.

    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.
    0 讨论(0)
提交回复
热议问题