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 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, color: "#4661EE" },
{ x: 20, y: 55, color: "#EC5657" },
{ x: 30, y: 50, color: "#1BCDD1" },
{ x: 40, y: 65, color: "#8FAABB" },
{ x: 50, y: 95, color: "#B08BEB" },
{ x: 60, y: 68, color: "#3EA0DD" },
{ x: 70, y: 28, color: "#F5A52A" },
{ x: 80, y: 34, color: "#23BFAA" },
{ x: 90, y: 14, color: "#FAA586" }
]
}]
});
renderMultiColoredAreaChart();
chart.render();
function renderMultiColoredAreaChart() {
var multiColoredAreaChart = [];
for (var i = 0; i < chart.options.data[0].dataPoints.length - 1; 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 options = chart.options.data[0];
options.type = "line";
options.name = options.name || "DataSeries 1";
multiColoredAreaChart.push(options);
chart.options.data = multiColoredAreaChart;
chart.options.toolTip = {
shared: true,
content: "<b>{x}: </b> {y}",
};
}