A minimal and powerful treemap visualization library for creating interactive hierarchical data visualizations.
- π Lightweight: Minimal bundle size with maximum performance
- π¨ Customizable: Rich theming and styling options
- π Plugin System: Extensible architecture with built-in plugins
- π± Responsive: Automatic resizing
- β‘ Interactive: Built-in zoom, drag, highlight, and menu interactions
- π― TypeScript: Full type safety and excellent DX
npm install squarified
# or
yarn add squarified
# or
pnpm add squarified
import { createTreemap, c2m, sortChildrenByKey } from 'squarified'
// Create a treemap instance
const treemap = createTreemap({
plugins: [
// Add built-in plugins for interactions
]
})
// Initialize with a DOM element
const container = document.querySelector('#treemap-container')
treemap.init(container)
// Set your data
treemap.setOptions({
data: [
{
id: 'root',
label: 'Root',
weight: 100,
groups: [
{ id: 'child1', label: 'Child 1', weight: 60 },
{ id: 'child2', label: 'Child 2', weight: 40 }
]
}
]
})
import {
createTreemap,
c2m,
sortChildrenByKey,
presetColorPlugin,
presetZoomablePlugin,
presetHighlightPlugin,
presetDragElementPlugin,
presetScalePlugin,
presetMenuPlugin
} from 'squarified'
// Create treemap with plugins
const treemap = createTreemap({
plugins: [
presetColorPlugin,
presetZoomablePlugin,
presetHighlightPlugin,
presetDragElementPlugin,
presetScalePlugin(),
presetMenuPlugin({
style: {
borderRadius: '5px',
padding: '6px 12px',
backgroundColor: 'rgba(0, 0, 0, 0.8)',
color: 'white',
cursor: 'pointer',
userSelect: 'none'
},
render: () => [
{ html: '<span>π Zoom In</span>', action: 'zoom' },
{ html: '<span>π Reset</span>', action: 'reset' }
],
onClick(action, module) {
switch (action) {
case 'zoom':
if (module?.node.id) {
treemap.zoom(module.node.id)
}
break
case 'reset':
treemap.resize()
break
}
}
})
]
})
// Convert and prepare data
async function loadData() {
const response = await fetch('/api/data')
const rawData = await response.json()
// Transform data structure
const convertedData = rawData.map(item => ({
...item,
groups: item.children?.map(child => convertChildrenToGroups(child))
}))
// Convert to treemap format and sort
const treemapData = sortChildrenByKey(
convertedData.map(item =>
c2m(item, 'value', d => ({
...d,
id: d.path,
label: d.name
}))
),
'weight'
)
return treemapData
}
// Initialize
const container = document.querySelector('#app')
treemap.init(container)
// Load and set data
loadData().then(data => {
treemap.setOptions({ data })
})
// Handle events
treemap.on('click', (event, module) => {
console.log('Clicked:', module?.node)
})
// Auto-resize on container changes
new ResizeObserver(() => {
treemap.resize()
}).observe(container)
Creates a new treemap instance.
interface CreateTreemapOptions {
plugins?: Plugin[]
}
Initialize the treemap with a DOM container.
Update treemap configuration and data.
Manually trigger a resize recalculation.
Subscribe to treemap events.
Clean up the treemap instance.
interface TreemapNode {
id: string
label: string
weight: number
groups?: TreemapNode[]
// Custom properties...
}
Convert hierarchical data to treemap format.
Sort treemap nodes by a specific key.
- presetColorPlugin: Automatic color assignment
- presetZoomablePlugin: Zoom interactions
- presetHighlightPlugin: Hover highlighting
- presetDragElementPlugin: Drag interactions
- presetScalePlugin: Scaling controls
- presetMenuPlugin: Context menus
- Chrome/Edge 80+
- Firefox 75+
- Safari 13+
Squarified is optimized for performance with:
- Canvas-based rendering
- Efficient layout algorithms
- Smart redraw optimizations
- Memory-conscious design
Contributions are welcome! Please read our contributing guide for details.
Kanno
Algorithm ported from esbuild Bundle Size Analyzer by Evan Wallace, refactored and optimized for general-purpose treemap visualization.