If you are using node-red V1.3.x you can use functionexternalmodules
-
Add require
s for chart.js and canvas
-
Enter code to generate a chart in the on message
Demo flow...
[{"id":"1b1e2516d420ad11","type":"function","z":"553814a2.1248ec","name":"","func":"\nconst createCanvas = Canvas.createCanvas;\nconst canvas = createCanvas(320, 320)\ncanvas.style = {\"background-color\": \"white\"};\nconst ctx = canvas.getContext('2d');\n\nvar myChart = new Chart(ctx, {\n type: \"pie\",\n data: {\n labels: [\"Red\", \"Blue\", \"Yellow\", \"Green\", \"Purple\", \"Orange\"],\n datasets: [\n {\n label: \"# of Votes\",\n data: [12, 19, 3, 5, 2, 3],\n backgroundColor: [\n \"rgba(255, 99, 132, 0.2)\",\n \"rgba(54, 162, 235, 0.2)\",\n \"rgba(255, 206, 86, 0.2)\",\n \"rgba(75, 192, 192, 0.2)\",\n \"rgba(153, 102, 255, 0.2)\",\n \"rgba(255, 159, 64, 0.2)\"\n ],\n borderColor: [\n \"rgba(255, 99, 132, 1)\",\n \"rgba(54, 162, 235, 1)\",\n \"rgba(255, 206, 86, 1)\",\n \"rgba(75, 192, 192, 1)\",\n \"rgba(153, 102, 255, 1)\",\n \"rgba(255, 159, 64, 1)\"\n ],\n borderWidth: 1\n }\n ]\n },\n options: {\n responsive: false, //MUST BE false to avoid errors\n animation: false, //MUST BE false to avoid errors\n scales: {\n yAxes: [\n {\n ticks: {\n beginAtZero: true\n }\n }\n ]\n }\n }\n});\n\nctx.globalCompositeOperation = 'destination-over'\nctx.fillStyle = \"white\";\nctx.fillRect(0, 0, canvas.width, canvas.height);\n\ncanvas.toBuffer(function (err, buf) {\n if (err) throw err;\n msg.payload = buf;\n node.send(msg);\n //fs.writeFileSync(\"chart.png\", buf);\n});","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"Chart","module":"chart.js"},{"var":"Canvas","module":"canvas"}],"x":1610,"y":1500,"wires":[["81ecc8eadd602295"]]},{"id":"efd0d429627d8545","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1440,"y":1500,"wires":[["1b1e2516d420ad11"]]},{"id":"81ecc8eadd602295","type":"file","z":"553814a2.1248ec","name":"","filename":"chartimage.png","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":1800,"y":1500,"wires":[["460ac2f955f85fa9"]]}]