Published
Edited
Nov 26, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const links = data.links.map(d => Object.create(d));
const nodes = data.nodes.map(d => Object.create(d));

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

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

const link = svg
.append("g")
.attr("stroke-opacity", 0.1)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke", "#93d")
.attr("stroke-width", d => Math.sqrt(d.distance) * 0.02);

const node = svg
.append("g")
.attr("stroke", "#ccc")
.attr("stroke-width", 1)
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("r", 10)
.attr("fill", (d, i) => colorScale(data.nodes[i].id))
.call(drag(simulation));

var texts = svg
.selectAll(".texts")
.data(nodes)
.join("text")
.attr("dy", -12)
.attr("font-family", "sans-serif")
.attr('font-size', '14px')
.attr('text-anchor', 'middle')
.text(d => d.id);

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

texts.attr("x", d => d.x).attr("y", d => d.y);
});

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

return svg.node();
}
Insert cell
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);
}
Insert cell
Insert cell
//// x = d3.scaleBand()
// .rangeRound([margin.left, width-margin.right])
// .domain(d3.range(dataMatrix.length))
// .paddingInner(0.1)
// .align(0);

Insert cell

// opacity = d3.scaleLinear()
// .domain([0, d3.max(dataMatrix, function (d) { return d3.max(d.connections); })])
// .range([0.25, 1])
// .clamp(true);

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
matrix = {
let nodes = cities;
for (let i in nodes) {
nodes[i].connections = new Array(nodes.length).fill(0);
}
for (let j in data.links) {
let source = data.links[j].source;
let target = data.links[j].target;
nodes[source].connections[target] = data.links[j].dist;
}
return;
}
Insert cell
Insert cell
Insert cell
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