chart = {
const root = treemap(prepped_tree);
let focus = root;
const svg = d3
.create("svg")
.attr("viewBox", [0, 0, width, height])
.style("font", "11px sans-serif");
const nestedTreeData = d3
.nest()
.key(d => d.height)
.entries(root.descendants());
const node = svg
.selectAll("g")
.data(nestedTreeData)
.join("g")
.attr("class", "filterGroup")
.selectAll("g")
.attr("class", d => "groupHeight_" + d.height)
.data(d => d.values)
.join("g")
.attr("class", d => "treeHeight_" + d.height)
.attr("transform", d => `translate(${d.x0},${d.y0})`);
// then we append to the variable labels
const labels = svg
.append("g")
.attr('class', 'nodelabel')
.selectAll("g")
.data(nestedTreeData) // first we start on the highest level up
.join("g")
.attr("class", "labelgroup")
.selectAll("g")
.data(d => d.values) //
.join("g")
.attr("class", d => "treeHeight_" + d.height)
.attr("transform", d => `translate(${d.x0},${d.y0})`);
const labelHandler = d => {
// Hides the label for individual objects
// and only shows labels for classifications and departments...
mutable inspectData = d; // console.log(d);
if (d.height >= 1) {
return d.data.key;
} else {
return '';
}
};
// assigning it to a mutable makes it possible for us to inspect
mutable inspectSelector = labels
.append("text")
.join("text")
.attr("class", d => "treeHeightLabel_" + d.height)
.attr("y", 11)
.text(d => {
return labelHandler(d);
});
node.append("title").text(
d =>
`${d
.ancestors()
.reverse()
.map(d => d.data.key)
.join("/")}\n${format(d.data.title)}`
);
const rect = node
.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) {
// unsure why we have to use "function(d)" for it to work
console.log(this);
console.log(d3.select(this));
d3.select(this).attr("fill", "blue");
mouseoverHelper(d);
})
.on("click", clicked)
.on("mouseout", function(d) {
d3.select(this)
.transition()
.duration(250) // try these
.attr("fill", d => color(d.height));
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
const text = node
.append("text")
.attr("clip-path", d => d.clipUid) // temp
.selectAll("tspan")
.data(d => d)
.join("tspan")
.attr("fill-opacity", (d, i, nodes) =>
i === nodes.length - 1 ? 0.7 : null
)
.text(d => helperTitleNoOverplot(d)); /// TO EMMA: this might be wrong
// node.filter(d => d.children).selectAll("tspan")
// .attr("dx", 3)
// .attr("y", 13);
const tspan = 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`
);
function clicked(p) {
focus = focus === p ? (p = p.parent) : p;
root.each(
d =>
(d.target = {
x0: ((d.x0 - p.x0) / (p.x1 - p.x0)) * height, // calculate the new view
x1: ((d.x1 - p.x0) / (p.x1 - p.x0)) * height,
y0: d.y0 - p.y0,
y1: d.y1 - p.y0
})
);
const t = node
.transition()
.duration(750)
.attr("transform", d => `translate(${d.target.y0},${d.target.x0})`);
rect.transition(t).attr("height", d => rectHeight(d.target));
text.transition(t).attr("fill-opacity", d => +labelVisible(d.target));
tspan.transition(t).attr("fill-opacity", d => labelVisible(d.target) * 0.7);
}
function rectHeight(d) {
return d.x1 - d.x0 - Math.min(1, (d.x1 - d.x0) / 6);
}
function labelVisible(d) {
return d.y1 <= width && d.y0 >= 0 && d.x1 - d.x0 > 4;
}
return svg.node();
}