Published
Edited
Oct 15, 2019
1 fork
1 star
Insert cell
Insert cell
Insert cell
chart = {
const root = treemap(prepped_tree);

const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("font", "10px sans-serif");

const shadow = DOM.uid("shadow");

svg.append("filter")
.attr("id", shadow.id)
.append("feDropShadow")
.attr("flood-opacity", 0.3)
.attr("dx", 0)
.attr("stdDeviation", 3);

const node = svg.selectAll("g")
.data(d3.nest().key(d => d.height).entries(root.descendants()))
.join("g")
.attr("filter", shadow)
.selectAll("g")
.data(d => d.values)
.join("g")
.attr("transform", d => `translate(${d.x0},${d.y0})`);

node.append("title")
.text(d => `${d.ancestors().reverse().map(d => d.data.key).join("/")}\n${format(d.value)}`);
node.filter(d => d.children).append("rect") // for now, filters leaves out of display because too cluttered
.attr("id", d => (d.nodeUid = DOM.uid("node")).id)
.attr("fill", d => color(d.height))
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0)
// ---- Interactivity starts here
.on("mouseover", function(d) {
// console.log(d.data.values);
// Get this bar's x/y values, then augment for the tooltip
// grabbing the x/y values is not working
var xpos = parseFloat(d3.select(this).attr(d.x0))/* + xScale.bandwidth() / 2*/;
var ypos = parseFloat(d3.select(this).attr(d.y0));
// Create the tooltip label as an SVG group with a text and a rect inside
var tgrp = svg.append("g")
.attr("id", "tooltip")
.attr("transform", (d, i) => `translate(${xpos},${ypos})`);
tgrp.append("rect")
// TODO: pass dynamic values to determine tooltip size
// alternatively -- is scrolling tooltip a thing or too complex?
.attr("width", "500px")
.attr("height", "50px")
.attr("fill", "white")
tgrp.append("text")
.attr("x", 5)
.attr("y", 14)
.attr("text-anchor", "left")
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
// .attr("font-weight", "bold")
.attr("fill", "black")
.text(`Artworks in the ${d.parent.data.key} department classified as ${d.data.key}`)
tgrp.append("text")
.attr("x", 5)
.attr("y", 25)
.text(`${artworkList(d.data.values)}`);
// ideally we can format it as an unordered list
// tgrp.append("ul").selectAll('li')
// .data(d.data.values)
// .enter()
// .append('li')
// .html(String);
})
.on("mouseout", function(d) {
// Remove the tooltip
d3.select("#tooltip").remove();
})
;

node.append("clipPath")
.attr("id", d => (d.clipUid = DOM.uid("clip")).id)
.append("use")
// .attr("xlink:href", d => d.nodeUid.href); this is broken, our data is missing .clipUid

node.append("text")
.attr("clip-path", d => d.clipUid) // temp
.selectAll("tspan")
.data(d => {
if (d.height === 0) { // if the data is a leaf
// print the key and value
const artData = []
Object.entries(d.data).forEach(([key, value]) => {
const capitalizedInfoType = key.charAt(0).toUpperCase() + key.slice(1)
artData.push(`${capitalizedInfoType}: ${value}`)
})
return artData
// return Object.values(d.data)
} else { // not a leaf, just print the key
// suuuuper gross conditional because for some reason our tree has both "name" and "key"
return (d.data.name ? d.data.name.split(/(?=[A-Z][^A-Z])/g).concat(format(d.value)) : d.data.key.split(/(?=[A-Z][^A-Z])/g).concat(format(d.value)))
}
})
.join("tspan")
.attr("fill-opacity", (d, i, nodes) => i === nodes.length - 1 ? 0.7 : null)
.text(d => d);

node.filter(d => d.children).selectAll("tspan")
.attr("dx", 3)
.attr("y", 13);

node.filter(d => !d.children).selectAll("tspan")
.attr("x", 3)
.attr("y", (d, i, nodes) => `${(i === nodes.length - 1) * 0.3 + 1.1 + i * 0.9}em`);

return svg.node();
}
Insert cell
function artworkList(d) {
let displayList = []
let listSize = 5
// TODO: ideally i'd write this better/with more parameters but it will have to do
for (let i = 0; i < listSize; i++) {
// TODO: need to catch undefined data
displayList.push(`${d[i].title} by ${d[i].artistname}`)
}
return displayList
}

Insert cell
Insert cell
Insert cell
Insert cell
// just for testing
root = treemap(prepped_tree).children
Insert cell
// // make function to temporarily remove data
// function popData (obj){
// let size = obj["values"].length;
// while(size > 1){ // size of output
// obj["values"].pop();
// size = size - 1;
// }
// return obj;
// }
Insert cell
// popData(prepped_tree)
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
Insert cell
byDepartmentClassification = d3.nest()
.key(d => d.department)
.key(d => d.classification)
.entries(rowdata)
Insert cell
// Pass thingy into hierarchy
hier = d3.hierarchy(prepped_tree, funcChildren)
Insert cell
treemap = data => d3.treemap()
.tile(d3[tile])
.size([width, height])
.padding(1)
.round(true)

(d3.hierarchy(data, funcChildren)
// running .sum in this way will count the nodes, similar to .count
// ideally we want to filter...
// .sum(d => (d.children ? d.parent : 1)).value
// changed sum -> count, counting leaves to generate values
// for now this didn't do much, just changing the output graph a little, rectangles fill up properly
.count(d => 1)
)

Insert cell
Insert cell
// When we run treemap, this function wraps up stuff
// and mutates the input, computing values that
// will be passed into root for the chart all the way above...
// The code below is called in chart but you can remove it from this here.
// This line is only included so we can inspect the output above...
treemap(prepped_tree);
Insert cell
// There is probably an easier way to do this in JS
// but this way uses a constructor
obj = function(name, children) {
this.name = name;
this.values = children; }
Insert cell
prepped_tree = new obj("metStuff", byDepartmentClassification);
Insert cell
funcChildren = d => d.values
Insert cell
funcChildren(prepped_tree)
Insert cell
width = 975
Insert cell
height = 1060
Insert cell
format = d3.format(",d")
Insert cell
color = d3.scaleSequential([8, 0], d3.interpolateViridis)
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