Published
Edited
Apr 27, 2020
Fork of Arc diagram
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height));

svg.append("style").text(`

.hover path {
stroke-opacity: 0.05;
}

.hover text {
fill: #ccc;
}

.hover g.primary text {
fill: black;
font-weight: bold;
}

.hover g.secondary text {
fill: #333;
font-weight: bold;
}

.hover path.primary {

stroke-opacity: 1;
}

`);

const label = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(rgraph.nodes)
.join("g")
.attr("transform", d => `translate(${margin.left},${d.y = y(d.Ancestry)})`)
.call(
g => g.append("text")
.attr("x", -10)
.attr("dy", "0.35em")
.attr("fill", d => d3.lab(color(d.Continent)).darker(2))
.text(d => Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(d.Income)+" "+d.Ancestry)
)
.call(g => g.append("circle")
.attr("r", d=> d.Income/15000)
.attr("fill", d => color(d.Continent)));

const path = svg.insert("g", "*")
.attr("fill", "none")
.attr("stroke-opacity", 0.6)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(rgraph.links)
.join("path")
.attr("stroke", d => d.source.Continent === d.target.Continent ? color(d.source.Continent) : "#aaa")
.attr("d", arc);

const overlay = svg.append("g")
.attr("fill", "none")
.attr("pointer-events", "all")
.selectAll("rect")
.data(rgraph.nodes)
.join("rect")
.attr("width", margin.left + 40)
.attr("height", step)
.attr("y", d => y(d.Ancestry) - step / 2)
.on("mouseover", d => {
svg.classed("hover", true);
label.classed("primary", n => n === d);
path.classed("primary", l => l.source === d || l.target === d).filter(".primary").raise();
label.classed("secondary", n => n.sourceLinks.some(l => l.target === d) || n.targetLinks.some(l => l.source === d));

})
.on("mouseout", d => {
svg.classed("hover", false);
label.classed("primary", false);
label.classed("secondary", false);
path.classed("primary", false).order();
})

function update() {
y.domain(rgraph.nodes.sort(viewof order.value).map(d => d.Ancestry));

const t = svg.transition()
.duration(750);

label.transition(t)
.delay((d, i) => i * 20)
.attrTween("transform", d => {
const i = d3.interpolateNumber(d.y, y(d.Ancestry));
return t => `translate(${margin.left},${d.y = i(t)})`;
});

path.transition(t)
.duration(750 + rgraph.nodes.length * 20)
.attrTween("d", d => () => arc(d));

overlay.transition(t)
.delay((d, i) => i * 20)
.attr("y", d => y(d.Ancestry) - step / 2);
}

viewof order.addEventListener("input", update);
invalidation.then(() => viewof order.removeEventListener("input", update));

return svg.node();
}
Insert cell
color = d3.scaleOrdinal(rgraph.nodes.map(d => d.Continent).sort(d3.ascending),
d3.schemeCategory10)
Insert cell
function arc(d) {
const y1 = d.source.y;
const y2 = d.target.y;
const r = Math.abs(y2 - y1) / 2;
return `M${margin.left},${y1}A${r},${r} 0,0,${y1 < y2 ? 1 : 0} ${margin.left},${y2}`;
}
Insert cell
y = d3.scalePoint(rgraph.nodes.map(d => d.Ancestry).sort(d3.ascending), [margin.top, height - margin.bottom])
Insert cell
margin = ({top: 20, right: 0, bottom: 20, left: 200})
Insert cell
height = (rdata.nodes.length - 1) * step + margin.top + margin.bottom
Insert cell
step = 18
Insert cell
rgraph = {
const nodes = rdata.nodes.map(({index, Ancestry, Income, Race, Continent, Race_group}) => ({
index,
Ancestry,
Income,
Race,
Continent,
Race_group,
sourceLinks: [],
targetLinks: []
}));

const nodeByAncestry = new Map(nodes.map(d => [d.Ancestry, d]));

const links = rdata.links.map(({index, source, target, value}) => ({
index,
source: nodeByAncestry.get(source),
target: nodeByAncestry.get(target),
value
}));
for (const link of links) {
const {index, source, target, value} = link;
}

return {nodes, links};
}
Insert cell
rdata = FileAttachment("COO_Income@2.json").json()
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