Bar chart - custom colors


In my Node-RED Open AQ dashboard, I have been using line chart to display air quality measurements.
I wanted to add bar chart, where bars have colors according to "National Air Quality Index"

I found tese discussions regarding bar chart colors:

Config more than 9 colours in bar chart

Bar Graph Colors determined by values

I did not need more that 9 colors, but I wanted to specify color for each bar.
angular-chart (and Chart.js) supports specifying colors, so I tried to include that in payload to input to Bar Chart node.

That did not work. After poking around in node-red-dashboard on GitHub, I found the code section included below in ui-chart-js.js.

The loadConfiguration function and its reference to item.colors caught my attention, and after putting this into msg ui_control, the colors shows up in the bar charr, that is - after a separate browser page refresh:

msg.ui_control = { look : 'bar', colors: colors };

The loadConfiguration function is called upon in the scope watch function, if the newValue is different from oldValue.
So, added a dummy property with a dynamic value, and then the bar chart colors shows up without refreshing page :slight_smile:

msg.ui_control = { look : 'bar', colors: colors, options: { dummy: new Date().getTime() } };

The dashboard is here: Open AQ dashboard


----- ui-chart-js.js code snippets -----

Line 50:
// Watch for any changes to controls

scope.$watchGroup(['me.item.legend','me.item.interpolate','me.item.ymin','me.item.ymax','me.item.xformat','','me.item.cutout','me.item.nodata','me.item.animation','me.item.spanGaps','me.item.options','me.item.look'], function (newValue, oldValue) {
                        type = newValue[11];
                        if (JSON.stringify(newValue) !== JSON.stringify(oldValue)) {
                            scope.config = loadConfiguration(type, scope);

LIne 153
function loadConfiguration

From line 177:
//Build colours array

//Build colours array

 var bColours = item.colors || ['#1F77B4', '#AEC7E8', '#FF7F0E', '#2CA02C', '#98DF8A', '#D62728', '#FF9896', '#9467BD', '#C5B0D5'];
    var baseColours = bColours.concat([
    config.colours = config.colours || baseColours;
    scope.barColours = [];
    scope.lineColours = [];
    baseColours.forEach(function(colour, index) {
            backgroundColor: colour,
            borderColor: colour
            backgroundColor: baseColours,
            borderColor: "#888",
            borderWidth: 1


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