Setting different fillColor for each dataPoint in Area Chart - CanvasJS JavaScript Charts
Setting different fillColor for each dataPoint in Area 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 colorSet = ["#4661EE","#EC5657","#1BCDD1","#8FAABB","#B08BEB","#3EA0DD","#F5A52A",
"#23BFAA","#FAA586"];
var chart = new CanvasJS.Chart("chartContainer", {
title: {
text: "Different Fill Color for each dataPoint"
},
data: [{
type: "area",
color: "green",
fillOpacity: .9,
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55 },
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14 }
]
}]
});
renderMultiColoredAreaChart();
chart.render();
function renderMultiColoredAreaChart() {
var multiColoredAreaChart = [];
for (var i = 0; i < chart.options.data[0].dataPoints.length - 1; i++) {
chart.options.data[0].dataPoints[i].color = colorSet[i];
multiColoredAreaChart.push({
type: "area",
fillOpacity: chart.options.data[0].fillOpacity || 1,
toolTipContent: null,
highlightEnabled: false,
showInLegend: false,
markerSize: 0,
color: chart.options.data[0].dataPoints[i].color,
dataPoints: [{
x: chart.options.data[0].dataPoints[i].x,
y: chart.options.data[0].dataPoints[i].y
}, {
x: chart.options.data[0].dataPoints[i + 1].x,
y: chart.options.data[0].dataPoints[i + 1].y
}]
});
}
var dataSeries = chart.options.data[0];
dataSeries.type = "line";
dataSeries.name = dataSeries.name || "DataSeries 1";
multiColoredAreaChart.push(dataSeries);
chart.options.data = multiColoredAreaChart;
chart.options.toolTip = {
shared: true,
content: "<b>{x}: </b> {y}",
};
}