I am pretty new to both Node-red and HTML, I hope I can get help on this forum. I have an HTML script in a core template node. Upon a GET request, the script renders a line graph from data array. The data is saved in a flow context array var by another node, and I need the HTML script node to read this data and plot it. Everything works fine on simulated data array that I generate "inside" the HTML script, but I could not figure out how to get the actual data from the flow context var (please see where the TODO is). Thanks in advance for any help.
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function () {
var options = {
exportEnabled: true,
animationEnabled: true,
animationDuration: 200,
theme: "light2",
title :{
text: "Simple Line Chart"
},
axisY: {
includeZero: false
},
data: [{
type: "spline",
indexLabelFontSize: 16,
dataPoints: []
}]
};
var xVal = 0;
var yVal = 100;
var updateInterval = 2000;
var WaveData = [];
// TODO temporary random data
var createWaveData = function () {
var srcData = flow.get('Pressure') || [];
WaveData = srcData;
/* WaveData = [];
var count = 50;
for ( var j = 0; j <count; j++) {
yVal = yVal + Math.round(5 + Math.random() *(-5-5));
WaveData.push(yVal);
}*/
}
// when plotting next WaveData, let it start from x = 0
var updateChart = function (cnt) {
createWaveData();
options.data[0].dataPoints = [];
for (var j = 0; j < cnt; j++) {
yVal = WaveData[j];
options.data[0].dataPoints.push({y: yVal});
}
(new CanvasJS.Chart("chartContainer", options)).render();
};
setInterval(function(){ updateChart(WaveData.length) }, updateInterval);
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width:100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>