0

是否可以针对具有某些键值的节点?

具有“类别”的节点:数据集,我正在尝试为其分配不同的颜色。我想这是可能的。我想知道我是否可以编写某种 if 语句,例如

if(d.node.category ==dataset){
 d3.select(this).style("fill", "yellow");
}

这就是我现在向节点添加颜色的方式

var z = d3.scale.category10();

var node = svg.selectAll("circle.node")
      .data(nodes)
    .enter().append("svg:circle")
      .attr("r", function(d){return d.size*2})
      .attr('class', 'generalClass')
      .style("fill", function(d) { return z(d.parent); })
      .style("stroke", "#000")
      .style("stroke-width", "4px")
      .call(force.drag);

我的 JSON 看起来像这样

function getNodes() {

var inNodes = {
    "name": "APP",
    "dept": "NYC",
    "children": [
        {
        "name": "API 1",
        "dept": "Third Party",
        "size": 15,
        "url": "http://www.nytimes.com/"
        },
        {
        "name": "API 2",
        "dept": "NYC",
        "size": 15,
        "url": "https://www.google.com/"
        },
        {
        "name": "Dataset",
        "category": "Dataset", // this one I am trying to assign a diff color fill
        "dept": "Third Party",
        "size": 15,
        "url": "http://www.wired.com/",
        }
    ],
    "size":20,
    "url": "http://www.nyc.gov/html/index.html"
};
return inNodes;
}
4

1 回答 1

0

当类别存在时,您可以分配不同的填充吗?

d3.selectAll('path').data(inNodes.children).enter()
    .append('path')
    .doOtherStuffToCreateYourPath()
    .attr('fill', function (d) {
        return (d.category === 'DataSet' ? 'red' : 'defaultColor');
    });
于 2013-06-20T19:27:18.550 回答