outline/border of graph elements using strokes
In response to stackoverflow question: http://stackoverflow.com/questions/10375255/how-to-create-an-outline-for-selected-edges-and-vertices-in-d3-js/10393179#10393179
by jsl6906
HTML
<script src="http://mbostock.github.com/d3/d3.v2.js"></script>
CSS
circle {
stroke-width: 1.5px;
}
line {
stroke: #999;
}
JavaScript
var w = 400,
h = 350,
r = 6,
fill = d3.scale.category20();
var force = d3.layout.force().charge(-120).linkDistance(30).size([w, h]);
var svg = d3.select("body").append("svg:svg").attr("width", w).attr("height", h);
d3.json("http://bl.ocks.org/d/1129492/readme.json", function(json) {
var linkoutline = svg.selectAll(".outline").data(json.links).enter().append("svg:line").attr("class","outline").style("stroke","red").style("stroke-width",20).style("stroke-linecap","round");
var linkback = svg.selectAll(".backline").data(json.links).enter().append("svg:line").attr("class","backline").style("stroke","white").style("stroke-width",18).style("stroke-linecap","round");
var link = svg.selectAll(".mainline").data(json.links).enter().append("svg:line").attr("class","mainline").style("stroke","black").style("stroke-width",1).style("stroke-linecap","round");
var node = svg.selectAll("circle").data(json.nodes).enter().append("svg:circle").attr("r", r - .75).style("fill", function(d) {
return fill(d.group);
}).style("stroke", function(d) {
return d3.rgb(fill(d.group)).darker();
}).call(force.drag).on("mouseover", fade(.1)).on("mouseout", fade(1));;
force.nodes(json.nodes).links(json.links).on("tick", tick).start();
var linkedByIndex = {};
json.links.forEach(function(d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
function isConnected(a, b) {
return linkedByIndex[a.index + "," + b.index] || linkedByIndex[b.index + "," + a.index] || a.index == b.index;
}
function tick() {
node.attr("cx", function(d) {
return d.x = Math.max(r, Math.min(w - r, d.x));
}).attr("cy", function(d) {
return d.y = Math.max(r, Math.min(h - r, d.y));
});
linkoutline.attr("x1", function(d) {
return d.source.x;
}).attr("y1", function(d) {
return d.source.y;
}).attr("x2", function(d) {
...