Adding markers to Dynamic line chart only when the given condition satisfies - CanvasJS JavaScript Charts
Adding markers to Dynamic line chart only when the given condition satisfies - 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 dps = []; // dataPoints
var chart = new CanvasJS.Chart("chartContainer",{
title :{
text: "Dynamic Chart"
},
data: [{
type: "line",
markerType:"none",
dataPoints: dps
}]
});
var xVal = 0;
var yVal = 100;
var updateInterval = 1000;
var dataLength = 20; // number of dataPoints visible at any point
var updateChart = function (count) {
count = count || 1; // count is number of times loop runs to generate random dataPoints.
for (var j = 0; j < count; j++) {
yVal = yVal + Math.floor(5 + Math.random() *(-5-5));
if((yVal % 10) === 0) { // marker will be visible only if this condition is met. You can add the required condition here.
dps.push({
x: xVal,
y: yVal,
markerSize: 8,
markerType: "circle"
});
}
else {
dps.push({
x: xVal,
y: yVal
});
}
xVal++;
};
chart.render();
};
// generates first set of dataPoints
updateChart(dataLength);
// update chart after specified time.
setInterval(function(){updateChart()}, updateInterval);