Public
Edited
May 10
Insert cell
Insert cell
chart = {
// Specify the dimensions of the chart.
const width = 1200;
const height = 800;

// Define fixed colors for each category.
const colorScale = {
area: "#1f77b4", // Azul
pessoa: "#ff7f0e", // Laranja
projeto: "#2ca02c" // Verde
};

// The force simulation mutates links and nodes, so create a copy
// so that re-evaluating this cell produces the same result.
const links = data.links.map(d => ({...d}));
const nodes = data.nodes.map(d => ({...d}));

// Create a simulation with several forces.
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(80)) // Distância mais curta
.force("charge", d3.forceManyBody().strength(-200)) // Menos repulsão
.force("center", d3.forceCenter(0, 0))
.force("collision", d3.forceCollide().radius(25)) // Ajuste para nós mais próximos
.force("x", d3.forceX().strength(0.05))
.force("y", d3.forceY().strength(0.05));

// Create the SVG container with zoom and pan support.
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; cursor: grab; background-color: #f0f0f0;")
.call(d3.zoom()
.scaleExtent([0.2, 3]) // Limites de zoom (20% a 300%)
.on("zoom", (event) => {
g.attr("transform", event.transform);
}))
.on("dblclick.zoom", null);

// Create a group for zoomable content.
const g = svg.append("g");

// Add a line for each link.
const link = g.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", 2);

// Add nodes with text.
const node = g.append("g")
.selectAll("g")
.data(nodes)
.join("g")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

// Add circles for nodes.
node.append("circle")
.attr("r", d => d.type === "area" ? 20 : (d.type === "pessoa" ? 12 : 15))
.attr("fill", d => colorScale[d.type] || "#ccc")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5);

// Add text labels for each node.
node.append("text")
.attr("x", 12)
.attr("y", 4)
.attr("font-size", d => d.type === "area" ? "14px" : "12px")
.attr("fill", "#333")
.text(d => d.id)
.style("pointer-events", "none"); // Prevent text from blocking drag

// 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("transform", d => `translate(${d.x},${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.
invalidation.then(() => simulation.stop());

return svg.node();
}

Insert cell
data = FileAttachment("graph@5.json").json()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more