d3顺序量表中的映射 [英] Mapping in d3 ordinal scales
本文介绍了d3顺序量表中的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用D3的序数刻度,将数字映射到颜色。
我使用这个:
I am using D3's ordinal scale, to map numbers to color. I have used this:
color = d3.scale.ordinal().range(["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2"]).domain([0,6]);
console.log(color(0),color(1),color(2),color(3),color(4),color(5),color(6));
我得到的答案是:
#1f77b4#2ca02c#d62728#9467bd#8c564b#e377c2#ff7f0e
我很好奇,回应是:
#1f77b4,#ff7f0e,#2ca02c,#d62728,#9467bd,#8c564b,#e377c2
请指教。
推荐答案
右侧:
> color.domain()
[0, 6, 1, 0.5]
在有序标度范围中,域中应该有一个匹配值。
For each value in an ordinal scale's range, there should to be a matching value in the domain.
而不是将域设置为 [0,6]
,应该是 0,1,2,3,4,5,6]
:
Instead of setting the domain to [0,6]
, it should be [0, 1, 2, 3, 4, 5, 6]
:
color = d3.scale.ordinal()
.range(["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2"])
.domain(d3.range(0,7));
检查以确保更新颜色后一切正常:
Checking to make sure everything works after updating color:
> d3.range(0,7).forEach(function(d){ console.log(color(d)); })
#1f77b4
#ff7f0e
#2ca02c
#d62728
#9467bd
#8c564b
#e377c2
这篇关于d3顺序量表中的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文