chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', 'white')
.on('click', function() {
d3.selectAll('.link').style('stroke-opacity', '0.6');
d3.selectAll('.node').style('opacity', '1');
});
var container = svg.append('g');
svg.call(zoom(container));
var link = container.append("g")
.attr("class", "links")
.selectAll("line");
var node = container.append("g")
.attr("class", "nodes")
.selectAll("circle");
var linkedByIndex = {};
json.links.forEach(function(d) {
linkedByIndex[d.source.index + ',' + d.target.index] = 1;
linkedByIndex[d.target.index + ',' + d.source.index] = 1;
});
function neighboring(a, b) {
return linkedByIndex[a.source.index + ',' + b.source.index];
}
return Object.assign(svg.node(), {
update(nodes,links) {
link = link.data(links, function(d) { return d.source.index + ", " + d.target.index;});
link.exit().remove();
var linkEnter = link.enter().append("line")
.attr('class', 'link')
.attr('stroke', 'gray');
link = linkEnter.merge(link);
node = node.data(nodes);
node.exit().remove();
var nodeEnter = node.enter().append("circle")
// Use degree centrality from NetworkX in json.
.attr('r', function(d, i) { return degreeSize(d.degree); })
// Color by group, a result of modularity calculation in NetworkX.
.attr("fill", function(d) { return color(d.group); })
.attr('class', 'node')
// On click, toggle ego networks for the selected node.
.on('click', function(d, i) {
// Ternary operator restyles links and nodes if they are adjacent.
d3.selectAll('.link').style('stroke-opacity', function (l) {
return l.target == d || l.source == d ? 1 : 0.1;
});
d3.selectAll('.node').style('opacity', function (n) {
return neighboring(d, n) ? 1 : 0.1;
});
d3.select(this).style('opacity', 1);
})
.call(drag(simulation));
node = nodeEnter.merge(node);
node.append("title")
.text(function(d) { return d.name; });
simulation
.nodes(nodes)
.on("tick", ticked);
simulation.force("link")
.links(links);
simulation.alpha(1).restart();
function ticked() {
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; });
}
}
});
}