Skip to content

fix(ui-chart): honour msg y-axis property for number/string payloads - #2393

Open
pcbeingused333 wants to merge 10 commits into
FlowFuse:mainfrom
pcbeingused333:fix/chart-yaxis-payload
Open

pcbeingused333 wants to merge 10 commits into
FlowFuse:mainfrom
pcbeingused333:fix/chart-yaxis-payload

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2392

When Y is configured as a message property, the chart uses that property for Y. A missing parent in a nested path such as msg.value.y is handled as absent chart data so a message carrying ui_update still reaches the client.

An empty msg.payload array remains the chart's clear command, including when the configured Y property is absent. The regression test seeds chart history, sends the clear command through the node, and checks that history is empty and that the socket message carries an empty datapoint array.

All 11 tests in test/nodes/widgets/ui_chart.spec.js passed.

Changes and description were prepared with AI assistance.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@colinl

colinl commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

This does fix the issue in the test flow I posted, but the change makes decisions on what is in msg.payload and the error is not related to the value in msg.payload. If the posted flow is changed slightly to Delete msg.payload then again no data are plotted.

@colinl

colinl commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Fix

For a single (non-array) number/string payload, evaluate the configured msg property for y, falling back to the payload if it is undefined. The default (msg.payload) is unchanged.

This is the fundamental error here. It should just read something like:
Where Y is configured as a msg property then use that property for y.

pcbeingused333 and others added 2 commits November 23, 2026 18:05
…payload

When y is configured as a msg property, use it for a single (non-array)
value whether msg.payload is a number, a string or absent, instead of
deciding by the payload's type.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @colinl, you were right and my first follow-up was incomplete: it only fixed the _datapoint computed in beforeSend. A message without msg.payload (e.g. after Delete msg.payload) was still dropped, because onInput skipped the history store and the client ignored messages without a payload. Pushed another commit: when Y is a msg property, a message that carries that property now counts as data on the server (stored in history) and on the client (plotted). ui_update messages without a payload are still not treated as data. Added tests that check the point reaches the datastore for { topic: 'a', value: 3 }, and that a payload-less ui_update does not. I've also updated the description: when Y is a msg property, that property is used for y, with no fallback to the payload.

This comment was drafted with AI assistance (Claude Code); the change was tested by running test/nodes/widgets/ui_chart.spec.js (the client-side change in UIChart.vue has not been exercised in a browser).

@colinl

colinl commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

The function addToChart(payload, series) is a low level functions that adds the data in payload to the chart. It should not be testing config or message properties and overriding the passed in payload parameter. The fix needs to be at a higher level so that, where a msg property is configured for the Y value, then that is picked up and passed into addToChart()

addToChart is a low-level helper and should not read config or msg to override the value it is given. Resolve the configured msg y property in beforeSend and pass it in.
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @colinl, that makes sense. addToChart no longer reads the config or the msg: beforeSend now resolves the value first (the configured msg property when Y is a msg property, otherwise the payload) and passes it into addToChart, which is back to just adding the value it is given. Pushed as a new commit; behaviour and tests are unchanged (test/nodes/widgets/ui_chart.spec.js passes).

This comment was drafted with AI assistance (Claude Code); the change was tested by running test/nodes/widgets/ui_chart.spec.js.

@colinl

colinl commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

In the test flow in the issue, if msg.payload is set to an object then addToChart() is called with that object in the first parameter. Is that intended? Somehow the correct value does end up getting added to the chart, but not from that parameter. I do not understand why, if the value to plot is in msg.someProperty, that msg.payload is being tested to see if it is an object.

Also the new function isDataMsg returns true if msg.payload is present. However, if the value to be charted is coming from, for example, msg.value then the value of msg.payload is irrelevant. If msg.value is not present then it is not a data message whatever the value of msg.payload.

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @colinl. On the first point: you're right that the object case shouldn't be left out. With Y set to a msg property, beforeSend now resolves y from that property for object payloads as well, and passes it to addToChart instead of letting addToChart read the config/msg again. The object payload still supplies x and the series (by key); only y comes from the msg property. Added a test with an object payload and Y = msg.value. (The result for objects was already y from the msg property in the plain case, so this is a consistency change, and the test guards it.)

