Published
Edited
Aug 21, 2019
1 star
Insert cell
Insert cell
Insert cell
viewof r = html`<input type="range" min="1" max="100" step="1">`
Insert cell
chart = {
replay;

const nodes = pack().leaves();

const simulation = d3.forceSimulation(nodes)
.force("center", d3.forceCenter(width / 2, height / 2))
.force("x", d3.forceX(width / 2).strength(0.01))
.force("y", d3.forceY(height / 2).strength(0.01))
.force("cluster", forceCluster())
.force("collide", forceCollide());

const svg = d3.select(DOM.svg(width, height));

const node = svg.append("g")
.selectAll("circle")
.data(nodes)
.join("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("fill", d => odss[d.data.text].color)
.call(drag(simulation)).on('click', (d,i) => switchRadius(400, i)());
node.transition()
.delay((d, i) => Math.random() * 500)
.duration(750)
.attrTween("r", d => {
const i = d3.interpolate(0, d.r);
return t => d.r = i(t);
});

simulation.on("tick", () => {
node
.attr("cx", d => d.x)
.attr("cy", d => d.y)
});


function switchRadius(newRadius, index) {
return function() {
d3.selectAll('.node')
.filter(function(d,i) { return i === index; })
.transition().duration(1000)
.tween('r', function(d) {
var that = d3.select(this);
var i = d3.interpolate(d.r, newRadius);
return function(t) {
d.radius = i(t);
that.attr('r', function(d) { return d.r; });
simulation.nodes(data)
}
});
simulation.alpha(1).restart();
}
}

function clicked(d, i) {
if (d3.event.defaultPrevented) return; // dragged
d3.select(this).transition()
.attr("r", d=> d.r * 2);
//switchRadius(300, i);
}

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

return svg.node();
}
Insert cell
Insert cell
function forceCluster() {
const strength = 0.2;
let nodes;

function force(alpha) {
const centroids = d3.rollup(nodes, centroid, d => d.data.group);
const l = alpha * strength;
for (const d of nodes) {
const {x: cx, y: cy} = centroids.get(d.data.group);
d.vx -= (d.x - cx) * l;
d.vy -= (d.y - cy) * l;
}
}

force.initialize = _ => nodes = _;

return force;
}
Insert cell
d3.rollup(pack().leaves(), centroid, d => d.data.group).get(1);
Insert cell
Insert cell
function forceCollide() {
const alpha = 0.4; // fixed for greater rigidity!
const padding1 = 2; // separation between same-color nodes
const padding2 = 6; // separation between different-color nodes
let nodes;
let maxRadius;

function force() {
const quadtree = d3.quadtree(nodes, d => d.x, d => d.y);
for (const d of nodes) {
const r = d.r + maxRadius;
const nx1 = d.x - r, ny1 = d.y - r;
const nx2 = d.x + r, ny2 = d.y + r;
quadtree.visit((q, x1, y1, x2, y2) => {
if (!q.length) do {
if (q.data !== d) {
const r = d.r + q.data.r + (d.data.group === q.data.data.group ? padding1 : padding2);
let x = d.x - q.data.x, y = d.y - q.data.y, l = Math.hypot(x, y);
if (l < r) {
l = (l - r) / l * alpha;
d.x -= x *= l, d.y -= y *= l;
q.data.x += x, q.data.y += y;
}
}
} while (q = q.next);
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
});
}
}

force.initialize = _ => maxRadius = d3.max(nodes = _, d => d.r) + Math.max(padding1, padding2);

return force;
}
Insert cell
pack().leaves();
Insert cell
pack = () => d3.pack()
.size([width, height])
.padding(1)
(d3.hierarchy(data)
.sum(d => d.value))
Insert cell
data = ({
children: Array.from(
d3.group(histograma, d => d.group),
([, children]) => ({children})
)
})
Insert cell
function centroid(nodes) {
let x = 0;
let y = 0;
let z = 0;
for (const d of nodes) {
let k = d.r ** 2;
x += d.x * k;
y += d.y * k;
z += k;
}
return {x: x / z, y: y / z};
}
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
n = 200 // number of nodes
Insert cell
m = 10 // number of groups
Insert cell
color = d3.scaleOrdinal(d3.range(m), d3.schemeCategory10)
Insert cell
height = 600
Insert cell
histograma = await d3.json("https://raw.githubusercontent.com/whatevercamps/graph_jsons_tw_unfpa/master/histograma_ods.json").then(res => {return res.items.map(d => {d.group = 1; d.value = d.count; return d})})
Insert cell
odss = await d3.json('https://raw.githubusercontent.com/whatevercamps/graph_jsons_tw_unfpa/master/ods.json')
Insert cell
d3 = require("d3@5", "d3-array@2")
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