1 | import { __assign, __extends, __spreadArray } from "tslib";
|
2 | import { jsx, Component, isEqual as equal, createRef } from '@antv/f-engine';
|
3 | import { Category } from '../../attr';
|
4 | import CoordController from '../../controller/coord';
|
5 | import { hierarchy, treemap, treemapBinary } from '../../deps/d3-hierarchy/src';
|
6 | import Theme from '../../theme';
|
7 | import { deepMix, each, isFunction } from '@antv/util';
|
8 | var withTreemap = function withTreemap(View) {
|
9 | return function (_super) {
|
10 | __extends(Treemap, _super);
|
11 | function Treemap(props, context) {
|
12 | var _this = _super.call(this, props, context) || this;
|
13 | var color = props.color,
|
14 | data = props.data,
|
15 | theme = props.theme,
|
16 | _a = props.selection,
|
17 | selection = _a === void 0 ? {} : _a;
|
18 | var px2hd = context.px2hd;
|
19 | context.theme = deepMix(px2hd(Theme), theme);
|
20 | _this.coord = new CoordController();
|
21 | _this.color = new Category(__assign(__assign({
|
22 | range: context.theme.colors
|
23 | }, color), {
|
24 | data: data
|
25 | }));
|
26 | var _b = selection.defaultSelected,
|
27 | defaultSelected = _b === void 0 ? null : _b;
|
28 | _this.state.selected = defaultSelected;
|
29 | _this.coordRef = createRef();
|
30 | _this.records = [];
|
31 | return _this;
|
32 | }
|
33 | Treemap.prototype.isSelected = function (record) {
|
34 | var state = this.state;
|
35 | var selected = state.selected;
|
36 | if (!selected || !selected.length) {
|
37 | return false;
|
38 | }
|
39 | for (var i = 0, len = selected.length; i < len; i++) {
|
40 | var item = selected[i];
|
41 | if (equal(record, item)) {
|
42 | return true;
|
43 | }
|
44 | }
|
45 | return false;
|
46 | };
|
47 | Treemap.prototype.getSelectionStyle = function (record) {
|
48 | var _a = this,
|
49 | state = _a.state,
|
50 | props = _a.props;
|
51 | var selected = state.selected;
|
52 | if (!selected || !selected.length) {
|
53 | return null;
|
54 | }
|
55 | var selection = props.selection;
|
56 | var selectedStyle = selection.selectedStyle,
|
57 | unSelectedStyle = selection.unSelectedStyle;
|
58 | var isSelected = this.isSelected(record);
|
59 | if (isSelected) {
|
60 | return isFunction(selectedStyle) ? selectedStyle(record) : selectedStyle;
|
61 | }
|
62 | return isFunction(unSelectedStyle) ? unSelectedStyle(record) : unSelectedStyle;
|
63 | };
|
64 | Treemap.prototype.willMount = function () {
|
65 | var _a = this,
|
66 | props = _a.props,
|
67 | coord = _a.coord,
|
68 | layout = _a.layout;
|
69 | var coordOption = props.coord;
|
70 | coord.updateLayout(layout);
|
71 | coord.create(coordOption);
|
72 | };
|
73 | Treemap.prototype.willReceiveProps = function (nextProps) {
|
74 | var nextSelection = nextProps.selection;
|
75 | var lastSelection = this.props.selection;
|
76 | if (!nextSelection || !lastSelection) {
|
77 | return;
|
78 | }
|
79 | var nextDefaultSelected = nextSelection.defaultSelected;
|
80 | var lastDefaultSelected = lastSelection.defaultSelected;
|
81 | if (!equal(nextDefaultSelected, lastDefaultSelected)) {
|
82 | this.state.selected = nextDefaultSelected;
|
83 | }
|
84 | };
|
85 | Treemap.prototype.treemapLayout = function () {
|
86 | var _this = this;
|
87 | var _a = this,
|
88 | props = _a.props,
|
89 | coord = _a.coord,
|
90 | colorAttr = _a.color;
|
91 | var _b = coord.getCoord(),
|
92 | width = _b.width,
|
93 | height = _b.height;
|
94 | var data = props.data,
|
95 | value = props.value,
|
96 | _c = props.space,
|
97 | space = _c === void 0 ? 0 : _c;
|
98 | var root = hierarchy({
|
99 | children: data
|
100 | }).sum(function (d) {
|
101 | return d[value];
|
102 | }).sort(function (a, b) {
|
103 | return b[value] - a[value];
|
104 | });
|
105 | var treemapLayout = treemap()
|
106 |
|
107 | .tile(treemapBinary).round(false).size([width, height])
|
108 |
|
109 | .paddingInner(space);
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | var nodes = treemapLayout(root);
|
116 | return nodes.children.map(function (item) {
|
117 | var data = item.data,
|
118 | x0 = item.x0,
|
119 | y0 = item.y0,
|
120 | x1 = item.x1,
|
121 | y1 = item.y1;
|
122 | var color = colorAttr.mapping(data[colorAttr.field]);
|
123 | var rect = {
|
124 | xMin: x0,
|
125 | xMax: x1,
|
126 | yMin: y0,
|
127 | yMax: y1
|
128 | };
|
129 | var style = _this.getSelectionStyle(data);
|
130 | return __assign(__assign({
|
131 | key: data.key,
|
132 | origin: data,
|
133 | color: color
|
134 | }, rect), {
|
135 | style: style
|
136 | });
|
137 | });
|
138 | };
|
139 | Treemap.prototype.select = function (ev, trigger) {
|
140 | var _this = this;
|
141 | var points = ev.points,
|
142 | x = ev.canvasX,
|
143 | y = ev.canvasY;
|
144 | var _a = this.props.selection,
|
145 | selection = _a === void 0 ? {} : _a;
|
146 | var triggerOn = selection.triggerOn,
|
147 | _b = selection.type,
|
148 | type = _b === void 0 ? 'single' : _b,
|
149 | _c = selection.cancelable,
|
150 | cancelable = _c === void 0 ? true : _c;
|
151 | if (!triggerOn || trigger !== triggerOn) return;
|
152 | var point = triggerOn === 'click' ? {
|
153 | x: x,
|
154 | y: y
|
155 | } : points[0];
|
156 | var selected = this.state.selected;
|
157 | var origin = [];
|
158 | each(this.records, function (record) {
|
159 | if (point.x >= record.xMin && point.x <= record.xMax && point.y >= record.yMin && point.y <= record.yMax) {
|
160 | origin.push(record === null || record === void 0 ? void 0 : record.origin);
|
161 | }
|
162 | });
|
163 |
|
164 | if (!origin) {
|
165 | this.setState({
|
166 | selected: null
|
167 | });
|
168 | return;
|
169 | }
|
170 | if (!selected) {
|
171 | this.setState({
|
172 | selected: origin
|
173 | });
|
174 | return;
|
175 | }
|
176 |
|
177 | var newSelected = [];
|
178 | origin.forEach(function (record) {
|
179 | if (!_this.isSelected(record)) {
|
180 | newSelected.push(record);
|
181 | }
|
182 | });
|
183 | if (type === 'single') {
|
184 | this.setState({
|
185 | selected: cancelable ? newSelected : origin
|
186 | });
|
187 | return;
|
188 | }
|
189 | this.setState({
|
190 | selected: __spreadArray(__spreadArray([], newSelected, true), selected, true)
|
191 | });
|
192 | };
|
193 | Treemap.prototype.render = function () {
|
194 | var _this = this;
|
195 | var nodes = this.treemapLayout();
|
196 | this.records = nodes;
|
197 | var _a = this,
|
198 | props = _a.props,
|
199 | coord = _a.coord;
|
200 | var _b = coord.getCoord(),
|
201 | width = _b.width,
|
202 | height = _b.height;
|
203 | return jsx("group", {
|
204 | style: {
|
205 | width: width,
|
206 | height: height,
|
207 | fill: 'transparent'
|
208 | },
|
209 | onClick: function onClick(ev) {
|
210 | return _this.select(ev, 'click');
|
211 | },
|
212 | onPress: function onPress(ev) {
|
213 | return _this.select(ev, 'press');
|
214 | }
|
215 | }, jsx(View, __assign({
|
216 | nodes: nodes
|
217 | }, props, {
|
218 | coord: coord.getCoord()
|
219 | })));
|
220 | };
|
221 | return Treemap;
|
222 | }(Component);
|
223 | };
|
224 | export default withTreemap; |
\ | No newline at end of file |