简体   繁体   English

我可以针对d3中的特定节点

[英]can I target a specific node in d3

Is it possible to target nodes with certain key values? 是否可以使用某些键值来定位节点?

The node which has a "category": dataset, I am trying to assign a different color to it. 具有“类别”:数据集的节点,我正在尝试为其分配其他颜色。 I would imagine this is possible. 我想这是可能的。 I am wondering if I could write some sort of if statement like 我想知道我是否可以写一些if语句,例如

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

This is how I am adding color to the nodes now 这就是我现在为节点添加颜色的方式

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);

My JSON looks like this 我的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;
}

You could just assign a different fill when category is present? 当类别存在时,您可以分配其他填充?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM