Public
Edited
Aug 29, 2023
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create('svg')
.attr('width', 500)
.attr('height', 500)
const hierarchy = d3.hierarchy(data)
.sum(d => d.value)
const pack = d3.pack()
.size([ 500, 500 ])
.padding( 3 )
const nodeData = pack( hierarchy ).leaves()
const color = d3.scaleOrdinal().range(d3.schemeSet2)
// all of our nodes, used to handle click events
let nodes
showAll()
return svg.node()
function showAll() {
const t = svg.transition()
.duration(1000)
nodes = svg.selectAll('g')
.data( nodeData, d => d.data.name )
.join(
enter => enter
.append('g')
.attr('class', d => 'depth-' + d.depth)
.attr('transform', d => `translate(${ d.x },${ d.y})`)
.style('opacity', 0)
.call(g => g.append('circle')
.attr('r', d => d.r)
.style('fill', d => color(d.data.group))
)
.call(g => g
.append('text')
.style('font-family', 'sans-serif')
.style('font-size', 10)
.attr('dy', '0.35em')
.attr('text-anchor', 'middle')
.text(d => d.data.name)
)
.call(g => g
.transition(t)
.style('opacity', 1)
),
update => update
.call(g => g
.transition(t)
.attr('transform', d => `translate(${ d.x },${ d.y})`)
)
.call(g => g
.select('circle')
.transition(t)
.attr('r', d => d.r)
)
.call(g => g
.select('text')
.transition(t)
.attr('x', 0)
),
exit => exit.remove()
).on('click', showSelected)

return nodes
}
function showSelected(e,d) {

const filteredData = nodeData.filter(node => node.data.group == d.data.group )
const t = svg.transition()
.duration(1000)

nodes
.data( filteredData, d => d.data.name )
.join(
enter => enter,
update => update
.call(g => g
.transition(t)
.attr('transform', (d,i) => `translate(${ 20 },${ 20 + i * 50 })`)
)
.call(g => g
.select('circle')
.transition(t)
.attr('r', 15)
)
.call(g => g
.select('text')
.transition(t)
.attr('x', 50)
)
.on('click', showAll),
exit => exit
.transition()
.style('opacity', 0)
.remove()
)

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