D3.js D3强制定向贴图有条件地设置节点的填充颜色

D3.js D3强制定向贴图有条件地设置节点的填充颜色,d3.js,colors,force-layout,D3.js,Colors,Force Layout,我正在尝试向我的方法发送一组ID,该方法用于构建我的部队定向地图。我循环遍历节点,并希望设置从父id中选择的节点的填充颜色。我在页面上有一个列表,当您从中选择项目时,它将通过匹配颜色高亮显示其他节点。我的填充颜色好像不对。非常感谢您的帮助 <code> function buildAllForceDirectedMultiMap(node, ids) { d3.select("svg").remove(); var width = 960, hei

我正在尝试向我的方法发送一组ID,该方法用于构建我的部队定向地图。我循环遍历节点,并希望设置从父id中选择的节点的填充颜色。我在页面上有一个列表,当您从中选择项目时,它将通过匹配颜色高亮显示其他节点。我的填充颜色好像不对。非常感谢您的帮助

<code>
function buildAllForceDirectedMultiMap(node, ids) {

    d3.select("svg").remove();

    var width = 960,
        height = 500;

    var svg = d3.select(node).append("svg")
        .attr("width", width)
        .attr("height", height);    

    var color = d3.scale.category20();

    var force = d3.layout.force()
        .charge(-120)
        .linkDistance(30)
        .size([width, height]);

    var tip = d3.tip()
        .attr('class', 'd3-tip')
        .offset([-10, 0])
        .html(function (d) {
            return "<strong>DPID:</strong> <span style='color:red'>" + d.dpid + "</span><br />" + "<strong>Type:</strong> <span style='color:red'>" + d.type + "</span>";
        })      

        svg.call(tip);

    d3.json("http://atxapps.com/apps/data/onos/dataGetFDAllInstances.php", function(error, graph) {
      force
          .nodes(graph.nodes)
          .links(graph.links)
          .start();

      var link = svg.selectAll(".link")
        .data(graph.links)
        .enter().append("line")
            .attr("class", "link")
            .style("stroke-width", function(d) { return Math.sqrt(d.value); });

      var node = svg.selectAll(".node")
            .data(graph.nodes)
            .enter().append("circle")
                .attr("class", function(d) { 
                                    if(d.type == undefined) {
                                        return "node"; 
                                    } else {
                                        return d.type; 
                                    }})
                .attr("r", function(d) { 
                                    if(d.type == undefined) {
                                        return 5; 
                                    } else {
                                        switch(d.type) {
                                            case "core":
                                                return 10;
                                                break;
                                            case "agg":
                                                return 8;
                                                break;
                                            default:
                                                return 5;
                                            } 
                                    }})
                .style("fill", function(d) {                                    
                                    var count = ids.length;
                                    for (i = 0; i <= count; i++) {
                                        if(ids[i] != undefined) {
                                            if(ids[i].attributes.id == d.instance_id) {
                                                return d.color;
                                            } else {
                                                return "#2a2a2a";
                                            }}
                                        }
                                        return d.color;                                         
                                    }
                                )
                .on('mouseover', tip.show)
                .on('mouseout', tip.hide)
                .call(force.drag)
                .on("click", function (d) {
                    //alert(d.dpid);
                });             

      node.append("title")
          .text(function(d) { return d.name; });

      force.on("tick", function() {
        link.attr("x1", function(d) { return d.source.x; })
            .attr("y1", function(d) { return d.source.y; })
            .attr("x2", function(d) { return d.target.x; })
            .attr("y2", function(d) { return d.target.y; });

        node.attr("cx", function(d) { return d.x; })
            .attr("cy", function(d) { return d.y; });
      });
    });
}
</code>
<pre>
                .style("fill", function(d) {                                    
                                    var count = ids.length;
                                    var color = "rgb(0, 0, 0)";
                                    if(d3.select(this).style("fill") == color){
                                        for (i = 0; i <= count; i++) {
                                            if(ids[i] != undefined) {
                                                    if(ids[i].attributes.id == d.instance_id) {
                                                        color = d.color;
                                                    } 
                                                } 
                                            }
                                            return color;                                       
                                        }
                                    }
                                )
</pre>

函数buildAllForceDirectedMultiMap(节点,ID){
d3.选择(“svg”).remove();
可变宽度=960,
高度=500;
var svg=d3.select(node).append(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var color=d3.scale.category20();
var-force=d3.layout.force()
。收费(-120)
.linkDistance(30)
.尺寸([宽度、高度]);
var tip=d3.tip()
.attr('class','d3 tip')
.偏移量([-10,0])
.html(函数(d){
返回“DPID:”+d.DPID+”
“+”类型:“+d.Type+”; }) svg.call(tip); d3.json(“http://atxapps.com/apps/data/onos/dataGetFDAllInstances.php,函数(错误,图形){ 力 .nodes(图.nodes) .links(graph.links) .start(); var link=svg.selectAll(“.link”) .数据(图表.链接) .enter().append(“行”) .attr(“类”、“链接”) .style(“笔划宽度”,函数(d){return Math.sqrt(d.value);}); var node=svg.selectAll(“.node”) .数据(图.节点) .enter().append(“圆”) .attr(“类”,函数(d){ if(d.type==未定义){ 返回“节点”; }否则{ 返回d.type; }}) .attr(“r”,函数(d){ if(d.type==未定义){ 返回5; }否则{ 开关(d型){ 案例“核心”: 返回10; 打破 案例“agg”: 返回8; 打破 违约: 返回5; } }}) .style(“填充”,函数(d){ var count=ids.length;
对于(i=0;i我必须选择我正在使用的d3元素的实例


.style(“填充”,函数(d){
var count=ids.length;
var color=“rgb(0,0,0)”;
如果(d3.选择(此).style(“填充”)==颜色){
对于(i=0;i