Published
Edited
Apr 12, 2020
Insert cell
md`# Covid19 cases in Myanmar`
Insert cell
chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));
const fcolor = "#4e79a7";

const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id).distance(50))
.force("charge", d3.forceManyBody().strength(-100))
.force("x", d3.forceX())
.force("y", d3.forceY());

const svg = d3.create("svg")
.attr("viewBox", [-width / 2, -height / 2, width, height]);

const link = svg.append("g")
.attr("stroke", fcolor)
.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", 12)
.attr("fill", d => Number.isInteger(d.id) ? fcolor : "#c7bbbb")
.call(drag(simulation));
node.on('click', d => {
console.log('findind id: ' + d.id);
const connectedNodes = findConnectedNodes(d.id, []);
console.log('result: ', connectedNodes);
svg.selectAll("circle")
.classed("selected", (c) => Number.isInteger(c.id) && connectedNodes.indexOf(c.id) > -1);
svg.selectAll("line")
.classed("selected",
(c) => {
const lk = data.links[c.index];
return connectedNodes.indexOf(lk.source) > -1 || connectedNodes.indexOf(lk.target) > -1;
}, {passive: true});
});

const label = svg.append("g")
.selectAll("text")
.data(nodes)
.join("text")
.attr("font-size", 10)
.attr("fill", "white")
.attr("pointer-events", "none")
.text(d => d.id)
.call(drag(simulation));

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);
label
.attr("dx", d => d.x - (d.id < 10 ? 3 : 5))
.attr("dy", d => d.y + 3);
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
html`<style>
circle { transition: fill .4s ease; }
.selected {
fill: #ff7f0e; stroke: #ff7f0e;

}
</style>
`
Insert cell
findConnectedNodes(1, [])
Insert cell
function findConnectedNodes(id, curNodes){
curNodes.push(id);
const linkedNodeIds = data.links
.filter(l => l.source === id || l.target === id)
.map(l => l.source === id ? l.target : l.source);
const newNodeIds = linkedNodeIds.filter(i => curNodes.indexOf(i) < 0);
if (newNodeIds.length > 0) {
newNodeIds.forEach(ni => {
findConnectedNodes(ni, curNodes);
});
}
return curNodes;
}
Insert cell
data = FileAttachment("graph-1@2.json").json()
Insert cell
height = 680
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@5")
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