Reputation: 1107
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.
Upvotes: 0
Views: 1937
Reputation: 710
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/
Upvotes: 1
Reputation: 805
Highcharts.setOptions({
colors: ['#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4']
});
It should work that way ;-)
Upvotes: 2