I need to set different colors for each column in Highcharts graph dynamically. My Highcharts graph is:
options = { chart: { renderTo: 'chart', type: 'column', width: 450 }, title: { text: 'A glance overview at your contest’s status' }, xAxis: { categories: ['Approved Photos', 'Pending Approval Photos', 'Votes', 'Entrants'], labels: { //rotation: -45, style: { font: 'normal 9px Verdana, sans-serif, arial' } } }, yAxis: { allowDecimals: false, min: 0, title: { text: 'Amount' } }, legend: { enabled: false }, series: [] }; series = { name: "Amount", data: [], dataLabels: { enabled: true, color: '#000000', align: 'right', x: -10, y: 20, formatter: function () { return this.y; }, style: { font: 'normal 13px Verdana, sans-serif' } } };
The data is set this way:
for (var i in Data) { if (parseInt(Data[i]) != 0) { series.data.push(parseInt(Data[i])); } else { series.data.push(null); } } options.series.push(series); chart = new Highcharts.Chart(options);
How can I dynamically set different colors for each data point in this loop?
On the Format tab, in the Current Selection group, click Format Selection. tab, expand Fill, and then do one of the following: To vary the colors of data markers in a single-series chart, select the Vary colors by point check box.
Add which colors you want to colors and then set colorByPoint to true . Reference: http://api.highcharts.com/highstock#colors. http://api.highcharts.com/highcharts#plotOptions.column.colorByPoint.
The plotOptions is a wrapper object for config objects for each series type. The config objects for each series can also be overridden for each series item as given in the series array. Configuration options for the series are given in three levels. Options for all series in a chart are given in the plotOptions.
Here is another solution with the latest version of Highcharts (currently 3.0).
Set the colorByPoint option to true and define the color sequence that you want.
options = { chart: {...}, plotOptions: { column: { colorByPoint: true } }, colors: [ '#ff0000', '#00ff00', '#0000ff' ] }
Here is an example based upon Highcharts Column with rotated labels demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With