2013-02-08 37 views

回答

4

你可以只添加属性fill改变颜色:

 
node.append("circle") 
     .attr("r", function(d) { return d.r; }) 
     .style("fill", function(d){ return d.color; }); 

在上面的例子中,假设你的数据包含color场。

+1

这是我工作的:.style(“fill”,function(d){return color(d.packageName);});不知道这是否与你的建议不同,即使用.style vs .attr – nilanjan

+0

事实上''fill''是在css样式表中设置的,因此它是一种样式,所以你必须用'style'命令而不是'attr'。我相应地编辑了我的答案。 –

+0

我相信你必须首先将颜色设置到节点上。我的解决方案包括这里的变化:函数递归(名称,节点)if(node.children)node.children.forEach(function(child){recurse(node.name,child); }); 其他classes.push(包名称: className:node.name, value:node.size, color:node.color }); } http://stackoverflow.com/questions/11788105/d3-circle-pack-setting-circle-colors/36407165#36407165 – seokhoonlee