2012-04-07 47 views

回答

2

的源代码是线2997-上d3.v2.js

d3.scale.category20c = function() { 
    return d3.scale.ordinal().range(d3_category20c); 
}; 

这是呼唤:

// lines 2894-2896 
d3.scale.ordinal = function() { 
    return d3_scale_ordinal([], {t: "range", x: []}); 
}; 

,然后调用

// lines 2903-2905 
function scale(x) { 
    return range[((index.get(x) || index.set(x, domain.push(x))) - 1) % range.length]; 
} 
(基于通过了类别

["#3182bd", "#6baed6", "#9ecae1", 
"#c6dbef", "#e6550d", "#fd8d3c", 
"#fdae6b", "#fdd0a2", "#31a354", 
"#74c476", "#a1d99b", "#c7e9c0", 
"#756bb1", "#9e9ac8", "#bcbddc", 
"#dadaeb", "#636363", "#969696", 
"#bdbdbd", "#d9d9d9"] 

通过我看到这样分配的颜色代码步进:

参数传入,x设置为d3_category20c其从下方观察的20种可用的颜色的列表分配颜色在):

cluster = 0 #3182bd 
graph = 1 #6baed6 
optimization = 2 #9ecae1 
animate = 3 #c6dbef 
interpolate = 4 #e6550d 
converters = 5 #fd8d3c 
data = 6 #fdae6b 
display = 7 #fdd0a2 
flex = 8 #31a354 
physics = 9 #74c476 
query = 10 #a1d99b 
methods = 11 #c7e9c0 
scale = 12 #756bb1 
util = 13 #9e9ac8 
heap = 14 #bcbddc 
math = 15 #dadaeb 
palette = 16 #636363 
axis = 17 #969696 
controls = 18 #bdbdbd 
render = 19 #d9d9d9 
events = 0 #3182bd 
legend = 1 #6baed6 
etc... 

注意,它使用mod运算符,因此它可以保持指定颜色,如果在类别通过数超过20种的色彩范围。

请注意,d3_category20c的颜色在category.js的第48-54行中定义。

0

如已经提到的,这样的代码:

var r = 960, 
    format = d3.format(",d"), 
    fill = d3.scale.category20c(); 

定义填充()。我只是想添加以下方便的文件和参考卡组合:

enter image description here