viewof chart = {
const width = 928;
const height = 680;
let selectedNode = null;
const nodeStroke = '#fff';
const nodeStrokeWidth = 1.5;
const nodeStrokeOpacity = 1;
const linkOpacity = 0.6;
const nodeDeselectOpacity = 0.4;
const linkDeselectOpacity = 0.2;
const nodeHightlightStroke = '#ffd700';
const nodeHightlightStrokeWidth = 2.0;
const links = data.links.map(d => ({...d}));
const nodes = data.nodes.map(d => ({...d}));
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("x", d3.forceX())
.force("y", d3.forceY());
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height])
.attr("style", "max-width: 100%; height: auto;");
svg.on('click', deselect_node);
// Add a line for each link, and a circle for each node.
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => radius.get(d.id) * 3)
.attr("fill", d => color(d.group))
.on('click', clicked);
node.append("title")
.text(d => {
if(d.group == "Movie") {
return `${d.id}`
}
else{
return `${d.id}: ${radius.get(d.id) - 1} movies`;
}
});
// Add a drag behavior.
node.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
// Set the position attributes of links and nodes each time the simulation ticks.
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node
.attr("cx", d => d.x)
.attr("cy", d => d.y);
});
// Reheat the simulation when drag starts, and fix the subject position.
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
// Update the subject (dragged node) position during drag.
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
// Restore the target alpha so the simulation cools after dragging ends.
// Unfix the subject position now that it’s no longer being dragged.
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
function clicked(event, d) {
event.stopPropagation();
const allNodes = svg.selectAll('circle')
.attr('stroke', nodeStroke)
.attr('stroke-width', nodeStrokeWidth)
.attr('stroke-opacity', nodeDeselectOpacity)
.attr('fill-opacity', nodeDeselectOpacity);
const allLinks = svg.selectAll('line')
.attr('stroke-opacity', linkDeselectOpacity);
const currentNode = d3.select(this)
.attr('stroke', nodeHightlightStroke)
.attr('stroke-width', nodeHightlightStrokeWidth)
.attr('stroke-opacity', nodeStrokeOpacity)
.attr('fill-opacity', nodeStrokeOpacity);
selectedNode = d;
svg.property('value', Object.assign(svg.node(),
{ selectedNode: d })).dispatch('input');
}
function deselect_node(event) {
const allNodes = svg.selectAll('circle')
.attr('stroke', nodeStroke)
.attr('stroke-width', nodeStrokeWidth)
.attr('stroke-opacity', nodeStrokeOpacity)
.attr('fill-opacity', nodeStrokeOpacity);
const allLinks = svg.selectAll('line')
.attr('stroke-opacity', linkOpacity);
selectedNode = null;
svg.property('value', Object.assign(svg.node(),
{ selectedNode: null })).dispatch('input');
}
// When this cell is re-run, stop the previous simulation. (This doesn’t
// really matter since the target alpha is zero and the simulation will
// stop naturally, but it’s a good practice.)
invalidation.then(() => simulation.stop());
deselect_node();
return Object.assign(svg.node(), { selectedNode: null });
}