Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random fill colors in Chart.js

I have been using nvd3 for a long time. In nvd3 we have an option to specify automatic graph fill colors.

chart.barColor()

How can I fill random colors in Chart.js graphs without defining each color in datasets?

I don't want to use JavaScript function to generate and get random colors from it. I need something similar to nvd3 barColor()

If there is a possible way, then please help me out.

like image 688
Aditya Chaudhary Avatar asked Jul 06 '15 10:07

Aditya Chaudhary


2 Answers

        function getRandomColor() {
            var letters = '0123456789ABCDEF'.split('');
            var color = '#';
            for (var i = 0; i < 6; i++ ) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
                }

Then set:

fillcolor = getRandomColor()
like image 76
Silvan Vogel Avatar answered Sep 24 '22 13:09

Silvan Vogel


I am afraid there just is no in-built function in chart.js library for doing this. And what is the harm in defining your own javascript function anyways?

The implementation would look pretty much similar to what you are looking for, except that you would have defined what barColor() would do yourself.

If you haven't found them already, there are a couple of great solutions here. (using JavaScript functions)

like image 39
Swanky Coder Avatar answered Sep 21 '22 13:09

Swanky Coder