diff --git a/pages/example/deplacement-en-france.tsx b/pages/example/deplacement-en-france.tsx index da4ca35e..a0fc8075 100644 --- a/pages/example/deplacement-en-france.tsx +++ b/pages/example/deplacement-en-france.tsx @@ -8,7 +8,8 @@ import Link from 'next/link'; import { SankeyBumpChartDemo } from 'viz/SankeyBumpChart/SankeyBumpChartDemo'; import { LinkAsButton } from 'component/LinkAsButton'; import { Treemap } from '@/viz/TreemapFrenchTravel/Treemap'; -import { data } from '@/viz/TreemapFrenchTravel/data'; +import { data, rawData } from '@/viz/TreemapFrenchTravel/data'; +import { stratify } from 'd3'; const graphDescription = ( <> @@ -17,6 +18,31 @@ const graphDescription = ( ); export default function Home() { + const groups = ['Etudes', 'Professionnel', 'Vacances']; + const target = 'Temps'; + + const mainDataSelection = rawData['Motifs de déplacement']; + console.log('mainDataSelection', mainDataSelection); + + const selectedData = Object.keys(mainDataSelection).map((key) => { + return { + parent: 'Temps', + name: key, + value: Number(mainDataSelection[key]['Temps'].replace('%', '')), + }; + }); + console.log('selectedData', selectedData); + + const hierarchyBuilder = stratify() + .id((d) => d.name) + .parentId((d) => d.parent); + const hierarchy = hierarchyBuilder([ + { parent: undefined, name: 'Temps' }, + ...selectedData, + ]); + + console.log(hierarchy); + return ( - +
diff --git a/viz/TreemapFrenchTravel/Treemap.tsx b/viz/TreemapFrenchTravel/Treemap.tsx index dfbaa8f6..abb116f3 100644 --- a/viz/TreemapFrenchTravel/Treemap.tsx +++ b/viz/TreemapFrenchTravel/Treemap.tsx @@ -1,12 +1,24 @@ import { useMemo } from 'react'; -import { Tree } from './data'; import * as d3 from 'd3'; import styles from './treemap.module.css'; +export type TreeNode = { + type: 'node'; + name: string; + children: Tree[]; +}; +export type TreeLeaf = { + parent: string; + name: string; + value: number; +}; + +export type Tree = TreeNode | TreeLeaf; + type TreemapProps = { width: number; height: number; - data: Tree; + data: d3.HierarchyNode; }; const colors = [ @@ -21,21 +33,22 @@ const colors = [ export const Treemap = ({ width, height, data }: TreemapProps) => { const hierarchy = d3.hierarchy(data).sum((d) => d.value); + console.log('hierarchy', hierarchy); - // List of item of level 1 (just under root) & related color scale const firstLevelGroups = hierarchy?.children?.map((child) => child.data.name); + console.log('firstLevelGroups', firstLevelGroups); + var colorScale = d3 .scaleOrdinal() .domain(firstLevelGroups || []) .range(colors); - const root = useMemo(() => { - const treeGenerator = d3.treemap().size([width, height]).padding(4); - return treeGenerator(hierarchy); - }, [hierarchy, width, height]); + const treeGenerator = d3.treemap().size([width, height]).padding(4); + const root = treeGenerator(hierarchy); + console.log('root', root); const allShapes = root.leaves().map((leaf, i) => { - const parentName = leaf.parent?.data.name; + console.log('leaf', leaf); return ( { width={leaf.x1 - leaf.x0} height={leaf.y1 - leaf.y0} stroke="transparent" - fill={colorScale(parentName)} + fill={colorScale(leaf.data.name)} className={'opacity-80 hover:opacity-100'} />