On isDataMsg: agreed, it was wrong. When Y is a msg property it no longer looks at msg.payload: a msg is data only if that property is present and the msg is not a ui_update (an empty-array payload still counts, as the clear request). A msg with a payload but no value in the configured property is not stored. Added a test for that case; test/nodes/widgets/ui_chart.spec.js passes.

This comment was drafted with AI assistance (Claude Code); the change was tested by running test/nodes/widgets/ui_chart.spec.js.

@colinl

colinl commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

a msg is data only if that property is present and the msg is not a ui_update

It is perfectly valid to have chart data and msg.ui_update in the same message.

an empty-array payload still counts, as the clear request

I think that possibly, if a message property is specified for the data, then the empty array should be sent to that property rather than msg.payload to clear the chart. What do you think?
@n-lark I think your opinion is needed here.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @colinl. Agreed on ui_update: it can travel with chart data, so I removed that exclusion. When Y is a msg property, a msg is now data whenever that property is present, with or without msg.ui_update; a ui_update msg without it is still not stored. Added a test for the combined case, which fails without the change.

On clearing: sending the empty array to the configured property (e.g. msg.value = []) does seem more consistent with taking the data from there. The catch is compatibility: msg.payload = [] clears the chart today, including for charts where Y is already a msg property other than payload, so existing flows could rely on it. The options I see are (a) keep msg.payload = [] only, (b) accept [] in either place, or (c) move to the configured property only. I'd lean towards (b), but have not changed this behaviour and will follow what you and @n-lark prefer.

This comment was drafted with AI assistance (Claude Code); the change was tested by running test/nodes/widgets/ui_chart.spec.js.

@colinl

colinl commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Why does isDataMsg() always return true if msg.payload is an array, even if msg.payload is not the source of the data t be plotted?
Similarly line 144
const yFromMsg = config.yAxisPropertyType === 'msg' && !Array.isArray(p) && isDataMsg(msg)
is affected by msg.payload being an array for some reason.

I am having difficulty working out why the new override parameter is necessary in addToChart, can you explain the logic please?

@colinl

colinl commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Another issue. If Y is set to msg.value.y then ui_update messages do not appear to be getting through to the chart. It is ok if Y is msg.value.

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks, Colin. The array exception in isDataMsg() was there to preserve the existing clear command. It was too broad, so it now applies only to msg.payload = []. With Y set to msg.value, that still clears the chart and its history even when msg.value is absent. A non-empty payload array without msg.value is ignored. The clear command still uses msg.payload while you and @n-lark consider the API.

The override was intended to let an object payload supply X and series while getting Y from the configured message property. The object path already reads that property, so the override was redundant and has been removed. The array path already processes the payload separately, which also makes the explicit array check in yFromMsg redundant.

For msg.value.y, Node-RED throws when the parent property is missing. The server now treats that missing path as absent chart data and still sends the options update to the client. The widget tests cover the socket update and the empty-payload clear command, and all 11 pass.

This reply was drafted with AI assistance.

@colinl

colinl commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

I thought we might be there at last, but charts where the series is defined by JSON seem to be messed up. This flow doesn't plot anything.

