Reputation: 417
I am attempting to send in a set of IDs to my method that builds my Force Directed map. I loop through the nodes and I want to set the fill color of the nodes that are selected from the parent id. I have a list on the page where when you select an item from it it will highlight the additional nodes by matching the color. I can't seem to get the fill color right. Any help is greatly appreciated.
<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>
Upvotes: 2
Views: 1466
Reputation: 417
wound up having to select the instance of the d3 element I was working with. Code looks like this now
<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>
Upvotes: 3