图表的颜色集.js边框颜色



我正在尝试为折线图上的边框设置特定的颜色,因为目前我只能随机化:

var randomColorGenerator = function () { 
    return '#' + (Math.random().toString(16) + '0000000').slice(2, 8); 
};
newDataset = { 
    label: data[i].firstName+' '+data[i].lastName, 
    borderColor: randomColorGenerator(), 
    backgroundColor: "rgba(0,0,0,0)", 
    data: tmpscore, 
}; 

这个想法是拥有一组颜色,因为随机颜色与我创建的主题不匹配,并想知道是否有办法从颜色池中随机选择?

像下面这样的东西应该可以解决问题:

var colorArray = [["#FF4000", false], ["#81BEF7", false], ["#5882FA", false], 
                 ["#04B404", false], ["#A901DB", false], ["#F5A9BC", false]];
// The following makes sure you don't use the same color twice for your datasets
var color;
while (true) {
    var test = colorArray[parseInt(Math.random() * 6)];
    if (!test[1]) {
        color = test[0];
        colorArray[colorArray.indexOf(test)][1] = true;
        break;
    }
}
newDataset = { 
    label: data[i].firstName+' '+data[i].lastName, 
    borderColor: color, 
    backgroundColor: "rgba(0,0,0,0)", 
    data: tmpscore, 
}; 

你基本上创建了一个你想要的颜色数组(适合你的风格),并在你的数据集中随机使用其中一个。

最新更新