Bar Graph Colors determined by values

Loving the dashboard, many thanks for the work gone into making it.

With regards to bar graphs, would it be possible to control the color of the bar with the value being passed into it, in a similar way to the min, seg1, seg2, and max variables work with the gauge graph?

For example I'm using the dashboard bar graphs to monitor battery levels and it would be great to color the bar green for good, amber for low, and red for depleted.

If this can already be achieved via code I'd love to know how.

Thanks again.


No easy way - though there was a discussion fairly recently on how to use a template to achieve it.
I think it may have been this one Battery gauge load msg.payload into javascript [Resolved]

Also see this post Led icon changed

Also see this post Method to interact bewteen node-red/JS/HTML/CSS ui_template

Below example flow uses this code to achieve what you want. You can modify it to add how many color segments you want as well as the associated colors.

let pay = msg.payload;
let col = "lime";

switch (true) {
    case (pay >70):
        col = "lime";
    case (pay >50):
        col = "orange";
    case (pay >0):
        col = "red";

msg.color = col;
return msg;
[{"id":"e0816f8b.d98e6","type":"tab","label":"SOC - 3 - Simplified - without scope.$watch","disabled":false,"info":""},{"id":"a7d3387d.fa7518","type":"ui_template","z":"e0816f8b.d98e6","group":"9145e585.5ef2e8","name":"Batt_NEW","order":0,"width":0,"height":0,"format":"<!DOCTYPE html>\n<html>\n<head>\n\n<style>\n\n/*Initial Code By Mickael Novotny*/\n\n:root {\n --batt_lev: {{msg.payload}}%;\n --batt_col: {{msg.color}};\n}\n\nbody {\n padding: 25px 50px;\n}\n\n.battery {\n/* Battery outerwall*/\n border: 5px solid #333;\n width: 278px;\n height: 108px;\n padding: 4px;\n border-radius: 2px;\n position: relative;\n margin: 15px 0;\n} \n\n\n.battery::before {\n/* Battery Pole */\n content: '';\n height: 90px;\n width: 16px;\n background: #333;\n display: block;\n position: absolute;\n top: 7.5px;\n right: -16px;\n border-radius: 0 4px 4px 0;\n}\n\n.battery::after {\n/* Battery inner margins*/\n content: '';\n display: block;\n position: absolute;\n top: -3px;\n left: -3px;\n right: -3px;\n bottom: -3px;\n border: 6px solid #fff;\n border-radius: 2px;\n}\n\n-->\n\n.battery-level {\n/* battery level*/\n background: var(--batt_col, blue); \n position: absolute;\n top: 0px;\n bottom: 0px;\n width: var(--batt_lev, 50%);\n left: 0%;\n}\n\n.battery-text{\n z-index:99;\n position: absolute;\n font-size:32px;\n top: 20px;\n left:120px;\n color:'';\n}\n\n.warn {\n background-color: #efaf13;\n}\n \n.alert {\n background-color: #e81309;\n} \n\n.alert::after {\n background-image: url('data:image/svg+xml;charset=US-ASCII,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%3C!'); \n background-repeat: no-repeat;\n background-size: 18px;\n height: 18px;\n width: 18px;\n margin: 6px 0 0 8px;\n content: '';\n display: inline-block;\n position: absolute;\n}\n\n</style>\n</head>\n\n<body>\n\n<div class=\"battery\">\n <div class=\"battery-level\"></div>\n <div class=\"battery-text\"><p><span id=\"myMsg\">{{msg.payload}}</span>%</p></div>\n</div>\n\n</body>\n\n</html>","storeOutMessages":true,"fwdInMessages":true,"templateScope":"local","x":650,"y":180,"wires":[[]]},{"id":"4f78d16a.b9de9","type":"inject","z":"e0816f8b.d98e6","name":"Trigger","topic":"","payload":"","payloadType":"date","repeat":"0.9","crontab":"","once":true,"onceDelay":0.1,"x":140,"y":180,"wires":[["1e94b315.e8ea7d"]]},{"id":"1e94b315.e8ea7d","type":"change","z":"e0816f8b.d98e6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload%100","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":180,"wires":[["7d5ca2fe.e0561c"]]},{"id":"3da31f26.c9d51","type":"comment","z":"e0816f8b.d98e6","name":"Modified: removed scope.$watch","info":"","x":170,"y":120,"wires":[]},{"id":"7d5ca2fe.e0561c","type":"function","z":"e0816f8b.d98e6","name":"Set color","func":"let pay = msg.payload;\nlet col = \"lime\";\n\nswitch (true) {\n    case (pay >70):\n        col = \"lime\";\n        break;\n    case (pay >50):\n        col = \"orange\";\n        break; \n    case (pay >0):\n        col = \"red\";\n        break;\n}\n\nmsg.color = col;\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":180,"wires":[["a7d3387d.fa7518"]]},{"id":"9145e585.5ef2e8","type":"ui_group","z":"","name":"Car Data","tab":"a2516971.840328","order":3,"disp":true,"width":"12","collapse":false},{"id":"a2516971.840328","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

EDIT: in order to work properly this flow should be configured with Theme = Light and configured to use "Angular theme in ui_template"


1 Like