Public
Edited
Aug 29, 2023
2 forks
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = treemap
Insert cell
Insert cell
zoom = d3.zoom()
.translateExtent([[ 0, 0 ],[ width, height ]])
.scaleExtent([ 1, 20 ])
.on('zoom', (d,i) => {

d3.select( chart )
.select('.treemap-container')
.attr('transform', d3.event.transform)

revealText()
})
Insert cell
Insert cell
d3.select( chart )
.select('g.treemap-container')
.call( zoom )
Insert cell
revealText = () => {
const leaf = d3.select(treemap).selectAll('g.leaf')

leaf.each((d, i, arr) => {

// The current leaf element
const current = arr[i]

const left = d.x0,
right = d.x1,
// calculate its width from the data
width = (right - left) * d3.event.transform.k,
top = d.y0,
bottom = d.y1,
// calculate its height from the data
height = (d.y1 - d.y0) * d3.event.transform.k

// Check if it's too small for text
const tooSmall = width < 34 || height < 25

d3.select( current ).select('text')
.attr('opacity', tooSmall ? 0 : 0.9)

})

}
Insert cell
Insert cell
Insert cell
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