我可以在d3中定位特定节点吗?

时间:2013-06-20 19:07:44

标签: json d3.js

是否可以使用某些键值来定位节点?

具有“类别”:数据集的节点,我正在尝试为其指定不同的颜色。我想这是可能的。我想知道我是否可以编写某种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;
}

1 个答案:

答案 0 :(得分:0)

您可以在类别出现时指定不同的填充吗?

d3.selectAll('path').data(inNodes.children).enter()
    .append('path')
    .doOtherStuffToCreateYourPath()
    .attr('fill', function (d) {
        return (d.category === 'DataSet' ? 'red' : 'defaultColor');
    });