Skip to content

Commit

Permalink
Component | Treemap: Misc cleanup, towards labels
Browse files Browse the repository at this point in the history
  • Loading branch information
curran committed Jan 3, 2025
1 parent 8c49b2c commit d41b469
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
5 changes: 4 additions & 1 deletion packages/dev/src/examples/misc/treemap/basic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const component = (): JSX.Element => {
(d: TreemapExampleDatum) => d.group,
(d: TreemapExampleDatum) => d.name,
]}
tileLabel={(node: TreemapNode<TreemapExampleDatum>) => node.data.datum?.name}
tileLabel={(node: TreemapNode<TreemapExampleDatum>) => {
// console.log(node)
return node.data.datum?.name
}}
padding={24}
/>
</VisSingleContainer>
Expand Down
17 changes: 6 additions & 11 deletions packages/ts/src/components/treemap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,15 @@ export class Treemap<Datum> extends ComponentCore<Datum[], TreemapConfigInterfac

const treemapData = treemapLayout(rootNode) as TreemapNode<Datum>

// Get max depth by traversing the tree
const maxDepth = max(treemapData.descendants().map(d => d.depth))
const descendants = treemapData.descendants()

// Then update the opacity scale domain to use maxDepth
const opacity = scaleLinear()
.domain([1, maxDepth])
.domain([1, max(descendants, d => d.depth)])
.range([1, 0.2])

treemapData
.eachBefore((node) => {
if (!node.children) return

node.children.forEach((child, i) => {
const treemapChild = child as TreemapNode<Datum>
const color = getColor(treemapChild, config.tileColor, i, treemapChild.depth !== 1)
Expand All @@ -71,13 +68,11 @@ export class Treemap<Datum> extends ComponentCore<Datum[], TreemapConfigInterfac
})
})

const nodes = treemapData.descendants()
.filter(d => d.depth > 0)

// Render tiles
const tiles = this.tiles.selectAll<SVGGElement, TreemapNode<Datum>>('g')
.data(nodes, d => d._id)

const visibleNodes = descendants.filter(d => d.depth > 0)
const tiles = this.tiles
.selectAll<SVGGElement, TreemapNode<Datum>>('g')
.data(visibleNodes, d => d._id)
const tilesEnter = tiles.enter().append('g')

// Background rectangles
Expand Down

0 comments on commit d41b469

Please sign in to comment.