chart = {
const links = l3.map(d => Object.create(d));
const nodes = n3.map(d => Object.create(d));
const nn = nodes.map(n => {
const red = d3.randomInt(1, 256)();
const green = d3.randomInt(1, 256)();
const blue = d3.randomInt(1, 256)();
const alpha = Math.random();
return {
color: d3.rgb(red, green, blue, alpha)
};
});
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
const gethtml = (node) => {
return `<div>Usuario: ${node.data.user.name}</div> <div>followers: ${node.data.user.followers_count}</div>`;
}
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(-140).distanceMax(50).distanceMin(10))
.force("center", d3.forceCenter(width / 2, height / 2));
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", 1);
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => r_scale(d.data.user.followers_count))
.attr("fill", d => color(d))
.on('mouseover', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '.85');
div.transition()
.duration(50)
.style("opacity", 1);
let num = 50;
div.html(gethtml(d))
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 15) + "px");
})
.on('mouseout', function (d, i) {
d3.select(this).transition()
.duration('50')
.attr('opacity', '1');
div.transition()
.duration('50')
.style("opacity", 0);
})
.call(drag(simulation));
simulation.on("tick", () => {
node.attr("cx", function(d) { return d.x = Math.max(5, Math.min(width - 5, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(5, Math.min(height - 5, d.y)); });
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);
});
invalidation.then(() => simulation.stop());
return svg.node();
}