Hello guys!
I want to plot my dynamic data on the canvas like on oscilloscope.
The idea:
-The data comes and drawed from left to right, and when it hits for example 500 or 1000 datapoints, it draws from the beginning again but with the previous data on the canvas.
Can i do that somehow?
My code is looks like this now:
<div style = "height: 400px; width: 70%;"id="mydiv">
<div id = "chartContainer" style = "height: 400px; width: 70%;"></div>
</div>
<script>
var yVal , xVal = 0;
var updateCount = 0;
var dataPoints = [];
var chart;
window.onload = function () {
chart = new CanvasJS.Chart("chartContainer", {
title : {
text : "Osc_Test"
},
data : [{
type : "line",
dataPoints : dataPoints
}
]
});
chart.render();
}
var updateChart = function () {
updateCount++;
dataPoints.push({
y : yVal,
x : xVal--
});
if (dataPoints.length > 500 )
{
dataPoints.shift();
//dataPoints.unshift();
}
chart.options.title.text = "Update " + updateCount;
chart.render();
};
/** MY DATA COMES FROM A WEBSOCKET MESSAGE LIKE THIS **/
webSocket1 = new WebSocket('ws://' + window.location.hostname + ':81/');
webSocket1.onmessage=function(a){
var t = a.data;
if(t.indexOf('}')>-1){
var l = t.substring(0,t.length-1);
yVal = parseInt(l,10);
updateChart();
}
};
</script>