Skip to content

Commit

Permalink
simpler treemap basic
Browse files Browse the repository at this point in the history
  • Loading branch information
holtzy committed Nov 13, 2024
1 parent f315513 commit 9b322ca
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions viz/TreemapBasic/Treemap.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useMemo } from "react";
import { Tree } from "./data";
import * as d3 from "d3";
import { Tree } from './data';
import * as d3 from 'd3';

type TreemapProps = {
width: number;
Expand All @@ -9,14 +8,10 @@ type TreemapProps = {
};

export const Treemap = ({ width, height, data }: TreemapProps) => {
const hierarchy = useMemo(() => {
return d3.hierarchy(data).sum((d) => d.value);
}, [data]);
const hierarchy = d3.hierarchy(data).sum((d) => d.value);

const root = useMemo(() => {
const treeGenerator = d3.treemap<Tree>().size([width, height]).padding(4);
return treeGenerator(hierarchy);
}, [hierarchy, width, height]);
const treeGenerator = d3.treemap<Tree>().size([width, height]).padding(4);
const root = treeGenerator(hierarchy);

const allShapes = root.leaves().map((leaf) => {
return (
Expand All @@ -27,8 +22,8 @@ export const Treemap = ({ width, height, data }: TreemapProps) => {
width={leaf.x1 - leaf.x0}
height={leaf.y1 - leaf.y0}
stroke="transparent"
fill={"#69b3a2"}
className={"opacity-80 hover:opacity-100"}
fill={'#69b3a2'}
className={'opacity-80 hover:opacity-100'}
/>
<text
x={leaf.x0 + 3}
Expand Down

0 comments on commit 9b322ca

Please sign in to comment.