Published
Edited
Oct 9, 2019
Insert cell
Insert cell
Insert cell
// links = d3.csvParse(')
rowdata = d3.csv("https://gist.githubusercontent.com/rl2999/e15594889ee650f0a7ffe238d3c03f0e/raw/1edf08ce77b0a600c16f109a75fef3916926a572/met_highlights_dept_class.csv")

Insert cell
md` ## Nesting
Let's nest our data by department, then classification... so far it looks OK...
`
Insert cell
byDepartmentClassification = d3.nest()
.key(d => d.department)
.key(d => d.classification)
.entries(rowdata)
Insert cell
obj = function(name, children) {
this.name = name;
this.values = children; }
Insert cell
thingy = new obj("metStuff", byDepartmentClassification);
Insert cell
md`# // ASK: why does this output not have any children?... it should???
`
Insert cell
funcChildren = d => d.values
Insert cell
// make into hierarchy

hier = d3.hierarchy(
thingy, funcChildren) // ok this children accessor doesnt work
Insert cell
Insert cell
// Another chart demo
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("font", "14px sans-serif");

// Load in the root of the data...
const root = treemapLayout();
// create a d3 layout with params
var treemapLayout = d3.treemap()
.size([width, height])
.paddingOuter(50)
(root);
treemap = data => d3.treemap()
.size([width, height])
.paddingOuter(50)
// run sum to get the value count based on descendants
// root.sum(d => d.values);
//root.sum(function(d) { return d.value ? 1 : 0; });
// return root;
// do some idiosyncratic imperative function
// to mutate the root... :/
// treemapLayout(root);
// var root_shifted = root.descendants();
svg.selectAll('g rect')
.data(root.leaves())
.enter()
.append('rect')
.attr('x', d => d.x0)
.attr('y', d => d.y0)
.attr('width', d => d.x1 - d.x0)
.attr('height', d => d.y1 - d.y0)
.attr('fill', 'pink')
.style("stroke", "#000")
.attr('opacity', '0.35')
svg.selectAll('g rect')
.data(root.leaves())
.enter()
.append('text')
.text(d => d.data.country)
.attr('dx', d => d.x0)
.attr('dy', d => d.y0)
var nodes = svg.selectAll('g rect')
.selectAll('g')
.data(root.leaves())
.enter()
.append('g')
.attr('transform', d => 'translate(' + [d.x0, d.y0] + ')');

nodes
.append('rect')
.attr('width', function(d) { return d.x1 - d.x0; })
.attr('height', function(d) { return d.y1 - d.y0; });

nodes
.append('text')
.attr('dx', 4)
.attr('dy', 14)
.text(d => d.data.department);
svg.append('text').text('Treemap of Met data by Department')
.attr('x', 20)
.attr('y',40)
.style('font-weight', '600')
.style('font-size', '2rem')
return svg.node();
}
// https://www.d3indepth.com/layouts/
Insert cell
height = 800;
Insert cell
width = 975
Insert cell
color = d3.scaleOrdinal(d3.schemeCategory10)
Insert cell
d3 = require("d3@5", "d3-array")
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