UNPKG

9.16 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getItemsOfView = void 0;
4var tslib_1 = require("tslib");
5var util_1 = require("@antv/util");
6var tooltip_1 = require("../../util/tooltip");
7var graphics_1 = require("../../util/graphics");
8var base_1 = (0, tslib_1.__importDefault)(require("./base"));
9var DEFAULT_REGION_PATH_STYLE = {
10 fill: '#CCD6EC',
11 opacity: 0.3,
12};
13function getItemsOfView(view, point, tooltipCfg) {
14 var items = (0, tooltip_1.findItemsFromViewRecurisive)(view, point, tooltipCfg);
15 if (items.length) {
16 // 三层
17 items = (0, util_1.flatten)(items);
18 for (var _i = 0, items_1 = items; _i < items_1.length; _i++) {
19 var itemArr = items_1[_i];
20 for (var _a = 0, itemArr_1 = itemArr; _a < itemArr_1.length; _a++) {
21 var item = itemArr_1[_a];
22 var _b = item.mappingData, x = _b.x, y = _b.y;
23 item.x = (0, util_1.isArray)(x) ? x[x.length - 1] : x;
24 item.y = (0, util_1.isArray)(y) ? y[y.length - 1] : y;
25 }
26 }
27 var shared = tooltipCfg.shared;
28 // shared: false 代表只显示当前拾取到的 shape 的数据,但是一个 view 会有多个 Geometry,所以有可能会拾取到多个 shape
29 if (shared === false && items.length > 1) {
30 var snapItem = items[0];
31 var min = Math.abs(point.y - snapItem[0].y);
32 for (var _c = 0, items_2 = items; _c < items_2.length; _c++) {
33 var aItem = items_2[_c];
34 var yDistance = Math.abs(point.y - aItem[0].y);
35 if (yDistance <= min) {
36 snapItem = aItem;
37 min = yDistance;
38 }
39 }
40 items = [snapItem];
41 }
42 return (0, util_1.uniq)((0, util_1.flatten)(items));
43 }
44 return [];
45}
46exports.getItemsOfView = getItemsOfView;
47/**
48 * 背景框的 Action. 只作用于 interval 和 schema geometry
49 * @ignore
50 */
51var ActiveRegion = /** @class */ (function (_super) {
52 (0, tslib_1.__extends)(ActiveRegion, _super);
53 function ActiveRegion() {
54 return _super !== null && _super.apply(this, arguments) || this;
55 }
56 /**
57 * 显示
58 * @param {ShapeAttrs} style region-path 的样式
59 * @param {number} appendRatio 适用于笛卡尔坐标系. 对于 x 轴非 linear 类型: 默认:0.25, x 轴 linear 类型: 默认 0
60 * @param {number} appendWidth 适用于笛卡尔坐标系. 像素级别,优先级 > appendRatio
61 */
62 ActiveRegion.prototype.show = function (args) {
63 var view = this.context.view;
64 var ev = this.context.event;
65 var tooltipCfg = view.getController('tooltip').getTooltipCfg();
66 var tooltipItems = getItemsOfView(view, {
67 x: ev.x,
68 y: ev.y,
69 }, tooltipCfg);
70 if ((0, util_1.isEqual)(tooltipItems, this.items)) {
71 // 如果拾取数据同上次相同,则不重复绘制
72 return;
73 }
74 this.items = tooltipItems;
75 if (tooltipItems.length) {
76 var xField_1 = view.getXScale().field;
77 var xValue_1 = tooltipItems[0].data[xField_1];
78 // 根据 x 对应的值查找 elements
79 var elements_1 = [];
80 var geometries = view.geometries;
81 (0, util_1.each)(geometries, function (geometry) {
82 if (geometry.type === 'interval' || geometry.type === 'schema') {
83 var result = geometry.getElementsBy(function (ele) {
84 var eleData = ele.getData();
85 return eleData[xField_1] === xValue_1;
86 });
87 elements_1 = elements_1.concat(result);
88 }
89 });
90 // 根据 bbox 计算背景框的面积区域
91 if (elements_1.length) {
92 var coordinate_1 = view.getCoordinate();
93 var firstBBox_1 = elements_1[0].shape.getCanvasBBox();
94 var lastBBox_1 = elements_1[0].shape.getCanvasBBox();
95 var groupBBox_1 = firstBBox_1;
96 (0, util_1.each)(elements_1, function (ele) {
97 var bbox = ele.shape.getCanvasBBox();
98 if (coordinate_1.isTransposed) {
99 if (bbox.minY < firstBBox_1.minY) {
100 firstBBox_1 = bbox;
101 }
102 if (bbox.maxY > lastBBox_1.maxY) {
103 lastBBox_1 = bbox;
104 }
105 }
106 else {
107 if (bbox.minX < firstBBox_1.minX) {
108 firstBBox_1 = bbox;
109 }
110 if (bbox.maxX > lastBBox_1.maxX) {
111 lastBBox_1 = bbox;
112 }
113 }
114 groupBBox_1.x = Math.min(bbox.minX, groupBBox_1.minX);
115 groupBBox_1.y = Math.min(bbox.minY, groupBBox_1.minY);
116 groupBBox_1.width = Math.max(bbox.maxX, groupBBox_1.maxX) - groupBBox_1.x;
117 groupBBox_1.height = Math.max(bbox.maxY, groupBBox_1.maxY) - groupBBox_1.y;
118 });
119 var backgroundGroup = view.backgroundGroup, coordinateBBox = view.coordinateBBox;
120 var path = void 0;
121 if (coordinate_1.isRect) {
122 var xScale = view.getXScale();
123 var _a = args || {}, appendRatio = _a.appendRatio, appendWidth = _a.appendWidth;
124 if ((0, util_1.isNil)(appendWidth)) {
125 appendRatio = (0, util_1.isNil)(appendRatio) ? (xScale.isLinear ? 0 : 0.25) : appendRatio; // 如果 x 轴是数值类型,如直方图,默认不需要加额外的宽度
126 appendWidth = coordinate_1.isTransposed ? appendRatio * lastBBox_1.height : appendRatio * firstBBox_1.width;
127 }
128 var minX = void 0;
129 var minY = void 0;
130 var width = void 0;
131 var height = void 0;
132 if (coordinate_1.isTransposed) {
133 minX = coordinateBBox.minX;
134 minY = Math.min(lastBBox_1.minY, firstBBox_1.minY) - appendWidth;
135 width = coordinateBBox.width;
136 height = groupBBox_1.height + appendWidth * 2;
137 }
138 else {
139 minX = Math.min(firstBBox_1.minX, lastBBox_1.minX) - appendWidth;
140 // 直角坐标系 非转置:最小值直接取 坐标系 minY
141 minY = coordinateBBox.minY;
142 width = groupBBox_1.width + appendWidth * 2;
143 height = coordinateBBox.height;
144 }
145 path = [
146 ['M', minX, minY],
147 ['L', minX + width, minY],
148 ['L', minX + width, minY + height],
149 ['L', minX, minY + height],
150 ['Z'],
151 ];
152 }
153 else {
154 var firstElement = (0, util_1.head)(elements_1);
155 var lastElement = (0, util_1.last)(elements_1);
156 var startAngle = (0, graphics_1.getAngle)(firstElement.getModel(), coordinate_1).startAngle;
157 var endAngle = (0, graphics_1.getAngle)(lastElement.getModel(), coordinate_1).endAngle;
158 var center = coordinate_1.getCenter();
159 var radius = coordinate_1.getRadius();
160 var innterRadius = coordinate_1.innerRadius * radius;
161 path = (0, graphics_1.getSectorPath)(center.x, center.y, radius, startAngle, endAngle, innterRadius);
162 }
163 if (this.regionPath) {
164 this.regionPath.attr('path', path);
165 this.regionPath.show();
166 }
167 else {
168 var style = (0, util_1.get)(args, 'style', DEFAULT_REGION_PATH_STYLE);
169 this.regionPath = backgroundGroup.addShape({
170 type: 'path',
171 name: 'active-region',
172 capture: false,
173 attrs: (0, tslib_1.__assign)((0, tslib_1.__assign)({}, style), { path: path }),
174 });
175 }
176 }
177 }
178 };
179 /**
180 * 隐藏
181 */
182 ActiveRegion.prototype.hide = function () {
183 if (this.regionPath) {
184 this.regionPath.hide();
185 }
186 // this.regionPath = null;
187 this.items = null;
188 };
189 /**
190 * 销毁
191 */
192 ActiveRegion.prototype.destroy = function () {
193 this.hide();
194 if (this.regionPath) {
195 this.regionPath.remove(true);
196 }
197 _super.prototype.destroy.call(this);
198 };
199 return ActiveRegion;
200}(base_1.default));
201exports.default = ActiveRegion;
202//# sourceMappingURL=active-region.js.map
\No newline at end of file