Color based on dataPoint value in Column Chart - CanvasJS JavaScript Charts
Color based on dataPoint value in Column Chart - CanvasJS JavaScript Charts
by canvasjs
HTML
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/>
<!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>
JavaScript
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Changing Color of Datapoint based on it's Value"
},
data: [{
type: "column",
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 },
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 92 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 71 },
{ x: 90, y: 54 },
{ x: 100, y: 60 },
{ x: 110, y: 36 },
{ x: 120, y: 49 },
{ x: 130, y: 21 }
],
}]
});
changeColor(chart);
chart.render();
function changeColor(chart) {
for (var i = 0; i < chart.options.data.length; i++) {
for (var j = 0; j < chart.options.data[i].dataPoints.length; j++) {
y = chart.options.data[i].dataPoints[j].y;
if (y > 30)
chart.options.data[i].dataPoints[j].color = "green";
else
chart.options.data[i].dataPoints[j].color = "red";
}
}
}