Public
Edited
Jul 7, 2023
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
tree
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
csv_data = d3.csvParse(csvText)
Insert cell
Insert cell
stratified = d3
.stratify()
.id((d) => d.employee)
.parentId((d) => d.supervisor)(csv_data)
Insert cell
Insert cell
graph(d3.hierarchy(stratified))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
stratified_big_south = d3
.stratify()
.id((o) => o.expected_seeds)
.parentId((o) => o.expected_preseeds)(links)
Insert cell
Insert cell
stratified_big_south_pic = graph(d3.hierarchy(stratified_big_south))
Insert cell
Insert cell
games = new Map([
["8-9", "Char So df HP"],
["1-8", "UNCA vs Char So"],
["4-5", "SC Up vs GW"],
["7-10", "Camp df Pres"],
["2-7", "Long vs Camp"],
["3-6", "Rad vs Win"]
])
Insert cell
Insert cell
graph(d3.hierarchy(stratified_big_south), {
label: function (game) {
let expected_seeds = game.data.data.expected_seeds;
if (games.get(expected_seeds)) {
return games.get(expected_seeds);
}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Modified only slightly from
// https://observablehq.com/@d3/d3-hierarchy
function graph(
root,
{ label = (d) => d.data.id, highlight = () => false, marginLeft = 40 } = {}
) {
let dx = 12;
let dy = 120;
let tree = d3
.tree()
.separation(() => 3)
.nodeSize([dx, dy]);
let treeLink = d3
.linkHorizontal()
.x((d) => d.y)
.y((d) => d.x);
root = tree(root);

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 + dx * 2])
.style("overflow", "visible");

const g = svg
.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("transform", `translate(${marginLeft},${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("stroke", (d) =>
highlight(d.source) && highlight(d.target) ? "red" : null
)
.attr("stroke-opacity", (d) =>
highlight(d.source) && highlight(d.target) ? 1 : null
)
.attr("d", treeLink);

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) => (highlight(d) ? "red" : d.children ? "#555" : "#999"))
.attr("r", 2.5);

node
.append("text")
.attr("fill", (d) => (highlight(d) ? "red" : null))
.attr("stroke", "white")
.attr("paint-order", "stroke")
.attr("dy", "0.31em")
.attr("x", (d) => (d.children ? -6 : 6))
.attr("text-anchor", (d) => (d.children ? "end" : "start"))
.text(label);

return svg.node();
}
Insert cell
layout
Insert cell
styles
Insert cell
import { big_south_bracket, styles } from "33ccc404cac487d8"
Insert cell
import { pic, viewof new_tree, layout, tree, size } from "1ec728c5ee86f0f3"
Insert cell
import { viewof expr, parse_tree_pic, graphed } from "ded1d5802242c72d"
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