|
| 1 | +// deck.gl |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +// Copyright (c) vis.gl contributors |
| 4 | + |
| 5 | +import React, {useMemo} from 'react'; |
| 6 | +import {createRoot} from 'react-dom/client'; |
| 7 | +import {DeckGL} from '@deck.gl/react'; |
| 8 | +import {OrthographicView} from '@deck.gl/core'; |
| 9 | +import type {PickingInfo, OrthographicViewState, Color} from '@deck.gl/core'; |
| 10 | +import {TextLayer} from '@deck.gl/layers'; |
| 11 | +import {scaleOrdinal, hierarchy, treemap, treemapSquarify, format} from 'd3'; |
| 12 | +import type {HierarchyNode, HierarchyRectangularNode} from 'd3'; |
| 13 | + |
| 14 | +// Sample data |
| 15 | +const DATA_URL = |
| 16 | + 'https://raw.githubusercontent.com/d3/d3-hierarchy/refs/heads/main/test/data/flare.json'; |
| 17 | + |
| 18 | +const ColorScale = scaleOrdinal<string, Color>().range([ |
| 19 | + [78, 121, 167], |
| 20 | + [242, 142, 44], |
| 21 | + [225, 87, 89], |
| 22 | + [118, 183, 178], |
| 23 | + [89, 161, 79], |
| 24 | + [237, 201, 73], |
| 25 | + [175, 122, 161], |
| 26 | + [255, 157, 167], |
| 27 | + [156, 117, 95], |
| 28 | + [186, 176, 171] |
| 29 | +]); |
| 30 | + |
| 31 | +type Datum = { |
| 32 | + name: string; |
| 33 | + value: number; |
| 34 | +}; |
| 35 | + |
| 36 | +const formatValue = format(',d'); |
| 37 | + |
| 38 | +const WIDTH = 1000; |
| 39 | +const HEIGHT = 1000; |
| 40 | +const INITIAL_VIEW_STATE: OrthographicViewState = { |
| 41 | + target: [WIDTH / 2, HEIGHT / 2, 0], |
| 42 | + zoom: -1 |
| 43 | +}; |
| 44 | + |
| 45 | +export default function App({data}: {data?: HierarchyNode<Datum>}) { |
| 46 | + const layoutRoot = useMemo(() => { |
| 47 | + if (!data) return null; |
| 48 | + data.sort((a, b) => b.value! - a.value!); |
| 49 | + return treemap<Datum>().tile(treemapSquarify).size([WIDTH, HEIGHT]).padding(1)(data); |
| 50 | + }, [data]); |
| 51 | + |
| 52 | + const leaves = useMemo(() => layoutRoot?.leaves(), [layoutRoot]); |
| 53 | + |
| 54 | + const layers = [ |
| 55 | + new TextLayer<HierarchyRectangularNode<Datum>>({ |
| 56 | + id: 'labels-name', |
| 57 | + data: leaves, |
| 58 | + getPosition: d => [d.x0, d.y1], |
| 59 | + getText: d => d.data.name, |
| 60 | + getPixelOffset: [4, 0], |
| 61 | + getSize: 12, |
| 62 | + getColor: [255, 255, 255], |
| 63 | + getTextAnchor: 'start', |
| 64 | + getAlignmentBaseline: 'center', |
| 65 | + background: true, |
| 66 | + getBackgroundColor: d => { |
| 67 | + while (d.depth > 1) d = d.parent!; |
| 68 | + return ColorScale(d.data.name); |
| 69 | + }, |
| 70 | + getContentBox: d => [0, d.y0 - d.y1, d.x1 - d.x0, d.y1 - d.y0], |
| 71 | + contentAlignHorizontal: 'start', |
| 72 | + contentAlignVertical: 'center', |
| 73 | + contentCutoffPixels: [60, 30] |
| 74 | + }), |
| 75 | + new TextLayer<HierarchyRectangularNode<Datum>>({ |
| 76 | + id: 'labels-value', |
| 77 | + data: leaves, |
| 78 | + getPosition: d => [d.x0, d.y0], |
| 79 | + getText: d => formatValue(d.data.value), |
| 80 | + getPixelOffset: [4, 12], |
| 81 | + getSize: 10, |
| 82 | + getColor: [255, 255, 255, 200], |
| 83 | + getTextAnchor: 'start', |
| 84 | + getAlignmentBaseline: 'center', |
| 85 | + getContentBox: d => [0, 0, d.x1 - d.x0, d.y1 - d.y0], |
| 86 | + contentAlignHorizontal: 'start', |
| 87 | + contentAlignVertical: 'center', |
| 88 | + contentCutoffPixels: [60, 60] |
| 89 | + }) |
| 90 | + ]; |
| 91 | + |
| 92 | + return ( |
| 93 | + <DeckGL |
| 94 | + layers={layers} |
| 95 | + views={new OrthographicView()} |
| 96 | + initialViewState={INITIAL_VIEW_STATE} |
| 97 | + controller |
| 98 | + getTooltip={getTooltip} |
| 99 | + /> |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +function getTooltip({object}: PickingInfo<HierarchyRectangularNode<Datum>>) { |
| 104 | + return object |
| 105 | + ? `\ |
| 106 | + name: ${object |
| 107 | + .ancestors() |
| 108 | + .map(d => d.data.name) |
| 109 | + .reverse() |
| 110 | + .join('.')} |
| 111 | + value: ${formatValue(object.data.value)} |
| 112 | + ` |
| 113 | + : null; |
| 114 | +} |
| 115 | + |
| 116 | +export async function renderToDOM(container: HTMLDivElement) { |
| 117 | + const root = createRoot(container); |
| 118 | + root.render(<App />); |
| 119 | + |
| 120 | + const resp = await fetch(DATA_URL); |
| 121 | + const json = await resp.json(); |
| 122 | + const data = hierarchy(json).sum(d => d.value); |
| 123 | + root.render(<App data={data} />); |
| 124 | +} |
0 commit comments