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")
.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)
.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();
}