Skip to content

Commit

Permalink
try animating the treemap
Browse files Browse the repository at this point in the history
  • Loading branch information
holtzy committed Dec 4, 2024
1 parent 9c12ca5 commit 7ceae82
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 44 deletions.
62 changes: 44 additions & 18 deletions viz/TreemapFrenchTravel/Rectangle.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
import { useSpring, animated } from '@react-spring/web';
import { useSpring, animated, config } from '@react-spring/web';

type RectangleProps = {
width: number;
height: number;
x: number;
y: number;
label: string;
value: number;
color: string;
};

export const Rectangle = (props: RectangleProps) => {
const { x, y, width, height } = props;
const { x, y, width, height, label, value, color } = props;

const springProps = useSpring({
to: { x, y, width, height },
config: {
friction: 30,
},
to: { x, y, width, height, value },
config: config.molasses,
});

if (y === undefined) {
return null;
}

return (
<animated.rect
x={springProps.x}
y={springProps.y}
width={springProps.width}
height={springProps.height}
opacity={0.7}
stroke="#9d174d"
fill="#9d174d"
fillOpacity={0.3}
strokeWidth={1}
rx={1}
/>
<g>
<animated.rect
key={label}
x={springProps.x}
y={springProps.y}
width={springProps.width}
height={springProps.height}
opacity={0.7}
stroke={color}
fill={color}
fillOpacity={0.9}
strokeWidth={1}
rx={1}
/>
<animated.text
x={springProps.x.to((x) => x + 3)}
y={springProps.y.to((y) => y + 3)}
fontSize={12}
textAnchor="start"
alignmentBaseline="hanging"
fill="white"
className="font-bold"
>
{label}
</animated.text>
<animated.text
x={springProps.x.to((x) => x + 3)}
y={springProps.y.to((y) => y + 18)}
fontSize={12}
textAnchor="start"
alignmentBaseline="hanging"
fill="white"
className="font-light"
>
{springProps.value.to((val) => Math.round(val))}
</animated.text>
</g>
);
};
54 changes: 28 additions & 26 deletions viz/TreemapFrenchTravel/Treemap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as d3 from 'd3';
import styles from './treemap.module.css';
import { Tree } from '@/viz/TreemapFrenchTravel/data';
import { Rectangle } from './Rectangle';
import { useMemo } from 'react';

type TreemapProps = {
width: number;
Expand All @@ -17,47 +18,48 @@ const colors = [
'#9a6fb0',
'#a53253',
'#7f7f7f',
'red',
];

export const Treemap = ({ width, height, data }: TreemapProps) => {
const hierarchy = d3.hierarchy(data).sum((d) => d.value);
console.log('hierarchy', hierarchy);
const hierarchy = d3
.hierarchy(data)
.sum((d) => d.value)
.sort((a, b) => b.value - a.value);

const treeGenerator = useMemo(() => {
console.log('compute tree generator');

return d3
.treemap()
.size([width, height])
.padding(4)
.round(true)
.tile(d3.treemapResquarify);
}, []);

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

const allGroups = hierarchy
.leaves()
.map((l) => l.data.name)
.sort((a, b) => b.localeCompare(a));

var colorScale = d3.scaleOrdinal().domain(allGroups).range(colors);

const allShapes = root.leaves().map((leaf) => {
return (
<g key={leaf.id}>
<Rectangle
key={leaf.data.name}
x={leaf.x0}
y={leaf.y0}
width={leaf.x1 - leaf.x0}
height={leaf.y1 - leaf.y0}
label={leaf.data.name}
value={leaf.data.value}
color={colorScale(leaf.data.name)}
/>

{/* <text
x={leaf.x0 + 3}
y={leaf.y0 + 3}
fontSize={12}
textAnchor="start"
alignmentBaseline="hanging"
fill="white"
className="font-bold"
>
{leaf.data.name}
</text>
<text
x={leaf.x0 + 3}
y={leaf.y0 + 18}
fontSize={12}
textAnchor="start"
alignmentBaseline="hanging"
fill="white"
className="font-light"
>
{leaf.data.value}
</text> */}
</g>
);
});
Expand Down

0 comments on commit 7ceae82

Please sign in to comment.