node-red-dashboard icon indicating copy to clipboard operation
node-red-dashboard copied to clipboard

Bar chart incorrect history load

Open Martin12350 opened this issue 10 months ago • 4 comments

Current Behavior

When the page is refreshed and a bar chart loaded from history, it ignores the order of the bars. On the start, I clear the chart and fill series with labels and values with zeros. When the appropriate series arrives, it correctly shows the value for the correct bar. However, when the page is refreshed, the bars are weirdly reordered.

https://github.com/FlowFuse/node-red-dashboard/assets/47942899/fd01470a-c0fa-41e5-b2cf-20bf00e828d2

Expected Behavior

The order of bars should persist across refreshes.

Steps To Reproduce

Flow to reproduce Test flow.json

Environment

  • Dashboard version: 1.7.1
  • Node-RED version: 3.1.8
  • Node.js version: 18.19.0
  • npm version: 10.2.3
  • Platform/OS: Linux RPi
  • Browser: Firefox and Chrome

Have you provided an initial effort estimate for this issue?

I am not a FlowFuse team member

Martin12350 avatar Apr 06 '24 13:04 Martin12350

I'm suffering on the same issue.

m-schaeffler avatar Apr 17 '24 16:04 m-schaeffler

If You want, I have a "hacky" solution. But! note that this way, it refreshes the whole chart every time it gets a new value, so if You are using phone data and have a big chart with a fast refresh rate, it will chew your data out in no time.

In the chart set series to key: bar and y to key: value. You can change those names obviously. And before the chart, create a function with this code:

var data = context.get("data") || [];

//init data if does not exist yet
if (data.length === 0)
    for (var i = 0; i < 60; i++) {
        const bar = ("0" + i).slice(-2) + ".";
        data.push({ bar, value: 0 });
    }

//update value
var index = data.findIndex(d => d.bar == msg.bar);
if (index != -1) data[index].value = msg.payload;
 
context.set("data", data);
msg.payload = data;
return msg;

It is pretty self-explanatory, but in any case, the code stores all the chart data in context, and when a message arrives with a bar value, it finds its column, updates the value, saves it in context, and sends it to the chart.

Btw. when setting the series to JSON, something similar happens - it always expects all the data and redraws the whole chart. It would be really cool if it would just init the chart on the first time and then updates the columns depending on the payload. Now, if any value is missing in the payload, but is specified in the series, it treats it as 0.

Martin12350 avatar Apr 17 '24 18:04 Martin12350

@Martin12350 Yes: this is a kind of workaround

But it happens also with line diagrams! If you press several times F5 to reload the page, sporadically the sequence of the lines in the legend is changed and so the colors of the lines are exchanged.

m-schaeffler avatar Apr 18 '24 18:04 m-schaeffler

I have also a cool proposition, but that should be probably in another thread. When passing a new value to the chart, it could check for msg.options, where could be specified options for that specific value, such as color of the bar, color of the line, line width for the segment etc. It is similar idea, that somebody already mentioned as advanced config (#58), but the difference is, it would be specific to the value, not the whole chart. That way we could have multicolored charts based on value, instead of the group.

Martin12350 avatar Apr 19 '24 19:04 Martin12350