简体   繁体   中英

How to set color of each slice in pie chart in high charts library?

I am making pie chart from high chart library. I want to change color of each slice in pie chart How can i do this ? My code is -

var colors = ["color:'#2a8482'","color:'#64DECF'","color:'#BCCDF8'"]; 
var responseData = {"2":40,"1":30}
var obj =  $.parseJSON(responseData);
             var dataArrayFinal = Array();
             var value = Array();
             var name = Array();
             var j = 0;
             for (var key in obj) {
                  if (obj.hasOwnProperty(key)) {
                      name[j] =  "name:'"+key+"'";
                      value[j] = obj[key];
                      j++;
                  }
             }

             for(k=0;k<name.length;k++) { 
                   var temp = new Array(name[k],value[k],colors[k]); 
                   dataArrayFinal[k] = temp;     
             }

            $(function () {

                new Highcharts.Chart({
                    chart: {
                        renderTo: 'container'+counter,
                        type: 'pie',
                        height: 280,
                        width: 280
                    },
                    title: {
                        text: ' '
                    },
                    tooltip: {
                        valueSuffix: '%',
                        positioner: function () {
                            return {
                                x: this.chart.series[0].center[0] -
                                   (this.label.width / 2) + 8,
                                y: this.chart.series[0].center[1] -
                                   (this.label.height / 2) + 8
                            };
                        }
                    },
                    series: [{
                        name: 'Answer',
                        size: '100%',
                        innerSize: '65%',
                        data: dataArrayFinal
                    }]
                });
            });

In this graph is coming but colors are not changed. They are taking default color.

在此处输入图片说明

Highcharts.setOptions({
 colors: ['#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4']
});

It should work that way ;-)

if you change your color-array to:

var colors = ["#2a8482","#64DECF","#BCCDF8"];

and then at the beginning of your function:

$(function () {

add the following code:

Highcharts.getOptions().plotOptions.pie.colors = colors;

this should do the trick.

Example of coloring can be found in this JSFiddle, created by the developers of HighCharts: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/pie-monochrome/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM