Dear sir, madam,
I'm kind of new to programming but does anyone know/ care to explain how to wait for msg from nodered before proceeding the function ? // I am using uibuilder
Currently I'm trying to make a chart using chart.js but due to lack of knowledge, I'm unable to figure out how wait for msg, before using the datasets (im getting error because it loads the datasets which is empty because it's not waiting for the msg), my current code is,
function syntaxHighlight(json) {
json = JSON.stringify(json, undefined, 4);
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
json = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])"(\s:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
myChart.update();
return json;
} // --- End of syntaxHighlight --- //
// run this function when the document is loaded
window.onload = function test(){
// Start up uibuilder - see the docs for the optional parameters
uibuilder.start();
// Listen for incoming messages from Node-RED
uibuilder.onChange('msg', function(msg){
//console.info('[indexjs:uibuilder.onChange] msg received from Node-RED server:', msg);
// dump the msg as text to the "msg" html element
const eMsg = document.getElementById('msg');
const actMsg = msg;
globalvar = msg.payload[0].data[0][1];
// eMsg.innerHTML = syntaxHighlight(msg);
console.log(globalvar);
});
var globalvar;
const test = globalvar;
console.log(test);
};
var myChart = document.getElementById('myChart').getContext('2d');
var myChart2 = new Chart(myChart, {
type: 'line',
data: {
labels: [],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
type: 'time',
ticks: {
autoSkip: true,
maxTicksLimit: 10
},
time: {
unit: 'minute',
unitStepSize: 1,
displayFormats: {
minute: 'HH:mm'
}
}
}],
yAxes: [{
id: "1",
ticks: {
min: 0,
max: 60,
}
}, {
id: "2",
ticks: {
min: -2,
max: 2,
stepSize: 0.4
}
}]
},
animation: {
duration: 0
}
}
});
And im using this data