viewof graph_developer_game = {
const width = 938;
const height = 800;
var selected_name = "";
const color = d3.scaleOrdinal(d3.schemeSet1);
const links = data_graph.links.map(d => ({...d}));
const nodes = data_graph.nodes.map(d => ({...d}));
function limit_min_max(value, min, max){
return Math.max(Math.min(value, max), min);
}
function calculate_size(node){
if(node.group == "Game") return limit_min_max(Math.sqrt(node.popularity)/10, 4, 23)*0.9;
else return limit_min_max(Math.sqrt(node.popularity)/10, 6, 23) + 3;
}
const node_radius = new Map();
data_graph.nodes.forEach(d => { node_radius.set(d.id, calculate_size(d))})
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("x", d3.forceX(10))
.force("y", d3.forceY(10));
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;")
.on("click",
(event, data) => {
event.stopPropagation();
svg.selectAll("circle").attr("stroke-opacity", 1);
svg.selectAll("circle").attr("fill-opacity", 1);
svg.selectAll("text").text("Select a node");
});
const text = svg.append('text')
.attr('x', -400)
.attr('y', -300)
.text('Select a node')
.attr('font-size', '14px')
.attr('fill', 'black');
// 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 => node_radius.get(d.id) )
.attr("fill", d => color(d.group))
.text("amogous")
.on("click", click_function);
function click_function(event, data){
event.stopPropagation();
svg.selectAll("circle").attr("stroke-opacity", 0.2);
svg.selectAll("circle").attr("fill-opacity", 0.2);
svg.selectAll("text").text(data["id"]);
d3.select(this).attr("fill-opacity", 1).attr("stroke-opacity", 1);
}
node.append("title")
.text(d => d.id);
// 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;
}
// 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());
return svg.node();
}