Basic Treemap
Code
ChartUtil.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use client";
// reference: https://d3-graph-gallery.com/graph/treemap_custom.html
import React, { useEffect, useCallback, useRef } from "react";
import * as d3 from "d3";
import { useResizeObserver } from "@/hooks/useResizeObserver";
import { ChartProps } from "@/types";
const ChartUtil = ({ data }: ChartProps) => {
const containerRef = useRef<HTMLDivElement | null>(null);
const renderFunc = useCallback(() => {
const container = containerRef.current as HTMLElement;
if (!container) return;
const margin = { top: 0, right: 0, bottom: 0, left: 0 },
width = container.offsetWidth - margin.left - margin.right,
height = 640 - margin.top - margin.bottom;
const svg = d3
.select(container)
.html("")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
const root = d3.hierarchy(data).sum(function (d) {
return d.value;
});
d3
.treemap()
.size([width, height])
.paddingTop(28)
.paddingRight(7)
.paddingInner(3)(root);
const color = d3
.scaleOrdinal()
.domain(["boss1", "boss2", "boss3"])
.range(["#973ef7", "#D18975", "#63b344"]);
// And a opacity scale
const opacity = d3.scaleLinear().domain([10, 30]).range([0.5, 1]);
// use this information to add rectangles:
svg
.selectAll("rect")
.data(root.leaves())
.join("rect")
.attr("x", function (d: any) {
return d.x0;
})
.attr("y", function (d: any) {
return d.y0;
})
.attr("width", function (d: any) {
return d.x1 - d.x0;
})
.attr("height", function (d: any) {
return d.y1 - d.y0;
})
.attr("rx", 4)
.attr("ry", 4)
.style("stroke", "black")
.style("stroke-width", 0)
.style("fill", function (d: any) {
return color(d.parent.data.name) as string;
})
.style("opacity", function (d: any) {
return opacity(d.data.value);
});
svg
.selectAll("text")
.data(root.leaves())
.enter()
.append("text")
.attr("x", function (d: any) {
return d.x0 + 5;
})
.attr("y", function (d: any) {
return d.y0 + 20;
})
.text(function (d) {
return d.data.name.replace("mister_", "");
})
.attr("font-size", "19px")
.attr("fill", "white");
svg
.selectAll("vals")
.data(root.leaves())
.enter()
.append("text")
.attr("x", function (d: any) {
return d.x0 + 5;
})
.attr("y", function (d: any) {
return d.y0 + 35;
})
.text(function (d: any) {
return d.data.value;
})
.attr("font-size", "11px")
.attr("fill", "white");
svg
.selectAll("titles")
.data(
root.descendants().filter(function (d) {
return d.depth === 1;
})
)
.enter()
.append("text")
.attr("x", function (d: any) {
return d.x0;
})
.attr("y", function (d: any) {
return d.y0 + 21;
})
.text(function (d) {
return d.data.name;
})
.attr("font-size", "19px")
.attr("fill", function (d: any) {
return color(d.data.name) as string;
});
}, [data]);
useEffect(() => {
containerRef.current !== null && renderFunc();
}, [renderFunc]);
const resizeHandler = useCallback(() => {
containerRef.current !== null && renderFunc();
}, [renderFunc]);
useResizeObserver(containerRef, resizeHandler);
return <div ref={containerRef}></div>;
};
export default ChartUtil;