<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Icicle Plot Example</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
const width = 600;
const height = 400;
const data = {
name: 'Root',
children: [
{
name: 'Category A',
value: 10,
children: [
{ name: 'Subcategory 1', value: 5 },
{ name: 'Subcategory 2', value: 3 },
{ name: 'Subcategory 3', value: 2 }
]
},
{
name: 'Category B',
value: 12,
children: [
{ name: 'Subcategory 4', value: 7 },
{ name: 'Subcategory 5', value: 5 }
]
},
{
name: 'Category C',
value: 6,
children: [
{ name: 'Subcategory 6', value: 4 },
{ name: 'Subcategory 7', value: 2 }
]
}
]
};
const root = d3.hierarchy(data);
root.sum(d => d.value);
const color = d3.scaleOrdinal(d3.schemeCategory10);
const layout = d3.cluster()
.size([height, width - 160])
.separation((a, b) => (a.parent === b.parent ? 1 : 2) / a.depth);
layout(root);
const svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height)
.style('font', '10px sans-serif');
const linkGroup = svg.append('g')
.selectAll('path')
.data(root.links())
.enter().append('path')
.attr('d', d => `
M${d.source.y},${d.source.x}
L${d.target.y},${d.target.x}
`)
.attr('fill', 'none')
.attr('stroke', '#555')
.attr('stroke-opacity', 0.4)
.attr('stroke-width', d => Math.sqrt(d.source.value));
const nodeGroup = svg.append('g')
.selectAll('g')
.data(root.descendants())
.enter().append('g')
.attr('transform', d => `translate(${d.y},${d.x})`);
const nodeRect = nodeGroup.append('rect')
.attr('fill', d => color(d.depth))
.attr('width', d => d.dy)
.attr('height', d => d.dx)
.attr('stroke', '#fff')
.attr('stroke-width', 1)
.on