Random fill colors in Chart.js

2019-04-25 17:43发布

问题:

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.

回答1:

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)



回答2:

        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()