UNPKG

6.67 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { jsx, Component, computeLayout } from '@antv/f-engine';
3import { isFunction } from '@antv/util';
4export default (function (View) {
5 return /** @class */function (_super) {
6 __extends(Legend, _super);
7 function Legend(props) {
8 var _this = _super.call(this, props) || this;
9 _this._onclick = function (item) {
10 var _a;
11 var props = _this.props;
12 var chart = props.chart,
13 _b = props.clickable,
14 clickable = _b === void 0 ? true : _b,
15 onClick = props.onClick;
16 if (!clickable) return;
17 var clickItem = item.currentTarget;
18 if (!clickItem) {
19 return;
20 }
21 // @ts-ignore
22 var dataItem = clickItem.config['data-item'];
23 if (!dataItem) {
24 return;
25 }
26 if (isFunction(onClick)) {
27 onClick(dataItem);
28 }
29 var field = dataItem.field,
30 tickValue = dataItem.tickValue;
31 var prevFiltered = _this.state.filtered;
32 var filtered = __assign(__assign({}, prevFiltered), (_a = {}, _a[tickValue] = !prevFiltered[tickValue], _a));
33 _this.setState({
34 filtered: filtered
35 });
36 chart.filter(field, function (value) {
37 return !filtered[value];
38 });
39 };
40 _this.state = {
41 filtered: {},
42 items: []
43 };
44 return _this;
45 }
46 Legend.prototype.getOriginItems = function () {
47 var chart = this.props.chart;
48 return chart.getLegendItems();
49 };
50 Legend.prototype.getItems = function () {
51 var _a;
52 var _b = this,
53 props = _b.props,
54 state = _b.state;
55 var filtered = state.filtered;
56 var renderItems = ((_a = props.items) === null || _a === void 0 ? void 0 : _a.length) ? props.items : this.getOriginItems();
57 if (!renderItems) return null;
58 return renderItems.map(function (item) {
59 var tickValue = item.tickValue;
60 return __assign(__assign({}, item), {
61 filtered: filtered[tickValue]
62 });
63 });
64 };
65 Legend.prototype.setItems = function (items) {
66 this.setState({
67 items: items
68 });
69 };
70 Legend.prototype.getMaxItemBox = function (node) {
71 var maxItemWidth = 0;
72 var maxItemHeight = 0;
73 (node.children || []).forEach(function (child) {
74 var layout = child.layout;
75 var width = layout.width,
76 height = layout.height;
77 maxItemWidth = Math.max(maxItemWidth, width);
78 maxItemHeight = Math.max(maxItemHeight, height);
79 });
80 return {
81 width: maxItemWidth,
82 height: maxItemHeight
83 };
84 };
85 // 计算 legend 的位置
86 Legend.prototype._init = function () {
87 var _a = this,
88 props = _a.props,
89 context = _a.context;
90 var
91 // @ts-ignore
92 parentLayout = props.layout,
93 customWidth = props.width,
94 customHeight = props.height,
95 _b = props.position,
96 position = _b === void 0 ? 'top' : _b;
97 var items = this.getItems();
98 if (!items || !items.length) return;
99 var left = parentLayout.left,
100 top = parentLayout.top,
101 layoutWidth = parentLayout.width,
102 layoutHeight = parentLayout.height;
103 var width = context.px2hd(customWidth) || layoutWidth;
104 var node = computeLayout(this, this.render());
105 var _c = this.getMaxItemBox(node),
106 itemMaxWidth = _c.width,
107 itemMaxHeight = _c.height;
108 // 每行最多的个数
109 var lineMaxCount = Math.max(1, Math.floor(width / itemMaxWidth));
110 var itemCount = items.length;
111 // legend item 的行数
112 var lineCount = Math.ceil(itemCount / lineMaxCount);
113 var itemWidth = width / lineMaxCount;
114 var autoHeight = itemMaxHeight * lineCount;
115 var style = {
116 left: left,
117 top: top,
118 width: width,
119 // height 默认自适应
120 height: undefined,
121 flexDirection: 'row',
122 flexWrap: 'wrap',
123 alignItems: 'center',
124 justifyContent: 'flex-start'
125 };
126 // 如果只有一行,2端对齐
127 if (lineCount === 1) {
128 style.justifyContent = 'space-between';
129 }
130 if (position === 'top') {
131 style.height = customHeight ? customHeight : autoHeight;
132 }
133 if (position === 'left') {
134 style.flexDirection = 'column';
135 style.justifyContent = 'center';
136 style.width = itemMaxWidth;
137 style.height = customHeight ? customHeight : layoutHeight;
138 }
139 if (position === 'right') {
140 style.flexDirection = 'column';
141 style.alignItems = 'flex-start';
142 style.justifyContent = 'center';
143 style.width = itemMaxWidth;
144 style.height = customHeight ? customHeight : layoutHeight;
145 style.left = left + (width - itemMaxWidth);
146 }
147 if (position === 'bottom') {
148 style.top = top + (layoutHeight - autoHeight);
149 style.height = customHeight ? customHeight : autoHeight;
150 }
151 this.itemWidth = itemWidth;
152 this.legendStyle = style;
153 };
154 Legend.prototype.updateCoord = function () {
155 var _a = this,
156 context = _a.context,
157 props = _a.props,
158 legendStyle = _a.legendStyle;
159 var _b = props.position,
160 position = _b === void 0 ? 'top' : _b,
161 _c = props.margin,
162 margin = _c === void 0 ? '30px' : _c,
163 chart = props.chart;
164 var width = legendStyle.width,
165 height = legendStyle.height;
166 var marginNumber = context.px2hd(margin);
167 chart.updateCoordFor(this, {
168 position: position,
169 width: width + marginNumber,
170 height: height + marginNumber
171 });
172 };
173 Legend.prototype.willMount = function () {
174 var items = this.getItems();
175 if (!items || !items.length) return;
176 this._init();
177 this.updateCoord();
178 };
179 Legend.prototype.didMount = function () {
180 // this._initEvent();
181 };
182 Legend.prototype.willUpdate = function () {
183 var items = this.getItems();
184 if (!items || !items.length) return;
185 this.updateCoord();
186 };
187 Legend.prototype.render = function () {
188 var _a = this,
189 props = _a.props,
190 itemWidth = _a.itemWidth,
191 legendStyle = _a.legendStyle;
192 var items = this.getItems();
193 if (!items || !items.length) {
194 return null;
195 }
196 return jsx(View, __assign({}, props, {
197 items: items,
198 itemWidth: itemWidth,
199 style: __assign(__assign({}, legendStyle), props.style),
200 onClick: this._onclick
201 }));
202 };
203 return Legend;
204 }(Component);
205});
\No newline at end of file