javascript Chart.js 中的随机填充颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31243892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random fill colors in Chart.js
提问by Aditya Chaudhary
I have been using nvd3 for a long time. In nvd3 we have an option to specify automatic graph fill colors.
我已经使用 nvd3 很长时间了。在 nvd3 中,我们有一个选项来指定自动图形填充颜色。
chart.barColor()
How can I fill random colors in Chart.js graphs without defining each color in datasets?
如何在不定义数据集中的每种颜色的情况下在 Chart.js 图中填充随机颜色?
I don't want to use JavaScript function to generate and get random colors from it. I need something similar to nvd3 barColor()
我不想使用 JavaScript 函数来生成并从中获取随机颜色。我需要类似于 nvd3 的东西barColor()
If there is a possible way, then please help me out.
如果有可能的方法,那么请帮助我。
回答by Silvan Vogel
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()
回答by Swanky Coder
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?
恐怕chart.js 库中没有内置函数来执行此操作。无论如何定义自己的javascript函数有什么危害?
The implementation would look pretty much similar to what you are looking for, except that you would have defined what barColor()
would do yourself.
该实现看起来与您正在寻找的非常相似,只是您已经定义了barColor()
自己会做什么。
If you haven't found them already, there are a couple of great solutions here. (using JavaScript functions)
如果你还没有找到它们了,有几个伟大的解决方案在这里。(使用 JavaScript 函数)