8.8 KB354 lines
Blame
1> **Warning**
2>
3> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
4>
5> ## Please edit the corresponding file in [/packages/mermaid/src/docs/syntax/treemap.md](../../packages/mermaid/src/docs/syntax/treemap.md).
6
7# Treemap Diagram
8
9> A treemap diagram displays hierarchical data as a set of nested rectangles. Each branch of the tree is represented by a rectangle, which is then tiled with smaller rectangles representing sub-branches.
10
11> **Warning**
12> This is a new diagram type in Mermaid. Its syntax may evolve in future versions.
13
14## Introduction
15
16Treemap diagrams are an effective way to visualize hierarchical data and show proportions between categories and subcategories. The size of each rectangle is proportional to the value it represents, making it easy to compare different parts of a hierarchy.
17
18Treemap diagrams are particularly useful for:
19
20- Visualizing hierarchical data structures
21- Comparing proportions between categories
22- Displaying large amounts of hierarchical data in a limited space
23- Identifying patterns and outliers in hierarchical data
24
25## Syntax
26
27```
28treemap-beta
29"Section 1"
30 "Leaf 1.1": 12
31 "Section 1.2"
32 "Leaf 1.2.1": 12
33"Section 2"
34 "Leaf 2.1": 20
35 "Leaf 2.2": 25
36```
37
38### Node Definition
39
40Nodes in a treemap are defined using the following syntax:
41
42- **Section/Parent nodes**: Defined with quoted text `"Section Name"`
43- **Leaf nodes with values**: Defined with quoted text followed by a colon and value `"Leaf Name": value`
44- **Hierarchy**: Created using indentation (spaces or tabs)
45- **Styling**: Nodes can be styled using the `:::class` syntax
46
47## Examples
48
49### Basic Treemap
50
51```mermaid-example
52treemap-beta
53"Category A"
54 "Item A1": 10
55 "Item A2": 20
56"Category B"
57 "Item B1": 15
58 "Item B2": 25
59```
60
61```mermaid
62treemap-beta
63"Category A"
64 "Item A1": 10
65 "Item A2": 20
66"Category B"
67 "Item B1": 15
68 "Item B2": 25
69```
70
71### Hierarchical Treemap
72
73```mermaid-example
74treemap-beta
75"Products"
76 "Electronics"
77 "Phones": 50
78 "Computers": 30
79 "Accessories": 20
80 "Clothing"
81 "Men's": 40
82 "Women's": 40
83```
84
85```mermaid
86treemap-beta
87"Products"
88 "Electronics"
89 "Phones": 50
90 "Computers": 30
91 "Accessories": 20
92 "Clothing"
93 "Men's": 40
94 "Women's": 40
95```
96
97### Treemap with Styling
98
99```mermaid-example
100treemap-beta
101"Section 1"
102 "Leaf 1.1": 12
103 "Section 1.2":::class1
104 "Leaf 1.2.1": 12
105"Section 2"
106 "Leaf 2.1": 20:::class1
107 "Leaf 2.2": 25
108 "Leaf 2.3": 12
109
110classDef class1 fill:red,color:blue,stroke:#FFD600;
111```
112
113```mermaid
114treemap-beta
115"Section 1"
116 "Leaf 1.1": 12
117 "Section 1.2":::class1
118 "Leaf 1.2.1": 12
119"Section 2"
120 "Leaf 2.1": 20:::class1
121 "Leaf 2.2": 25
122 "Leaf 2.3": 12
123
124classDef class1 fill:red,color:blue,stroke:#FFD600;
125```
126
127## Styling and Configuration
128
129Treemap diagrams can be customized using Mermaid's styling and configuration options.
130
131### Using classDef for Styling
132
133You can define custom styles for nodes using the `classDef` syntax, which is a standard feature across many Mermaid diagram types:
134
135```mermaid-example
136treemap-beta
137"Main"
138 "A": 20
139 "B":::important
140 "B1": 10
141 "B2": 15
142 "C": 5
143
144classDef important fill:#f96,stroke:#333,stroke-width:2px;
145```
146
147```mermaid
148treemap-beta
149"Main"
150 "A": 20
151 "B":::important
152 "B1": 10
153 "B2": 15
154 "C": 5
155
156classDef important fill:#f96,stroke:#333,stroke-width:2px;
157```
158
159### Theme Configuration
160
161You can customize the colors of your treemap using the theme configuration:
162
163```mermaid-example
164---
165config:
166 theme: 'forest'
167---
168treemap-beta
169"Category A"
170 "Item A1": 10
171 "Item A2": 20
172"Category B"
173 "Item B1": 15
174 "Item B2": 25
175```
176
177```mermaid
178---
179config:
180 theme: 'forest'
181---
182treemap-beta
183"Category A"
184 "Item A1": 10
185 "Item A2": 20
186"Category B"
187 "Item B1": 15
188 "Item B2": 25
189```
190
191### Diagram Padding
192
193You can adjust the padding around the treemap diagram using the `diagramPadding` configuration option:
194
195```mermaid-example
196---
197config:
198 treemap:
199 diagramPadding: 200
200---
201treemap-beta
202"Category A"
203 "Item A1": 10
204 "Item A2": 20
205"Category B"
206 "Item B1": 15
207 "Item B2": 25
208```
209
210```mermaid
211---
212config:
213 treemap:
214 diagramPadding: 200
215---
216treemap-beta
217"Category A"
218 "Item A1": 10
219 "Item A2": 20
220"Category B"
221 "Item B1": 15
222 "Item B2": 25
223```
224
225## Configuration Options
226
227The treemap diagram supports the following configuration options:
228
229| Option | Description | Default |
230| -------------- | --------------------------------------------------------------------------- | ------- |
231| useMaxWidth | When true, the diagram width is set to 100% and scales with available space | true |
232| padding | Internal padding between nodes | 10 |
233| diagramPadding | Padding around the entire diagram | 8 |
234| showValues | Whether to show values in the treemap | true |
235| nodeWidth | Width of nodes | 100 |
236| nodeHeight | Height of nodes | 40 |
237| borderWidth | Width of borders | 1 |
238| valueFontSize | Font size for values | 12 |
239| labelFontSize | Font size for labels | 14 |
240| valueFormat | Format for values (see Value Formatting section) | ',' |
241
242## Advanced Features
243
244### Value Formatting
245
246Values in treemap diagrams can be formatted to display in different ways using the `valueFormat` configuration option. This option primarily uses [D3's format specifiers](https://github.com/d3/d3-format#locale_format) to control how numbers are displayed, with some additional special cases for common formats.
247
248Some common format patterns:
249
250- `,` - Thousands separator (default)
251- `$` - Add dollar sign
252- `.1f` - Show one decimal place
253- `.1%` - Show as percentage with one decimal place
254- `$0,0` - Dollar sign with thousands separator
255- `$.2f` - Dollar sign with 2 decimal places
256- `$,.2f` - Dollar sign with thousands separator and 2 decimal places
257
258The treemap diagram supports both standard D3 format specifiers and some common currency formats that combine the dollar sign with other formatting options.
259
260Example with currency formatting:
261
262```mermaid-example
263---
264config:
265 treemap:
266 valueFormat: '$0,0'
267---
268treemap-beta
269"Budget"
270 "Operations"
271 "Salaries": 700000
272 "Equipment": 200000
273 "Supplies": 100000
274 "Marketing"
275 "Advertising": 400000
276 "Events": 100000
277```
278
279```mermaid
280---
281config:
282 treemap:
283 valueFormat: '$0,0'
284---
285treemap-beta
286"Budget"
287 "Operations"
288 "Salaries": 700000
289 "Equipment": 200000
290 "Supplies": 100000
291 "Marketing"
292 "Advertising": 400000
293 "Events": 100000
294```
295
296Example with percentage formatting:
297
298```mermaid-example
299---
300config:
301 treemap:
302 valueFormat: '$.1%'
303---
304treemap-beta
305"Market Share"
306 "Company A": 0.35
307 "Company B": 0.25
308 "Company C": 0.15
309 "Others": 0.25
310```
311
312```mermaid
313---
314config:
315 treemap:
316 valueFormat: '$.1%'
317---
318treemap-beta
319"Market Share"
320 "Company A": 0.35
321 "Company B": 0.25
322 "Company C": 0.15
323 "Others": 0.25
324```
325
326## Common Use Cases
327
328Treemap diagrams are commonly used for:
329
3301. **Financial Data**: Visualizing budget allocations, market shares, or portfolio compositions
3312. **File System Analysis**: Showing disk space usage by folders and files
3323. **Population Demographics**: Displaying population distribution across regions and subregions
3334. **Product Hierarchies**: Visualizing product categories and their sales volumes
3345. **Organizational Structures**: Representing departments and team sizes in a company
335
336## Limitations
337
338- Treemap diagrams work best when the data has a natural hierarchy
339- Very small values may be difficult to see or label in a treemap diagram
340- Deep hierarchies (many levels) can be challenging to represent clearly
341- Treemap diagrams are not well suited for representing data with negative values
342
343## Related Diagrams
344
345If treemap diagrams don't suit your needs, consider these alternatives:
346
347- [**Pie Charts**](./pie.md): For simple proportion comparisons without hierarchy
348- **Sunburst Diagrams**: For hierarchical data with a radial layout (yet to be released in Mermaid).
349- [**Sankey Diagrams**](./sankey.md): For flow-based hierarchical data
350
351## Notes
352
353The treemap diagram implementation in Mermaid is designed to be simple to use while providing powerful visualization capabilities. As this is a newer diagram type, feedback and feature requests are welcome through the Mermaid GitHub repository.
354