-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreemap.ts
155 lines (141 loc) · 3.23 KB
/
treemap.ts
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
147
148
149
150
151
152
153
154
155
import { deepMix, get, last } from '@antv/util';
import { subObject } from '../utils/helper';
import { CompositionComponent as CC } from '../runtime';
import { TreemapMark } from '../spec';
import { maybeTooltip } from '../utils/mark';
import { treeDataTransform } from '../utils/treeDataTransform';
export type TreemapOptions = Omit<TreemapMark, 'type'>;
// Defaults
const GET_DEFAULT_LAYOUT_OPTIONS = (width, height) => ({
tile: 'treemapSquarify',
ratio: 0.5 * (1 + Math.sqrt(5)),
size: [width, height],
round: false,
ignoreParentValue: true,
padding: 0,
paddingInner: 0,
paddingOuter: 0,
paddingTop: 0,
paddingRight: 0,
paddingBottom: 0,
paddingLeft: 0,
sort: (a, b) => b.value - a.value,
layer: 0,
});
const GET_DEFAULT_OPTIONS = (width, height) => ({
type: 'rect',
axis: false,
encode: {
x: 'x',
y: 'y',
key: 'id',
color: (d) => d.path[1],
},
scale: {
x: { domain: [0, width], range: [0, 1] },
y: { domain: [0, height], range: [0, 1] },
},
style: {
stroke: '#fff',
},
state: {
active: { opacity: 0.6 },
inactive: { opacity: 1 },
},
});
const DEFAULT_LABEL_OPTIONS = {
fontSize: 10,
text: (d) => last(d.path),
position: 'inside',
fill: '#000',
textOverflow: 'clip',
wordWrap: true,
maxLines: 1,
wordWrapWidth: (d) => d.x1 - d.x0,
};
const DEFAULT_TOOLTIP_OPTIONS = {
title: (d) => d.path?.join?.('.'),
items: [{ field: 'value' }],
};
const DEFAULT_TOOLTIP_OPTIONS_DRILL = {
title: (d) => last(d.path),
items: [{ field: 'value' }],
};
export const Treemap: CC<TreemapOptions> = (options, context) => {
const { width, height, options: markOptions } = context;
const {
data,
encode = {},
scale,
style = {},
layout = {},
labels = [],
tooltip = {},
...resOptions
} = options;
const treemapDrillDown = get(markOptions, [
'interaction',
'treemapDrillDown',
]);
// Layout
const layoutOptions = deepMix(
{},
GET_DEFAULT_LAYOUT_OPTIONS(width, height),
layout,
{
layer: treemapDrillDown
? (d) => {
return d.depth === 1;
}
: layout.layer,
},
);
// Data
const [transformedData, transformedDataAll] = treeDataTransform(
data,
layoutOptions,
encode,
);
// Label
const labelStyle = subObject(style, 'label');
return deepMix(
{},
GET_DEFAULT_OPTIONS(width, height),
{
data: transformedData,
scale,
style,
labels: [
{
...DEFAULT_LABEL_OPTIONS,
...labelStyle,
},
...labels,
],
...resOptions,
encode,
tooltip: maybeTooltip(tooltip, DEFAULT_TOOLTIP_OPTIONS),
axis: false,
},
treemapDrillDown
? {
interaction: {
...resOptions.interaction,
treemapDrillDown: treemapDrillDown
? {
...treemapDrillDown,
originData: transformedDataAll,
layout: layoutOptions,
}
: undefined,
},
encode: {
color: (d) => last(d.path),
...encode,
},
tooltip: maybeTooltip(tooltip, DEFAULT_TOOLTIP_OPTIONS_DRILL),
}
: {},
);
};
Treemap.props = {};