chart3={
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));
const filteredLinks = links.filter(d => d.weight > treshold);
const filteredNodes = nodes.filter(d => {
return filteredLinks.some(link => link.source === d.id || link.target === d.id);
});
const simulation = d3.forceSimulation(filteredNodes)
.force("link", d3.forceLink(filteredLinks).id(d => d.id).distance(d => 5 * calculateDistance(d)))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2))
.on("tick", ticked);
const colorScale = d3.scaleOrdinal()
.domain([...new Set(filteredLinks.map(d => d.type))])
.range(d3.schemeCategory10);
const link = container.append("g")
.style("stroke-opacity", 0.6)
.selectAll("line")
.data(filteredLinks)
.enter().append("line")
.attr("stroke-width", 3)
.style("stroke", d => {
if (d.type === 'partnership') {
return 'red';
} else {
return '#999';
}
});
const node = container.append("g")
.style("stroke", "#fff")
.style("stroke-width", 1.5)
.selectAll("circle")
.data(filteredNodes)
.enter().append("circle")
.attr("r", 10)
.attr("fill", colores)
.call(drag(simulation))
.on("mouseover", handleMouseOver)
.on("mouseout", handleMouseOut);
const legend = svg.append("g")
.attr("class", "legend")
.selectAll("g")
.data([...new Set(filteredLinks.map(d => d.type))])
.enter()
.append("g")
.attr("transform", (d, i) => "translate(0," + i * 20 + ")");
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", d => {
if (d === 'partnership') {
return 'red';
} else {
return '#999';
}
});
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(d => d);
node.append("title")
.text(d => d.id);
function ticked() {
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);
}
return svg.node();
}