NOTE: per @hardillb answer, I fixed the above flow and posted the right one below. Thanks in advance for any help. I am a beginner in HTML and have to use it in Node-RED. I have a simple flow (shown in the image link above). I inject a message with sample line graph points in payload. I want to receive the message in an HTML script in a template node, which then renders the received msg.payload data on a line graph on the browser. The HTML script works fine if I generate random graph from inside the script, but I am failing to get the script to receive msg.payload. I tried something similar to How to send msg.payload from function node to template node
The HTML script in waveform HTML is below. I indicated TODO what I tried and didn't work.
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function () {
//define line graph options for CanvasJS line graph
var options = {
exportEnabled: true,
animationEnabled: true,
animationDuration: 200,
theme: "light2",
title :{
text: "Simple Line Chart"
},
axisY: {
includeZero: false
},
data: [{
type: "spline",
indexLabelFontSize: 16,
dataPoints: [] //this will be loaded with "y" values from msg.payload
}]
};
//TODO not working ---------------------
//load WaveData from msg.payload
var WaveData = [];
var createWaveData = function () {
WaveData = {{{payload}}}; //TODO fix, not working
//WaveData = [1, 2, 3, 4, 5, 4, 3, 2, 1]; //debug code, works fine, sample data is plotted on graph
}
//--------------------------------------
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();
};
var updateInterval = 2000;
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>
</html>
This is the CORRECT flow per @hardillb answer.