chart = {
const links = l4.map(d => Object.create(d));
const nodes = n4.map(d => Object.create(d));
var div = d3.select("body").append("div");
div.attr("class", "tooltip")
.style("opacity", 0);
const gethtml = (node) => {
return `<div style="z-index: 100000">Usuario: ${node.url.split("/")[3]}</div> <div>rtweets: ${node.retweets}</div> <a href="${node.url}" target="_blank"> ir al tweet</a>`;
}
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.retweets/1))
.attr("fill", d => d["es_quote"] == 1 || d["es_retweet"] == 1? "steelblue" : "firebrick")
.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);
})
.on("click", d => {
window.open(d.url, '_blank');
})
.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();
}