Published
Edited
May 7, 2020
Fork of Hierarchies
2 forks
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md `## tree`
Insert cell
tree = data => {
const root = d3.hierarchy(data);
root.dx = 15;
root.dy = width / (root.height + 1);
return d3.tree().nodeSize([root.dx, root.dy])(root);
}
Insert cell
chart = {
const root = tree(countryDataSunburst);

let x0 = Infinity;
let x1 = -x0;
root.each(d => {
if (d.x > x1) x1 = d.x;
if (d.x < x0) x0 = d.x;
});

const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, x1 - x0 + root.dx * 2]);

const g = svg
.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${root.dy / 3},${root.dx - x0})`);

const link = g
.append("g")
.attr("fill", "none")
.attr("stroke", "#555")
.attr("stroke-opacity", 0.4)
.attr("stroke-width", 1.5)
.selectAll("path")
.data(root.links())
.join("path")
.attr(
"d",
d3
.linkHorizontal()
.x(d => d.y)
.y(d => d.x)
);

const node = g
.append("g")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 3)
.selectAll("g")
.data(root.descendants())
.join("g")
.attr("transform", d => `translate(${d.y},${d.x})`);

node
.append("circle")
.attr("fill", d => (d.children ? "#555" : "#999"))
.attr("r", 2.5);

node
.append("text")
.attr("dy", "0.31em")
.attr("x", d => (d.children ? -6 : 6))
.attr("text-anchor", d => (d.children ? "end" : "start"))
.text(d => d.data.name)
.clone(true)
.lower()
.attr("stroke", "white");

return svg.node();
}
Insert cell
md`## sunburst`
Insert cell
sunburstChart
Insert cell
md`## treemap`
Insert cell
viewof tile = {
const options = [
{ name: "d3.treemapBinary", value: d3.treemapBinary },
{ name: "d3.treemapDice", value: d3.treemapDice },
{ name: "d3.treemapSlice", value: d3.treemapSlice },
{ name: "d3.treemapSliceDice", value: d3.treemapSliceDice },
{ name: "d3.treemapSquarify", value: d3.treemapSquarify, selected: true }
];
const form = html`<form style="display: flex; align-items: center; min-height: 33px;"><select name=i>${options.map(
o =>
Object.assign(html`<option>`, {
textContent: o.name,
selected: o.selected
})
)}`;
form.i.onchange = () => form.dispatchEvent(new CustomEvent("input"));
form.oninput = () => (form.value = options[form.i.selectedIndex].value);
form.oninput();
return form;
}
Insert cell
treemapChart
Insert cell
md`## Circle packing`
Insert cell
circlePackingChart
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
countryData = FileAttachment("countryHierarchy.json").json()
Insert cell
countryDataSunburst = Object.assign({}, countryData)
Insert cell
function idToName(node) {
let tmp = node.data.id;
node.name = tmp;
if (node.children != null) {
node.children.forEach(idToName);
}
}
Insert cell
idToName(countryDataSunburst)
Insert cell
countryDataSunburst
Insert cell
radius = width / 2
Insert cell
d3 = require("d3@5")
Insert cell
customSunburstParition = data => d3.partition()
.size([2 * Math.PI, radius])
(d3.hierarchy(data)
.sum(d => 1))
Insert cell
import { chart as sunburstChart } with {
countryDataSunburst as data,
customSunburstParition as partition
} from '@d3/sunburst'
Insert cell
treeMapheight = 954
Insert cell
customTreemap = data =>
d3
.treemap()
.tile(tile)
.size([width, treeMapheight])
.padding(1)
.round(true)(d3.hierarchy(data).sum(d => 1))
Insert cell
import { chart as treemapChart } with {
countryDataSunburst as data,
customTreemap as treemap
} from '@d3/treemap'
Insert cell
circlePackingHeight = width
Insert cell
customPack = data =>
d3
.pack()
.size([width, circlePackingHeight])
.padding(3)(
d3
.hierarchy(data)
.sum(d => 1)
.sort((a, b) => b.name - a.name)
)
Insert cell
import { chart as circlePackingChart } with {
countryDataSunburst as data,
customPack as pack
} from '@d3/zoomable-circle-packing'
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