CanvasJS Chart with Dynamic StripLines in Y-Axis
by canvasjs
HTML
<script type="text/javascript" src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
JavaScript
var chart = new CanvasJS.Chart("chartContainer", {
theme: "light2",
title: {
text: "Multi-Line Chart with Dynamic StripLines"
},
axisY: {
title: "Custom Y-Axis",
gridThickness: 0,
tickLength: 0,
labelFormatter: function(e) {
return "";
},
stripLines: []
},
data: [{
type: "line",
dataPoints: [
{ x: 10, y: 21 },
{ x: 20, y: 25},
{ x: 30, y: 20 },
{ x: 40, y: 25 },
{ x: 50, y: 27 },
{ x: 60, y: 28 },
{ x: 70, y: 28 },
{ x: 80, y: 24 },
{ x: 90, y: 26}
]
}, {
type: "line",
dataPoints: [
{ x: 10, y: 31 },
{ x: 20, y: 35},
{ x: 30, y: 30 },
{ x: 40, y: 35 },
{ x: 50, y: 35 },
{ x: 60, y: 38 },
{ x: 70, y: 38 },
{ x: 80, y: 34 },
{ x: 90, y: 44}
]
}, {
type: "line",
dataPoints: [
{ x: 10, y: 45 },
{ x: 20, y: 50},
{ x: 30, y: 40 },
{ x: 40, y: 45 },
{ x: 50, y: 45 },
{ x: 60, y: 48 },
{ x: 70, y: 43 },
{ x: 80, y: 41 },
{ x: 90, y: 28}
]
}, {
type: "line",
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}
]
}]
});
chart.options.axisY.stripLines = addDynamicStripLines(chart);
chart.render();
function addDynamicStripLines(chart) {
var stripLines = [];
for (var i = 0; i < chart.options.data.length; i++) {
var series = chart.options.data[i];
var firstDataPointY = series.dataPoints[0].y;
stripLines.push({
value: firstDataPointY,
color: "gray",
thickness: 2,
label: series.name || "Data " + i,
labelPlacement: "outside",
labelBackgroundColor: "transparent",
labelFontColor: "gray"
});
}
return stripLines;
}