Public
Edited
May 5
Insert cell
Insert cell
nodes = FileAttachment("hallucination_nodes.json").json()

Insert cell
Insert cell
d3 = require("d3@7")

Insert cell
Insert cell
Insert cell
Insert cell
viewof chart = {
const width = 1200;
const height = 800;

const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background", "#f9f9f9");

const colorScale = d3.scaleOrdinal(d3.schemeCategory10);
const sizeScale = d3.scaleLinear()
.domain(d3.extent(nodes, d => d.num_incorrect_answers))
.range([5, 20]);

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(40))
.force("charge", d3.forceManyBody().strength(-50))
.force("center", d3.forceCenter(width / 2, height / 2));

const zoom = d3.zoom().on("zoom", (event) => {
container.attr("transform", event.transform);
});
svg.call(zoom);

const container = svg.append("g");

const link = container.append("g")
.attr("stroke", "#aaa")
.attr("stroke-opacity", 0.4)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", 1);

const node = container.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 1.5)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", d => sizeScale(d.num_incorrect_answers))
.attr("fill", d => colorScale(d.category))
.call(drag(simulation));

const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("background", "#fff")
.style("padding", "10px")
.style("border", "1px solid #ccc")
.style("border-radius", "5px")
.style("box-shadow", "2px 2px 5px rgba(0,0,0,0.1)")
.style("font", "12px sans-serif");

node.on("mouseover", (event, d) => {
tooltip
.html(`<b>Q:</b> ${d.question}<br/><span style="color:red;">❌ ${d.best_incorrect_answer}</span><br/><span style="color:green;">✅ ${d.best_answer}</span>`)
.style("top", (event.pageY + 10) + "px")
.style("left", (event.pageX + 10) + "px")
.style("visibility", "visible");
})
.on("mouseout", () => tooltip.style("visibility", "hidden"));

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);
});

function drag(simulation) {
function dragstarted(event) {
if (!event.active) simulation.alphaTarget(0.3).restart();
event.subject.fx = event.subject.x;
event.subject.fy = event.subject.y;
}
function dragged(event) {
event.subject.fx = event.x;
event.subject.fy = event.y;
}
function dragended(event) {
if (!event.active) simulation.alphaTarget(0);
event.subject.fx = null;
event.subject.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}

return svg.node();
}

Insert cell
staticSVG = chart.cloneNode(true)

Insert cell
html`${staticSVG}`

Insert cell
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