我有一個隨機顏色函數,它爲散點圖中的每個圖產生不同的顏色。爲散點圖圖使用固定顏色集
function getRandomColor() {
var letters = 'ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
但是,我該如何給定一個固定的顏色集?
答案更新:
var colors = ['red','green','blue','#2A0A0A','#00FF40','#8A4B08',"#610B21","#0A2229","#5E610B","#3B0B2E","#FF0040","#B43104","#0B6121","#01DFA5","#2E2EFE","#DF01A5","#088A4B","#B40431","#1C1C1C","#B45F04"];
type: 'scatter',
color: function (color, d) {
return colors[d.index];
},
您可以使用預定義的色彩尺度之一:https://github.com/mbostock/d3/wiki/Ordinal-Scales#category20 –