|
29 | 29 | .sum((d) => d.value) |
30 | 30 | .sort(sortFunc('value', 'desc')); |
31 | 31 |
|
| 32 | + const simpleRoot = hierarchy({ |
| 33 | + name: 'root', |
| 34 | + children: [ |
| 35 | + { name: 'A', value: 1000 }, |
| 36 | + { name: 'B', value: 900 }, |
| 37 | + { name: 'C', value: 800 }, |
| 38 | + { name: 'D', value: 700 }, |
| 39 | + { name: 'E', value: 600 }, |
| 40 | + { name: 'F', value: 500 }, |
| 41 | + { name: 'G', value: 400 }, |
| 42 | + { name: 'H', value: 300 }, |
| 43 | + { name: 'I', value: 200 }, |
| 44 | + { name: 'J', value: 100 }, |
| 45 | + { name: 'K', value: 100 }, |
| 46 | + ], |
| 47 | + }).sum((d) => { |
| 48 | + // @ts-expect-error |
| 49 | + return d.value; |
| 50 | + }); |
| 51 | +
|
32 | 52 | let tile: ComponentProps<typeof Treemap>['tile'] = $state('squarify'); |
33 | 53 | let colorBy = $state('children'); |
34 | 54 |
|
|
44 | 64 | chromatic.schemeSpectral[9].filter((c) => hsl(c).h < 60 || hsl(c).h > 90) // filter out hard to see yellow and green |
45 | 65 | ); |
46 | 66 |
|
| 67 | + const simpleOrdinalColor = scaleOrdinal( |
| 68 | + chromatic.schemeSpectral[11].filter((c) => hsl(c).h < 60 || hsl(c).h > 90) // filter out hard to see yellow and green |
| 69 | + ); |
| 70 | +
|
47 | 71 | function getNodeColor(node: HierarchyNode<any>, colorBy: string) { |
48 | 72 | switch (colorBy) { |
49 | 73 | case 'children': |
|
64 | 88 |
|
65 | 89 | <h1>Example</h1> |
66 | 90 |
|
67 | | -<h1>Complex</h1> |
| 91 | +<h2>Playground</h2> |
68 | 92 |
|
69 | 93 | <div class="grid gap-1 mb-4"> |
70 | 94 | <div class="grid grid-cols-[6fr_3fr] gap-1"> |
|
98 | 122 | </div> |
99 | 123 | </div> |
100 | 124 |
|
101 | | -<Preview> |
| 125 | +<Preview data={root}> |
102 | 126 | <div class="h-[800px]"> |
103 | 127 | <Chart> |
104 | 128 | {#snippet children({ context })} |
105 | 129 | <Svg> |
106 | 130 | <Treemap |
107 | | - hierarchy={root.copy()} |
| 131 | + hierarchy={root} |
108 | 132 | {tile} |
109 | 133 | {paddingOuter} |
110 | 134 | {paddingInner} |
|
183 | 207 | </Chart> |
184 | 208 | </div> |
185 | 209 | </Preview> |
| 210 | + |
| 211 | +<h2>Simple / flat</h2> |
| 212 | + |
| 213 | +<Preview data={simpleRoot}> |
| 214 | + <div class="h-[400px]"> |
| 215 | + <Chart> |
| 216 | + <Svg> |
| 217 | + <Treemap hierarchy={simpleRoot}> |
| 218 | + {#snippet children({ nodes })} |
| 219 | + {#each nodes.filter((n) => n.depth > 0) as node} |
| 220 | + <Group x={node.x0} y={node.y0}> |
| 221 | + {@const nodeWidth = node.x1 - node.x0} |
| 222 | + {@const nodeHeight = node.y1 - node.y0} |
| 223 | + <Rect |
| 224 | + width={nodeWidth} |
| 225 | + height={nodeHeight} |
| 226 | + stroke="rgb(0, 0, 0, 0.2)" |
| 227 | + fill={simpleOrdinalColor(node.data.name)} |
| 228 | + /> |
| 229 | + <Text |
| 230 | + x={nodeWidth / 2} |
| 231 | + y={nodeHeight / 2} |
| 232 | + value={node.data.name} |
| 233 | + fill="rgb(0, 0, 0, 0.8)" |
| 234 | + textAnchor="middle" |
| 235 | + verticalAnchor="middle" |
| 236 | + /> |
| 237 | + </Group> |
| 238 | + {/each} |
| 239 | + {/snippet} |
| 240 | + </Treemap> |
| 241 | + </Svg> |
| 242 | + </Chart> |
| 243 | + </div> |
| 244 | +</Preview> |
0 commit comments