d3.scale.category20()返回相同的颜色

时间:2016-02-15 07:05:28

标签: d3.js

正如文档所述 d3.scale.category20() Constructs a new ordinal scale with a range of twenty categorical colors:。但我得到了相同的颜色。我做错了什么?



var color = '';
for (i = 1; i < 21; i++) {
   var test = d3.scale.category20();
   color += test(i);
}
document.write(color);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您每次都在for循环中创建色阶。

将其移至for循环var colorscale = d3.scale.category20();

之外

var color = '';
var colorscale = d3.scale.category20();
for (i = 1; i < 21; i++) {

   color = colorscale(i);
   document.write("<br>");
   document.write(color);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>