Dynamic Column Chart Conditional Formatting
Dynamic Column Chart Conditional Formatting
by Emmanuel Umozurike
HTML
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
JavaScript
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column',
events: {
load: function (event) {
var theSeries = this.series[1];
var theCompSeries = this.series[0];
var theData = theSeries.data;
var theCompData = theCompSeries.data;
for (var i = 0; i < theData.length; i++) {
var theDataPoint = theData[i].y
var theCompDataPoint = theCompData[i].y;
if (theData[i].y < theCompData[i].y * 0.5) {
console.log(theSeries);
theSeries.points[i].update({
color: 'red'
});
}
}
}
}
},
title: {
text: 'Monthly Average Temperature'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
title: {
text: 'Temperature (°C)'
}
},
tooltip: {
enabled: false,
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y +'°C';
}
},
plotOptions: {
column: {
dataLabels: {
enabled: true,
formatter: function() {
if (this.y != 0) {
this.series.color = "#FF00FF";
...