Published
Edited
Feb 8, 2022
7 forks
21 stars
Insert cell
Insert cell
Insert cell
viewof icicle = {
const root = partition(data);
const svg = d3.create("svg");
// Make this into a view, so that the currently hovered sequence is available to the breadcrumb
const element = svg.node();
element.value = { sequence: [], percentage: 0.0 };

svg
.attr("viewBox", `0 0 ${width} ${height}`)
.style("font", "12px sans-serif");

svg
.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", "none");

const segment = svg
.append("g")
.attr("transform", d =>
narrow ? `translate(${-root.y1}, 0)` : `translate(0, ${-root.y1})`
)
.selectAll("rect")
.data(
root.descendants().filter(d => {
// Don't draw the root node, and for efficiency, filter out nodes that would be too small to see
return d.depth && segmentWidth(d) >= 0.1;
})
)
.join("rect")
.attr("fill", d => color(d.data.name))
.attr("x", segmentX)
.attr("y", segmentY)
.attr("width", segmentWidth)
.attr("height", segmentHeight)
.on("mouseenter", (event, d) => {
// Get the ancestors of the current segment, minus the root
const sequence = d
.ancestors()
.reverse()
.slice(1);
// Highlight the ancestors
segment.attr("fill-opacity", node =>
sequence.indexOf(node) >= 0 ? 1.0 : 0.3
);
const percentage = ((100 * d.value) / root.value).toPrecision(3);
// Update the value of this view with the currently hovered sequence and percentage
element.value = { sequence, percentage };
element.dispatchEvent(new CustomEvent("input"));
});

svg.on("mouseleave", () => {
segment.attr("fill-opacity", 1);
// Update the value of this view
element.value = { sequence: [], percentage: 0.0 };
element.dispatchEvent(new CustomEvent("input"));
});

return element;
}
Insert cell
csv = d3.csvParseRows(await FileAttachment("visit-sequences@1.csv").text())
Insert cell
data = buildHierarchy(csv)
Insert cell
partition = data =>
d3
.partition()
.padding(1)
.size(narrow ? [height, width] : [width, height])(
d3
.hierarchy(data)
.sum(d => d.value)
.sort((a, b) => b.value - a.value)
)
Insert cell
color = d3
.scaleOrdinal()
.domain(["home", "product", "search", "account", "other", "end"])
.range(["#5d85cf", "#7c6561", "#da7847", "#6fb971", "#9e70cf", "#bbbbbb"])
Insert cell
targetHeight = 350
Insert cell
narrowHeight = 600
Insert cell
height = narrow ? narrowHeight : targetHeight
Insert cell
narrow = width <= 430
Insert cell
segmentX = d => (narrow ? d.y0 : d.x0)
Insert cell
segmentY = d => (narrow ? d.x0 : d.y0)
Insert cell
segmentWidth = d => (narrow ? d.y1 - d.y0 : d.x1 - d.x0)
Insert cell
segmentHeight = d => (narrow ? d.x1 - d.x0 : d.y1 - d.y0)
Insert cell
function buildHierarchy(csv) {
// Helper function that transforms the given CSV into a hierarchical format.
const root = { name: "root", children: [] };
for (let i = 0; i < csv.length; i++) {
const sequence = csv[i][0];
const size = +csv[i][1];
if (isNaN(size)) {
// e.g. if this is a header row
continue;
}
const parts = sequence.split("-");
let currentNode = root;
for (let j = 0; j < parts.length; j++) {
const children = currentNode["children"];
const nodeName = parts[j];
let childNode = null;
if (j + 1 < parts.length) {
// Not yet at the end of the sequence; move down the tree.
let foundChild = false;
for (let k = 0; k < children.length; k++) {
if (children[k]["name"] == nodeName) {
childNode = children[k];
foundChild = true;
break;
}
}
// If we don't already have a child node for this branch, create it.
if (!foundChild) {
childNode = { name: nodeName, children: [] };
children.push(childNode);
}
currentNode = childNode;
} else {
// Reached the end of the sequence; create a leaf node.
childNode = { name: nodeName, value: size };
children.push(childNode);
}
}
}
return root;
}
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more