[{"id":"ba41b692dfb2e8ae","type":"ui-chart","z":"997da33a0beedade","g":"0ab5c415ed940ea6","group":"467c894404fead05","name":"multi series test","label":"multi series test","order":1,"chartType":"line","category":"[\"sin\",\"cos\"]","categoryType":"json","xAxisLabel":"","xAxisProperty":"","xAxisPropertyType":"timestamp","xAxisType":"time","xAxisFormat":"","xAxisFormatType":"{HH}:{mm}:{ss}","xmin":"","xmax":"","yAxisLabel":"","yAxisProperty":"payload.value","yAxisPropertyType":"msg","ymin":"","ymax":"","bins":10,"action":"append","stackSeries":false,"pointShape":"circle","pointRadius":"3","showLegend":true,"removeOlder":"1","removeOlderUnit":"60","removeOlderPoints":"","colors":["#0095ff","#ff0000","#ff7f0e","#2ca02c","#a347e1","#d62728","#ff9896","#9467bd","#c5b0d5"],"textColor":["#346cda"],"textColorDefault":false,"gridColor":["#2dd920"],"gridColorDefault":false,"width":"4","height":"4","className":"","interpolation":"linear","x":1060,"y":2520,"wires":[[]]},{"id":"592d028f96a0c84e","type":"inject","z":"997da33a0beedade","g":"0ab5c415ed940ea6","name":"","props":[{"p":"payload"}],"repeat":"5","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":930,"y":2460,"wires":[["f2cc910a6bf6108b"]]},{"id":"f2cc910a6bf6108b","type":"function","z":"997da33a0beedade","g":"0ab5c415ed940ea6","name":"Sine wave","func":"const min =5\nconst max = 8\nconst now = new Date().getTime()\n// 1 cycle/min\nlet sin = Math.sin(now*6.283/60000) * (max-min)/2 + (max+min)/2\nlet cos = Math.cos(now*6.283/60000) * (max-min)/2 + (max+min)/2\n//msg.payload = {sin: sin}\nmsg.payload = {sin: sin, cos: cos}\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1110,"y":2460,"wires":[["ba41b692dfb2e8ae"]]},{"id":"914b1b1e5b2e85d4","type":"inject","z":"997da33a0beedade","g":"0ab5c415ed940ea6","name":"Clear","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"json","x":970,"y":2620,"wires":[["ba41b692dfb2e8ae"]]},{"id":"467c894404fead05","type":"ui-group","name":"multi series test","page":"a0d0557dd288bdc2","width":6,"height":1,"order":1,"showTitle":true,"className":"","visible":"true","disabled":"false","groupType":"default"},{"id":"a0d0557dd288bdc2","type":"ui-page","name":"Multi Series Test","ui":"ID-BASE-1","path":"/multiseriestest","icon":"home","layout":"grid","theme":"8dea5d50a632c4c6","breakpoints":[{"name":"Default","px":"0","cols":"3"},{"name":"Tablet","px":"576","cols":"6"},{"name":"Small Desktop","px":"768","cols":"9"},{"name":"Desktop","px":"1024","cols":"12"}],"order":5,"className":"","visible":"true","disabled":"false"},{"id":"ID-BASE-1","type":"ui-base","name":"Dashboard","path":"/dashboard","appIcon":"","includeClientData":true,"acceptsClientConfig":["ui-control","ui-notification"],"showPathInSidebar":false,"headerContent":"page","titleBarStyle":"default","showReconnectNotification":false,"notificationDisplayTime":"1","showDisconnectNotification":true,"allowInstall":true},{"id":"8dea5d50a632c4c6","type":"ui-theme","name":"CDL test","colors":{"surface":"#ffffff","primary":"#0094ce","bgPage":"#eeeeee","groupBg":"#ffffff","groupOutline":"#cccccc"},"sizes":{"density":"default","pagePadding":"12px","groupGap":"12px","groupBorderRadius":"4px","widgetGap":"12px"}},{"id":"fe80c0e259ec1ad9","type":"global-config","env":[],"modules":{"@flowfuse/node-red-dashboard":"1.37.0"}}]

Everything else that I have tested appears to be ok.

…grams

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @colinl. The JSON-series flow plotted nothing because the new data check required a value at the Y property (payload.value in your flow), even though the editor hides that field when Series is JSON and the series keys supply the values. The Y property is now ignored for JSON series, and also for histograms, where the field is hidden too. Each case now has a test in test/nodes/widgets/ui_chart.spec.js (both fail without the change). Your flow has not been tried in a browser yet.

This reply was drafted with AI assistance.

@colinl

colinl commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Your flow has not been tried in a browser yet.

Should the flow not have tested before committing the changes?

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Yes, it should have been, sorry. It has now been run in a headless Chromium against 695145a: your multi-series flow plots both sin and cos, they are still there after a reload, and Clear empties the chart. The flow from #2392 plots msg.value, also with msg.payload deleted. With Y = msg.value.y, a ui_update-only message still changes the chart title. The only change to your flows was a 1 s inject interval; no further commits.

This reply was drafted with AI assistance, and the browser check was run by the AI agent.

@colinl

colinl commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Excellent, all of my test flows appear to work correctly now.

@n-lark I think we need a decision as to what message should clear the chart. Currently msg.payload = [] clears the chart, independent of where the data come from. There are four situations I think:

  1. Data in msg.payload
  2. Data in msg.payload.someProperty
  3. Data in msg.someOtherProperty
  4. Data in msg.someOtherProperty.yetAnother

The question is, in each of those cases, where one should send the empty array to clear the chart?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chart fails if value is in, for example, msg.value rather than msg.payload

2 participants