question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic background color (callback)

See original GitHub issue

Hi there,

I got a case, where I need to dynamically set colors of single bars depending on the data value.

I would suggest to use a callback like this:

datasets: [{
  label: '# of Votes',
  data: [ 10, 21, 22 ],
  backgroundColor: function( value ) { // Callback instead of static color 
    if ( value < 30 ) {
      return 'red'; 
    }
    return 'green';
  }
}]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
haschucommented, May 16, 2016

Turns out that, after diving more into it, using the plugin system is way more elegant.

For anybody who comes across the same problem, this does the job:

    Chart.pluginService.register({
        beforeUpdate: function( chartInstance ) {
            chartInstance.data.datasets.forEach( function( dataset ) {
                dataset.backgroundColor = dataset.data.map( function( data ) {
                    if ( typeof dataset.backgroundColor === "function" ) {
                        return dataset.backgroundColor.call( this, data );
                    }
                    return dataset.backgroundColor;
                });
            });
        }
    });
3reactions
haschucommented, Feb 27, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically set backgroundColor and borderColor ...
So all you need to do is iterate over your dynamic data and build your data, backgroundColor, and borderColor arrays per your logic...
Read more >
Learn a simple way to dynamically change the background ...
Learn a simple way to dynamically change the background color of your webpage without any fancy frameworks. What you will need: Text Editor;...
Read more >
Dynamic colors - JavaScript Video Tutorial
Each button will represent a different color and we'll change the background color of the document body to the color value. Using a...
Read more >
How to Make Background Color Range Dynamic ... - YouTube
How to Make Background Color Range Dynamic With Changing X Scale in Chart JSIn this video we will explore how to make background...
Read more >
Client script to change the background color of a field
incident.company'). I will also suggest doing the script in an "onChange", as that will make it work dynamically when you change the value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found