Bar chart formatting

I'll have done this topic to the death shortly.
I have 10 charts on a UI page, each with the same code powering them just with a different payload variable gotten from the global context, in the picture below you can see that each is rendering the graph slightly differently.

The top one shows where the zero value is being drawn in red, this is not desirable, the middle one shows how I would like the graphs to render, the positive topic hasn't rendered a zero even though it was passed one. the bottom one shows them kind of sitting inside each other, this is acceptable but less than ideal as it is still noticeable.

Anyone have any suggestions to setting that can maybe nail them all down to do the same.
Code passed to the charts for the curious:

ts = msg.timestamp
pl = global.get("LiveChart.S1Dif")

if (pl < 0){
    pl2 = pl
    pl1 = null
} else {
    pl2 = null
    pl1 = pl
}
ui= {
    
    options: {
        scales: {
            xAxes: [{
                stacked: true,
                ticks: {
                    autoSkip: true,
                    maxTicksLimit: 12
                    }
            }],
            yAxes: [{
                stacked: true,
                ticks: {
                    max: 60,
                    min: -140
                }
            }]
        }
    }
}
//series 1 #green
msg = {
    payload: pl1,
    timestamp: ts,
    topic: "high",
    ui_control: ui
},

//series 2 #red
msg2 = {
    payload: pl2,
    timestamp: ts,
    topic: "low",
    ui_control: ui
}

if (pl > 0){
    return msg
}
else {
    return msg2
}

After further research and many failed attempts I've found that the extra colour is generated from the borders, even if the value is zero it had borders on some of the charts (for some reason).

The code below shows an example of removed borders and some other style tweaks.

ui= {
    options: {
        scales: {
            xAxes: [{
                stacked: true,
                ticks: {
                    autoSkip: true,
                    maxTicksLimit: 12
                    }
            }],
            yAxes: [{
                stacked: true,
                gridLines:{
                  zeroLineWidth: 1,
                  zeroLineColor: 'rgba(255, 255, 255, 1)'
                },
                ticks: {
                    max: 60,
                    min: -140
                }
            }]
        },
      elements: {
        rectangle: {
          borderWidth: 0
        }
      }
    }
}

image

Hello,

I have been following your chart-a-thon out of a general need to "learn new stuff".

Initially when I tested the bar chart, it has no "live" X axis... which seemed very odd.

I figured out how to use another node to "fix" that, but have never determined how you got both positive and negative values to display on the same chart.

Nor have I figured out where you put the above code to fix your display issues.. although I did notice the same on the negative part of my dual chart "solution" :stuck_out_tongue:

image

Can you share a bit more on how you even got the chart to look the way you did? Thanks.

Absolutely, the main reason I left these up was incase another could use the info.
Bare with me and feel free to ask me to clarify on any points.

So, my flow looks like this for each chart:
image
the top inject is a minute timestamp inject to fetch the updated payload from the context.
the function code is this:

ts = msg.timestamp
pl = global.get("LiveChart.S2Dif")

if (pl < 0){
    pl2 = pl
    pl1 = null
} else {
    pl2 = null
    pl1 = pl
}
ui= {
    options: {
        scales: {
            xAxes: [{
                stacked: true,
                ticks: {
                    autoSkip: true,
                    maxTicksLimit: 12
                    }
            }],
            yAxes: [{
                stacked: true,
                gridLines:{
                  zeroLineWidth: 1,
                  zeroLineColor: 'rgba(255, 255, 255, 1)'
                },
                ticks: {
                    max: 60,
                    min: -140
                }
            }]
        },
      elements: {
        rectangle: {
          borderWidth: 0
        }
      }
    }
}
//series 1 #green
msg = {
    payload: pl1,
    timestamp: ts,
    topic: "high",
    ui_control: ui
},

//series 2 #red
msg2 = {
    payload: pl2,
    timestamp: ts,
    topic: "low",
    ui_control: ui
}

if (pl > 0){
    return msg
}
else {
    return msg2
}

next you can see I have a chart preformatter node. its actually a custom modified version of node-red-contrib-dashboard-bar-chart-data to include more features that I required.
finally we have the chart node. nothing special here.
the two persist nodes (above and below) save the last message going to the chart so when I restart a flow or the machine its running on I don't lose any data, the bottom persist node is triggered on a one-hit when the flow starts and this provides the chart formatter with the last recorded data so the chart continues from that.

The gist of the two values on one chart is that each data set has a different topic, I use 'low' and high' these are then fixed to the first and second colours in the chart config page which allows me to use green for positive values and red for negative.

Hope this is clear

2 Likes

Thanks... that helped me understand it better.

Although I wouldn't have thought that the function node would work with script that doesn't quite look like either CSS or Javascript. Not sure if I would have figured out that one anytime soon without the assist :stuck_out_tongue:

But I managed to get my test working fine. Even a 0 that looks like a proper blank spot.

image

[{"id":"c29c5f4.b37daa","type":"function","z":"43641b25.40b524","name":"","func":"ts = msg.timestamp\npl = msg.randomdata\n\nif (pl < 0){\n    pl2 = pl\n    pl1 = null\n} else {\n    pl2 = null\n    pl1 = pl\n}\nui= {\n    options: {\n        scales: {\n            xAxes: [{\n                stacked: true,\n                ticks: {\n                    autoSkip: true,\n                    maxTicksLimit: 12\n                    }\n            }],\n            yAxes: [{\n                stacked: true,\n                gridLines:{\n                  zeroLineWidth: 0,\n                  zeroLineColor: 'rgba(0, 0, 0, 1)'\n                },\n                ticks: {\n                    max: 100,\n                    min: -100\n                }\n            }]\n        },\n      elements: {\n        rectangle: {\n          borderWidth: 0\n        }\n      }\n    }\n}\n//series 1 #green\nmsg = {\n    payload: pl1,\n    timestamp: ts,\n    topic: \"high\",\n    ui_control: ui\n},\n\n//series 2 #red\nmsg2 = {\n    payload: pl2,\n    timestamp: ts,\n    topic: \"low\",\n    ui_control: ui\n}\n\nif (pl > 0){\n    return msg\n}\nelse {\n    return msg2\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":860,"wires":[["1625fbb9.6fb504"]]},{"id":"9211bc27.470fc","type":"inject","z":"43641b25.40b524","name":"","props":[{"p":"timestamp","v":"","vt":"date"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":710,"y":860,"wires":[["c03c8451.e815a8"]]},{"id":"1625fbb9.6fb504","type":"bar-chart-data","z":"43641b25.40b524","name":"bar-chart-data","x_interval":"seconds","x_size":24,"unit":"","precision":"0","is_meter_reading":"False","agg_by":"sum","x":1140,"y":860,"wires":[["e6372ac7.3234e8"]]},{"id":"e6372ac7.3234e8","type":"ui_chart","z":"43641b25.40b524","name":"","group":"80716354.3609","order":2,"width":0,"height":0,"label":"","chartType":"bar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":true,"useUTC":false,"colors":["#d71d1d","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":1290,"y":860,"wires":[[]]},{"id":"c03c8451.e815a8","type":"random","z":"43641b25.40b524","name":"","low":"-100","high":"100","inte":"true","property":"randomdata","x":840,"y":860,"wires":[["c29c5f4.b37daa"]]},{"id":"80716354.3609","type":"ui_group","name":"Charts","tab":"bb93b1da.ab821","order":5,"disp":true,"width":"6","collapse":false},{"id":"bb93b1da.ab821","type":"ui_tab","name":"Tests","icon":"dashboard","disabled":false,"hidden":false}]
2 Likes

I'm glad my struggles managed to help at least one person

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.