I am trying to get data from influxdb to a multiline chart. I found many examples and tried them. But I don't seem to get it just right. My problem is with the (curly) brackets. Hope anyone can help me solve the problem.
> // Format the InfluxDB results to match the charts JSON format
> var series = ["A", "B"];
> var labels = [""];
> var data = "[[";
> var thetime, selfuse, totalsolar;
>
> for (var i=0; i < msg.payload.length; i++) {
> thetime = Number(msg.payload[i].time); // Some manipulation of the time may be required
> selfuse = msg.payload[i].selfuse;
> totalsolar = msg.payload[i].totalsolar;
> data += ' {"x":' + thetime + ', "y":' + selfuse + '},';
> data += ' {"x":' + thetime + ', "y":' + totalsolar +'} ';
> if (i < (msg.payload.length - 1)) {
> data += ","
> } else {
> data += "]]"
> }
> }
>
> var jsondata = JSON.parse(data);
> msg.payload = [{"series": series, "data": jsondata, "labels": labels}];
>
> return msg;
To clarify a bit, start with declaring data to be an empty array let data = []
(nowadays you should be using let not var)
Then in the loop create push each item into the array, something like data.push({x: thetime, y: selfuse})
then at the end, in the payload use data: [data]
Another problem I now see with the code is that you are trying to create two series, so, I think, but I haven't checked the docs, you will need to create arrays dataA and dataB and then in the payload returned use data:[dataA,dataB]
Colin, thanks for your reaction, Indeed I am trying for a multi line chart. I have tried to convert this (working) example for a single line chart, but got stuck. Now it is only 2 line chart, but I want even more lines.
Colin, it works like a charme. You were completly right!! Thanks very much.
This is the working function node now.
// Format the InfluxDB results to match the charts JSON format
let series = ["Eigen gebruik","Totaal Opgewekt"];
let labels = ["Watt"];
let data0 = [];
let data1 = [];
for (var i=0; i < msg.payload.length; i++) {
let thetime = Number(msg.payload[i].time);
let selfuse = msg.payload[i].selfuse;
let totalsolar = msg.payload[i].totalsolar;
data0.push({x: thetime, y: selfuse});
data1.push({x: thetime, y: totalsolar});
}
let data = [data0,data1];
// node.warn(data);
msg.payload = [{"series": series, "data": data, "labels": labels}];
return msg;