From e1b3565534313154d362fe130c45c3e2b4b8ae82 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 22 Nov 2026 19:08:01 -0400 Subject: [PATCH 01/10] fix(ui-chart): honour msg y-axis property for number/string payloads Co-Authored-By: Claude Sonnet 5.5 --- nodes/widgets/ui_chart.js | 7 +++ test/nodes/widgets/ui_chart.spec.js | 73 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 test/nodes/widgets/ui_chart.spec.js diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index 815126422..8aff97cfb 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -193,7 +193,14 @@ module.exports = function (RED) { datapoint.x = config.xAxisPropertyType === 'msg' || config.xAxisPropertyType === 'str' ? evaluateNodePropertyWithKey(node, msg, payload, config.xAxisProperty, config.xAxisPropertyType) : (new Date()).getTime() + // for a single (non-array) payload, honour a y-axis property of type 'msg' (e.g. msg.value) datapoint.y = payload + if (!Array.isArray(msg.payload) && config.yAxisPropertyType === 'msg') { + const y = evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) + if (typeof y !== 'undefined') { + datapoint.y = y + } + } } else if (typeof payload === 'object') { // let x = evaluateNodePropertyWithKey(node, msg, payload, config.xAxisProperty, config.xAxisPropertyType) let x = null diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js new file mode 100644 index 000000000..b6bacf719 --- /dev/null +++ b/test/nodes/widgets/ui_chart.spec.js @@ -0,0 +1,73 @@ +const helper = require('node-red-node-test-helper') +const should = require('should') // eslint-disable-line no-unused-vars + +const { testData1 } = require('../fixtures/index.js') +const testFlow1 = testData1.flows +const nodeImports = testData1.getImports(null, ['ui_chart']) + +helper.init(require.resolve('node-red')) + +describe('ui-chart node', function () { + beforeEach(function (done) { + helper.startServer(done) + }) + + afterEach(function (done) { + helper.unload() + helper.stopServer(done) + }) + + function chartFlow (overrides) { + return [ + { + id: 'node-ui-chart', + type: 'ui-chart', + z: 'tab-id', + group: 'config-ui-group', + name: '', + label: 'chart', + order: 0, + chartType: 'line', + category: 'topic', + categoryType: 'msg', + xAxisProperty: '', + xAxisPropertyType: 'timestamp', + xAxisType: 'time', + yAxisProperty: 'payload', + yAxisPropertyType: 'msg', + action: 'append', + width: 4, + height: 4, + wires: [], + ...overrides + }, + ...testFlow1 + ] + } + + async function datapointFor (overrides, msg) { + const flow = chartFlow(overrides) + await helper.load(nodeImports, flow) + const chart = helper.getNode('node-ui-chart') + const base = helper.getNode('config-ui-base') + const widget = base.ui.widgets.get('node-ui-chart') + const out = widget.hooks.beforeSend({ ...msg }) + should(chart).be.an.Object() + return out._datapoint + } + + it('uses msg.payload for a numeric payload by default', async function () { + const dp = await datapointFor({}, { topic: 'a', payload: 7, value: 3 }) + dp.y.should.equal(7) + }) + + it('uses the configured msg property for the y value when payload is a number', async function () { + const dp = await datapointFor({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }, { topic: 'a', payload: 7, value: 3 }) + dp.y.should.equal(3) + }) + + it('uses the configured msg property for the y value when payload is a string', async function () { + const dp = await datapointFor({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }, { topic: 'a', payload: '7', value: 3 }) + dp.y.should.equal(3) + }) +}) From a8a9fcc96cbf1ba0222c1a8a3486cbdb3f52c0f1 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 23 Nov 2026 18:05:46 -0400 Subject: [PATCH 02/10] fix(ui-chart): take y from the configured msg property regardless of 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 --- nodes/widgets/ui_chart.js | 16 +++++++--------- test/nodes/widgets/ui_chart.spec.js | 5 +++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index 8aff97cfb..b011d1043 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -186,21 +186,19 @@ module.exports = function (RED) { datapoint.category = series // construct our datapoint - if (typeof payload === 'number' || typeof payload === 'string') { + // when y is configured as a msg property, a single (non-array) value takes y from that property, + // regardless of what (or whether) msg.payload holds + const yFromMsg = config.yAxisPropertyType === 'msg' && !Array.isArray(msg.payload) && typeof payload !== 'object' + if (typeof payload === 'number' || typeof payload === 'string' || yFromMsg) { // do we have an x-property defined - if not, we're assuming time series // since key would attempt to evaluate a property on a number, we don't do evaluation when // x-axis is type is 'key' (only when it's 'msg' or 'str') datapoint.x = config.xAxisPropertyType === 'msg' || config.xAxisPropertyType === 'str' ? evaluateNodePropertyWithKey(node, msg, payload, config.xAxisProperty, config.xAxisPropertyType) : (new Date()).getTime() - // for a single (non-array) payload, honour a y-axis property of type 'msg' (e.g. msg.value) - datapoint.y = payload - if (!Array.isArray(msg.payload) && config.yAxisPropertyType === 'msg') { - const y = evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) - if (typeof y !== 'undefined') { - datapoint.y = y - } - } + datapoint.y = yFromMsg + ? evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) + : payload } else if (typeof payload === 'object') { // let x = evaluateNodePropertyWithKey(node, msg, payload, config.xAxisProperty, config.xAxisPropertyType) let x = null diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index b6bacf719..cb9e79eb7 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -70,4 +70,9 @@ describe('ui-chart node', function () { const dp = await datapointFor({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }, { topic: 'a', payload: '7', value: 3 }) dp.y.should.equal(3) }) + + it('uses the configured msg property for the y value when msg.payload is not set', async function () { + const dp = await datapointFor({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }, { topic: 'a', value: 3 }) + dp.y.should.equal(3) + }) }) From 0e0887f645e0d51406c1e49ad39e63ce8b222d5f Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 23 Nov 2026 18:07:54 -0400 Subject: [PATCH 03/10] fix(ui-chart): plot msgs without payload when y is a msg property --- nodes/widgets/ui_chart.js | 18 +++++++++++++++--- test/nodes/widgets/ui_chart.spec.js | 25 +++++++++++++++++++++++++ ui/src/widgets/ui-chart/UIChart.vue | 7 ++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index b011d1043..d0bcc15ba 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -120,6 +120,18 @@ module.exports = function (RED) { config.stackSeries = false } + // a msg is data for the chart if it has a payload, or if y is configured as a msg property + // that is present on a msg that is not a ui_update + function isDataMsg (msg) { + if (typeof msg.payload !== 'undefined') { + return true + } + if (config.yAxisPropertyType !== 'msg' || msg.ui_update) { + return false + } + return RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) !== undefined + } + const evts = { // beforeSend will run before messages are sent client-side, as well as before sending on within Node-RED // here, we use it to pre-process chart data to format it ready for plotting @@ -188,7 +200,7 @@ module.exports = function (RED) { // construct our datapoint // when y is configured as a msg property, a single (non-array) value takes y from that property, // regardless of what (or whether) msg.payload holds - const yFromMsg = config.yAxisPropertyType === 'msg' && !Array.isArray(msg.payload) && typeof payload !== 'object' + const yFromMsg = config.yAxisPropertyType === 'msg' && !Array.isArray(msg.payload) && typeof payload !== 'object' && isDataMsg(msg) if (typeof payload === 'number' || typeof payload === 'string' || yFromMsg) { // do we have an x-property defined - if not, we're assuming time series // since key would attempt to evaluate a property on a number, we don't do evaluation when @@ -238,8 +250,8 @@ module.exports = function (RED) { if (!datastore.has(node.id)) { datastore.save(base, node, []) } - // To prevent ui_update messages from deleting old data, skip this section if no msg.payload present - if (typeof msg.payload !== 'undefined') { + // To prevent ui_update messages from deleting old data, skip this section if the msg carries no data + if (isDataMsg(msg)) { if (Array.isArray(msg.payload) && !msg.payload.length) { // clear history datastore.save(base, node, []) diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index cb9e79eb7..b235a3f39 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -1,6 +1,7 @@ const helper = require('node-red-node-test-helper') const should = require('should') // eslint-disable-line no-unused-vars +const datastore = require('../../../nodes/store/data.js') const { testData1 } = require('../fixtures/index.js') const testFlow1 = testData1.flows const nodeImports = testData1.getImports(null, ['ui_chart']) @@ -75,4 +76,28 @@ describe('ui-chart node', function () { const dp = await datapointFor({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }, { topic: 'a', value: 3 }) dp.y.should.equal(3) }) + + it('stores a point in the datastore when y is a msg property and msg.payload is not set', async function () { + const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) + await helper.load(nodeImports, flow) + const base = helper.getNode('config-ui-base') + const widget = base.ui.widgets.get('node-ui-chart') + datastore.save(base, helper.getNode('node-ui-chart'), []) + const msg = widget.hooks.beforeSend({ topic: 'a', value: 3 }) + await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) + const stored = datastore.get('node-ui-chart') + stored.should.have.length(1) + stored[0]._datapoint.y.should.equal(3) + }) + + it('does not store a point for a ui_update msg without payload', async function () { + const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) + await helper.load(nodeImports, flow) + const base = helper.getNode('config-ui-base') + const widget = base.ui.widgets.get('node-ui-chart') + datastore.save(base, helper.getNode('node-ui-chart'), []) + const msg = widget.hooks.beforeSend({ topic: 'a', ui_update: { chartOptions: {} } }) + await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) + datastore.get('node-ui-chart').should.have.length(0) + }) }) diff --git a/ui/src/widgets/ui-chart/UIChart.vue b/ui/src/widgets/ui-chart/UIChart.vue index 9d98891fc..06b6ae9f4 100644 --- a/ui/src/widgets/ui-chart/UIChart.vue +++ b/ui/src/widgets/ui-chart/UIChart.vue @@ -417,7 +417,8 @@ export default { onMsgInput (msg) { // ignore if payload is empty and msg is not an array (on loading it can be an array) // ui_update messages are handled by OnDynamicProperties - if (msg.payload !== undefined || Array.isArray(msg)) { + // a msg without payload is still data when the server computed a y value for it (y set to a msg property) + if (msg.payload !== undefined || Array.isArray(msg) || (msg._datapoint && msg._datapoint.y !== undefined)) { if (Array.isArray(msg.payload) && !msg.payload.length) { // clear the chart if msg.payload = [] is received this.clearChart() @@ -533,8 +534,8 @@ export default { this.addPoints(p, d, label, options) } }) - } else if (payload !== null && payload !== undefined) { - // we have a single payload value and should append it to the chart + } else if ((payload !== null && payload !== undefined) || (payload === undefined && msg._datapoint && !Array.isArray(msg._datapoint) && msg._datapoint.y !== undefined)) { + // we have a single payload value (or a y value taken from a msg property) and should append it to the chart if (Array.isArray(msg._datapoint)) { // single message has multiple datapoints to render (e.g. when Series is set to JSON) msg._datapoint.forEach((d) => { From 8df18e4b081f8291da082bb82f6cf9071c8eef99 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 24 Nov 2026 10:05:16 -0400 Subject: [PATCH 04/10] refactor(ui-chart): resolve the msg y property before 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. --- nodes/widgets/ui_chart.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index d0bcc15ba..5f09487fb 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -136,7 +136,12 @@ module.exports = function (RED) { // beforeSend will run before messages are sent client-side, as well as before sending on within Node-RED // here, we use it to pre-process chart data to format it ready for plotting beforeSend: function (msg) { + // when y is configured as a msg property, a single (non-object) value takes y from that property, + // regardless of what (or whether) msg.payload holds. This is resolved here, before addToChart, + // which just adds the value it is given to the chart const p = msg.payload + const yFromMsg = config.yAxisPropertyType === 'msg' && typeof p !== 'object' && isDataMsg(msg) + const single = yFromMsg ? RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) : p let series = RED.util.evaluateNodeProperty(config.category, config.categoryType, node, msg) // if receiving a object payload, the series could be a within the payload @@ -161,13 +166,13 @@ module.exports = function (RED) { const points = [] series.forEach((s) => { if (hasProperty(p, s)) { - const datapoint = addToChart(p, s) + const datapoint = addToChart(single, s) points.push(datapoint) } }) msg._datapoint = points } else { - msg._datapoint = addToChart(p, series) + msg._datapoint = addToChart(single, series) } } @@ -198,19 +203,14 @@ module.exports = function (RED) { datapoint.category = series // construct our datapoint - // when y is configured as a msg property, a single (non-array) value takes y from that property, - // regardless of what (or whether) msg.payload holds - const yFromMsg = config.yAxisPropertyType === 'msg' && !Array.isArray(msg.payload) && typeof payload !== 'object' && isDataMsg(msg) - if (typeof payload === 'number' || typeof payload === 'string' || yFromMsg) { + if (typeof payload === 'number' || typeof payload === 'string') { // do we have an x-property defined - if not, we're assuming time series // since key would attempt to evaluate a property on a number, we don't do evaluation when // x-axis is type is 'key' (only when it's 'msg' or 'str') datapoint.x = config.xAxisPropertyType === 'msg' || config.xAxisPropertyType === 'str' ? evaluateNodePropertyWithKey(node, msg, payload, config.xAxisProperty, config.xAxisPropertyType) : (new Date()).getTime() - datapoint.y = yFromMsg - ? evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) - : payload + datapoint.y = payload } else if (typeof payload === 'object') { // let x = evaluateNodePropertyWithKey(node, msg, payload, config.xAxisProperty, config.xAxisPropertyType) let x = null From dba87576328900694107446d74cccb6591748a9b Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 24 Nov 2026 13:34:09 -0400 Subject: [PATCH 05/10] fix(ui-chart): decide whether a msg is data from the y msg property, not msg.payload Co-Authored-By: Claude Sonnet 5.5 --- nodes/widgets/ui_chart.js | 12 ++++++++---- test/nodes/widgets/ui_chart.spec.js | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index 5f09487fb..0d3e92857 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -120,13 +120,17 @@ module.exports = function (RED) { config.stackSeries = false } - // a msg is data for the chart if it has a payload, or if y is configured as a msg property - // that is present on a msg that is not a ui_update + // a msg is data for the chart if it has a payload. When y is configured as a msg property, what matters is + // that property: the msg is data if it is present (and the msg is not a ui_update), whatever msg.payload holds. + // An empty array payload is still a clear request. function isDataMsg (msg) { - if (typeof msg.payload !== 'undefined') { + if (config.yAxisPropertyType !== 'msg') { + return typeof msg.payload !== 'undefined' + } + if (Array.isArray(msg.payload)) { return true } - if (config.yAxisPropertyType !== 'msg' || msg.ui_update) { + if (msg.ui_update) { return false } return RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) !== undefined diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index b235a3f39..bdea9714a 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -100,4 +100,15 @@ describe('ui-chart node', function () { await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) datastore.get('node-ui-chart').should.have.length(0) }) + + it('does not store a point when y is a msg property that is missing, whatever msg.payload holds', async function () { + const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) + await helper.load(nodeImports, flow) + const base = helper.getNode('config-ui-base') + const widget = base.ui.widgets.get('node-ui-chart') + datastore.save(base, helper.getNode('node-ui-chart'), []) + const msg = widget.hooks.beforeSend({ topic: 'a', payload: 7 }) + await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) + datastore.get('node-ui-chart').should.have.length(0) + }) }) From e3a963057e8095783a4509e0e4c237f495018a06 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 24 Nov 2026 13:36:13 -0400 Subject: [PATCH 06/10] refactor(ui-chart): resolve y from the msg property for object payloads too --- nodes/widgets/ui_chart.js | 21 ++++++++++++--------- test/nodes/widgets/ui_chart.spec.js | 6 ++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index 0d3e92857..0520ff5ef 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -140,12 +140,13 @@ module.exports = function (RED) { // beforeSend will run before messages are sent client-side, as well as before sending on within Node-RED // here, we use it to pre-process chart data to format it ready for plotting beforeSend: function (msg) { - // when y is configured as a msg property, a single (non-object) value takes y from that property, - // regardless of what (or whether) msg.payload holds. This is resolved here, before addToChart, - // which just adds the value it is given to the chart + // when y is configured as a msg property, a single value takes y from that property, regardless of + // what (or whether) msg.payload holds (an object payload still supplies x and the series). This is + // resolved here, before addToChart, which just adds the y it is given to the chart const p = msg.payload - const yFromMsg = config.yAxisPropertyType === 'msg' && typeof p !== 'object' && isDataMsg(msg) - const single = yFromMsg ? RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) : p + const yFromMsg = config.yAxisPropertyType === 'msg' && !Array.isArray(p) && isDataMsg(msg) + const msgY = yFromMsg ? RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) : undefined + const single = yFromMsg && typeof p !== 'object' ? msgY : p let series = RED.util.evaluateNodeProperty(config.category, config.categoryType, node, msg) // if receiving a object payload, the series could be a within the payload @@ -170,13 +171,13 @@ module.exports = function (RED) { const points = [] series.forEach((s) => { if (hasProperty(p, s)) { - const datapoint = addToChart(single, s) + const datapoint = addToChart(single, s, msgY) points.push(datapoint) } }) msg._datapoint = points } else { - msg._datapoint = addToChart(single, series) + msg._datapoint = addToChart(single, series, msgY) } } @@ -201,7 +202,7 @@ module.exports = function (RED) { } // function to process a data point being appended to a line/scatter chart - function addToChart (payload, series) { + function addToChart (payload, series, yOverride) { const datapoint = {} // we group/categorize data by "series" datapoint.category = series @@ -238,7 +239,9 @@ module.exports = function (RED) { // we are using the "series" as a key to get the y value from the payload y = getProperty(payload, series) } else { - y = evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) + y = typeof yOverride !== 'undefined' + ? yOverride + : evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) } } datapoint.x = x diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index bdea9714a..f2ffd7f12 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -77,6 +77,12 @@ describe('ui-chart node', function () { dp.y.should.equal(3) }) + it('uses the configured msg property for the y value when payload is an object', async function () { + const dp = await datapointFor({ yAxisProperty: 'value', yAxisPropertyType: 'msg', xAxisProperty: 'ts', xAxisPropertyType: 'property' }, { topic: 'a', payload: { y: 7, ts: 123 }, value: 3 }) + dp.y.should.equal(3) + dp.x.should.equal(123) + }) + it('stores a point in the datastore when y is a msg property and msg.payload is not set', async function () { const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) await helper.load(nodeImports, flow) From a5ea81220bed8221c5d8eb4c6f5228e6055abe2d Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 24 Nov 2026 19:32:11 -0400 Subject: [PATCH 07/10] fix(ui-chart): accept chart data in a msg that also carries ui_update Co-Authored-By: Claude Sonnet 5.5 --- nodes/widgets/ui_chart.js | 5 +---- test/nodes/widgets/ui_chart.spec.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index 0520ff5ef..ec578ef8e 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -121,7 +121,7 @@ module.exports = function (RED) { } // a msg is data for the chart if it has a payload. When y is configured as a msg property, what matters is - // that property: the msg is data if it is present (and the msg is not a ui_update), whatever msg.payload holds. + // that property: the msg is data if it is present, whatever msg.payload holds (it may also carry a ui_update). // An empty array payload is still a clear request. function isDataMsg (msg) { if (config.yAxisPropertyType !== 'msg') { @@ -130,9 +130,6 @@ module.exports = function (RED) { if (Array.isArray(msg.payload)) { return true } - if (msg.ui_update) { - return false - } return RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) !== undefined } diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index f2ffd7f12..374485878 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -107,6 +107,19 @@ describe('ui-chart node', function () { datastore.get('node-ui-chart').should.have.length(0) }) + it('stores a point for a msg carrying both a ui_update and a value in the y property', async function () { + const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) + await helper.load(nodeImports, flow) + const base = helper.getNode('config-ui-base') + const widget = base.ui.widgets.get('node-ui-chart') + datastore.save(base, helper.getNode('node-ui-chart'), []) + const msg = widget.hooks.beforeSend({ topic: 'a', value: 3, ui_update: { chartOptions: {} } }) + await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) + const stored = datastore.get('node-ui-chart') + stored.should.have.length(1) + stored[0]._datapoint.y.should.equal(3) + }) + it('does not store a point when y is a msg property that is missing, whatever msg.payload holds', async function () { const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) await helper.load(nodeImports, flow) From 0aae616ec40a98e988ef5f04ce696ce75e0612d5 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 25 Nov 2026 08:43:48 -0400 Subject: [PATCH 08/10] fix(ui-chart): respect configured y for data detection --- nodes/widgets/ui_chart.js | 69 ++++++++++++++++------------- test/nodes/widgets/ui_chart.spec.js | 30 +++++++++++-- ui/src/widgets/ui-chart/UIChart.vue | 4 ++ 3 files changed, 69 insertions(+), 34 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index ec578ef8e..7845d3a25 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -120,29 +120,51 @@ module.exports = function (RED) { config.stackSeries = false } - // a msg is data for the chart if it has a payload. When y is configured as a msg property, what matters is - // that property: the msg is data if it is present, whatever msg.payload holds (it may also carry a ui_update). - // An empty array payload is still a clear request. - function isDataMsg (msg) { - if (config.yAxisPropertyType !== 'msg') { - return typeof msg.payload !== 'undefined' - } - if (Array.isArray(msg.payload)) { - return true + function getConfiguredY (msg) { + try { + return RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) + } catch (err) { + // Node-RED throws when an intermediate property in the path is missing. + if (!(err instanceof TypeError)) { + throw err + } } - return RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) !== undefined + } + + function isDataMsg (msg) { + return config.yAxisPropertyType === 'msg' + ? getConfiguredY(msg) !== undefined + : msg.payload !== undefined } const evts = { // beforeSend will run before messages are sent client-side, as well as before sending on within Node-RED // here, we use it to pre-process chart data to format it ready for plotting beforeSend: function (msg) { + const updates = msg.ui_update + if (updates) { + if (typeof updates.chartOptions !== 'undefined') { + // merge chart options specified here in with any others previously set + const currentOptions = statestore.getProperty(node.id, 'chartOptions') ?? {} + // Deep merge new options in with old + const mergedOptions = deepMerge(currentOptions, updates.chartOptions) + statestore.set(group.getBase(), node, msg, 'chartOptions', mergedOptions) + // pass the full set to the clients so they don't need to do the merge + msg.ui_update.chartOptions = mergedOptions + } + } + + if (!isDataMsg(msg)) { + msg._datapoint = null + return msg + } + // when y is configured as a msg property, a single value takes y from that property, regardless of // what (or whether) msg.payload holds (an object payload still supplies x and the series). This is // resolved here, before addToChart, which just adds the y it is given to the chart const p = msg.payload - const yFromMsg = config.yAxisPropertyType === 'msg' && !Array.isArray(p) && isDataMsg(msg) - const msgY = yFromMsg ? RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) : undefined + const yFromMsg = config.yAxisPropertyType === 'msg' + const msgY = yFromMsg ? getConfiguredY(msg) : undefined const single = yFromMsg && typeof p !== 'object' ? msgY : p let series = RED.util.evaluateNodeProperty(config.category, config.categoryType, node, msg) @@ -168,26 +190,13 @@ module.exports = function (RED) { const points = [] series.forEach((s) => { if (hasProperty(p, s)) { - const datapoint = addToChart(single, s, msgY) + const datapoint = addToChart(single, s) points.push(datapoint) } }) msg._datapoint = points } else { - msg._datapoint = addToChart(single, series, msgY) - } - } - - const updates = msg.ui_update - if (updates) { - if (typeof updates.chartOptions !== 'undefined') { - // merge chart options specified here in with any others previously set - const currentOptions = statestore.getProperty(node.id, 'chartOptions') ?? {} - // Deep merge new options in with old - const mergedOptions = deepMerge(currentOptions, updates.chartOptions) - statestore.set(group.getBase(), node, msg, 'chartOptions', mergedOptions) - // pass the full set to the clients so they don't need to do the merge - msg.ui_update.chartOptions = mergedOptions + msg._datapoint = addToChart(single, series) } } @@ -199,7 +208,7 @@ module.exports = function (RED) { } // function to process a data point being appended to a line/scatter chart - function addToChart (payload, series, yOverride) { + function addToChart (payload, series) { const datapoint = {} // we group/categorize data by "series" datapoint.category = series @@ -236,9 +245,7 @@ module.exports = function (RED) { // we are using the "series" as a key to get the y value from the payload y = getProperty(payload, series) } else { - y = typeof yOverride !== 'undefined' - ? yOverride - : evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) + y = evaluateNodePropertyWithKey(node, msg, payload, config.yAxisProperty, config.yAxisPropertyType) } } datapoint.x = x diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index 374485878..20c66e65f 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -1,3 +1,5 @@ +const { EventEmitter, once } = require('node:events') + const helper = require('node-red-node-test-helper') const should = require('should') // eslint-disable-line no-unused-vars @@ -120,14 +122,36 @@ describe('ui-chart node', function () { stored[0]._datapoint.y.should.equal(3) }) + it('emits a chart options update when the parent of the configured y property is missing', async function () { + const flow = chartFlow({ yAxisProperty: 'value.y', yAxisPropertyType: 'msg' }) + await helper.load(nodeImports, flow) + const chart = helper.getNode('node-ui-chart') + const base = helper.getNode('config-ui-base') + const emitter = new EventEmitter() + base.uiShared.connections['chart-test'] = { id: 'chart-test', emit: emitter.emit.bind(emitter) } + const history = [{ _datapoint: { x: 1, y: 3, category: 'a' } }] + datastore.save(base, chart, history) + + const update = { chartOptions: { title: { text: 'Updated' } } } + const received = once(emitter, 'msg-input:' + chart.id) + chart.receive({ ui_update: update }) + const [msg] = await received + msg.ui_update.should.deepEqual(update) + should(msg._datapoint).equal(null) + datastore.get(chart.id).should.deepEqual(history) + }) + it('does not store a point when y is a msg property that is missing, whatever msg.payload holds', async function () { const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) await helper.load(nodeImports, flow) const base = helper.getNode('config-ui-base') const widget = base.ui.widgets.get('node-ui-chart') datastore.save(base, helper.getNode('node-ui-chart'), []) - const msg = widget.hooks.beforeSend({ topic: 'a', payload: 7 }) - await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) - datastore.get('node-ui-chart').should.have.length(0) + for (const payload of [[{ y: 7 }], [], 7]) { + const msg = widget.hooks.beforeSend({ topic: 'a', payload }) + await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) + datastore.get('node-ui-chart').should.have.length(0) + should(msg._datapoint).equal(null) + } }) }) diff --git a/ui/src/widgets/ui-chart/UIChart.vue b/ui/src/widgets/ui-chart/UIChart.vue index 06b6ae9f4..9b2e0e7f3 100644 --- a/ui/src/widgets/ui-chart/UIChart.vue +++ b/ui/src/widgets/ui-chart/UIChart.vue @@ -415,6 +415,10 @@ export default { } }, onMsgInput (msg) { + // The server marks messages that carry no chart data. Dynamic updates are handled separately. + if (msg._datapoint === null) { + return + } // ignore if payload is empty and msg is not an array (on loading it can be an array) // ui_update messages are handled by OnDynamicProperties // a msg without payload is still data when the server computed a y value for it (y set to a msg property) From 697077de2e6edc62637b9de24e64194198f41576 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 25 Nov 2026 08:47:11 -0400 Subject: [PATCH 09/10] fix(ui-chart): preserve empty payload clear command --- nodes/widgets/ui_chart.js | 5 +++++ test/nodes/widgets/ui_chart.spec.js | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index 7845d3a25..e07e17fdd 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -132,6 +132,11 @@ module.exports = function (RED) { } function isDataMsg (msg) { + // An empty payload array clears the chart regardless of the configured Y property. + if (Array.isArray(msg.payload) && !msg.payload.length) { + return true + } + return config.yAxisPropertyType === 'msg' ? getConfiguredY(msg) !== undefined : msg.payload !== undefined diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index 20c66e65f..5556cb221 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -141,17 +141,34 @@ describe('ui-chart node', function () { datastore.get(chart.id).should.deepEqual(history) }) - it('does not store a point when y is a msg property that is missing, whatever msg.payload holds', async function () { + it('does not store a point when y is a msg property that is missing and msg.payload is not a clear request', async function () { const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) await helper.load(nodeImports, flow) const base = helper.getNode('config-ui-base') const widget = base.ui.widgets.get('node-ui-chart') datastore.save(base, helper.getNode('node-ui-chart'), []) - for (const payload of [[{ y: 7 }], [], 7]) { + for (const payload of [[{ y: 7 }], 7]) { const msg = widget.hooks.beforeSend({ topic: 'a', payload }) await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) datastore.get('node-ui-chart').should.have.length(0) should(msg._datapoint).equal(null) } }) + + it('clears chart history with an empty payload array when y is a different msg property', async function () { + const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) + await helper.load(nodeImports, flow) + const chart = helper.getNode('node-ui-chart') + const base = helper.getNode('config-ui-base') + const emitter = new EventEmitter() + base.uiShared.connections['chart-test'] = { id: 'chart-test', emit: emitter.emit.bind(emitter) } + const history = [{ _datapoint: { x: 1, y: 3, category: 'a' } }] + datastore.save(base, chart, history) + + const received = once(emitter, 'msg-input:' + chart.id) + chart.receive({ payload: [] }) + const [msg] = await received + datastore.get(chart.id).should.have.length(0) + should(msg._datapoint).deepEqual([]) + }) }) From 695145a07379b566f763d0961760f30aa9695860 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 25 Nov 2026 18:37:42 -0400 Subject: [PATCH 10/10] fix(ui-chart): ignore the hidden y property for JSON series and histograms Co-Authored-By: Claude Sonnet 5.5 --- nodes/widgets/ui_chart.js | 8 +++++--- test/nodes/widgets/ui_chart.spec.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/nodes/widgets/ui_chart.js b/nodes/widgets/ui_chart.js index e07e17fdd..7bdfe2cdf 100644 --- a/nodes/widgets/ui_chart.js +++ b/nodes/widgets/ui_chart.js @@ -120,6 +120,9 @@ module.exports = function (RED) { config.stackSeries = false } + // the editor hides the y property for JSON series and histograms, so any value left in it must be ignored + const yFromMsg = config.yAxisPropertyType === 'msg' && config.categoryType !== 'json' && config.chartType !== 'histogram' + function getConfiguredY (msg) { try { return RED.util.evaluateNodeProperty(config.yAxisProperty, 'msg', node, msg) @@ -137,7 +140,7 @@ module.exports = function (RED) { return true } - return config.yAxisPropertyType === 'msg' + return yFromMsg ? getConfiguredY(msg) !== undefined : msg.payload !== undefined } @@ -164,11 +167,10 @@ module.exports = function (RED) { return msg } - // when y is configured as a msg property, a single value takes y from that property, regardless of + // when y is taken from a msg property, a single value takes y from that property, regardless of // what (or whether) msg.payload holds (an object payload still supplies x and the series). This is // resolved here, before addToChart, which just adds the y it is given to the chart const p = msg.payload - const yFromMsg = config.yAxisPropertyType === 'msg' const msgY = yFromMsg ? getConfiguredY(msg) : undefined const single = yFromMsg && typeof p !== 'object' ? msgY : p diff --git a/test/nodes/widgets/ui_chart.spec.js b/test/nodes/widgets/ui_chart.spec.js index 5556cb221..b2e8a26a0 100644 --- a/test/nodes/widgets/ui_chart.spec.js +++ b/test/nodes/widgets/ui_chart.spec.js @@ -155,6 +155,23 @@ describe('ui-chart node', function () { } }) + it('plots one point per JSON series regardless of the configured y msg property', async function () { + const flow = chartFlow({ category: '["sin","cos"]', categoryType: 'json', yAxisProperty: 'payload.value', yAxisPropertyType: 'msg' }) + await helper.load(nodeImports, flow) + const base = helper.getNode('config-ui-base') + const widget = base.ui.widgets.get('node-ui-chart') + datastore.save(base, helper.getNode('node-ui-chart'), []) + const msg = widget.hooks.beforeSend({ payload: { sin: 5, cos: 8 } }) + await new Promise((resolve) => widget.hooks.onInput(msg, resolve, () => {})) + msg._datapoint.map((d) => [d.category, d.y]).should.deepEqual([['sin', 5], ['cos', 8]]) + datastore.get('node-ui-chart').should.have.length(1) + }) + + it('plots a histogram value regardless of the configured y msg property', async function () { + const dp = await datapointFor({ chartType: 'histogram', xAxisType: 'bins', yAxisProperty: 'value', yAxisPropertyType: 'msg' }, { topic: 'a', payload: 7 }) + dp.y.should.equal(7) + }) + it('clears chart history with an empty payload array when y is a different msg property', async function () { const flow = chartFlow({ yAxisProperty: 'value', yAxisPropertyType: 'msg' }) await helper.load(nodeImports, flow)