UNPKG

6.77 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var util_1 = require("@antv/util");
5var graphics_1 = require("../../util/graphics");
6var base_1 = tslib_1.__importDefault(require("./base"));
7var DEFAULT_REGION_PATH_STYLE = {
8 fill: '#CCD6EC',
9 opacity: 0.3,
10};
11/**
12 * 背景框的 Action
13 * @ignore
14 */
15var ActiveRegion = /** @class */ (function (_super) {
16 tslib_1.__extends(ActiveRegion, _super);
17 function ActiveRegion() {
18 return _super !== null && _super.apply(this, arguments) || this;
19 }
20 /**
21 * 显示
22 * @param {ShapeAttrs} style region-path 的样式
23 */
24 ActiveRegion.prototype.show = function (args) {
25 var view = this.context.view;
26 var ev = this.context.event;
27 var tooltipItems = view.getTooltipItems({
28 x: ev.x,
29 y: ev.y,
30 });
31 if (util_1.isEqual(tooltipItems, this.items)) {
32 // 如果拾取数据同上次相同,则不重复绘制
33 return;
34 }
35 this.items = tooltipItems;
36 if (tooltipItems.length) {
37 var xField_1 = view.getXScale().field;
38 var xValue_1 = tooltipItems[0].data[xField_1];
39 // 根据 x 对应的值查找 elements
40 var elements_1 = [];
41 var geometries = view.geometries;
42 util_1.each(geometries, function (geometry) {
43 if (geometry.type === 'interval' || geometry.type === 'schema') {
44 var result = geometry.getElementsBy(function (ele) {
45 var eleData = ele.getData();
46 return eleData[xField_1] === xValue_1;
47 });
48 elements_1 = elements_1.concat(result);
49 }
50 });
51 // 根据 bbox 计算背景框的面积区域
52 if (elements_1.length) {
53 var coordinate_1 = view.getCoordinate();
54 var firstBBox_1 = elements_1[0].shape.getCanvasBBox();
55 var lastBBox_1 = elements_1[0].shape.getCanvasBBox();
56 var groupBBox_1 = firstBBox_1;
57 util_1.each(elements_1, function (ele) {
58 var bbox = ele.shape.getCanvasBBox();
59 if (coordinate_1.isTransposed) {
60 if (bbox.minY < firstBBox_1.minY) {
61 firstBBox_1 = bbox;
62 }
63 if (bbox.maxY > lastBBox_1.maxY) {
64 lastBBox_1 = bbox;
65 }
66 }
67 else {
68 if (bbox.minX < firstBBox_1.minX) {
69 firstBBox_1 = bbox;
70 }
71 if (bbox.maxX > lastBBox_1.maxX) {
72 lastBBox_1 = bbox;
73 }
74 }
75 groupBBox_1.x = Math.min(bbox.minX, groupBBox_1.minX);
76 groupBBox_1.y = Math.min(bbox.minY, groupBBox_1.minY);
77 groupBBox_1.width = Math.max(bbox.maxX, groupBBox_1.maxX) - groupBBox_1.x;
78 groupBBox_1.height = Math.max(bbox.maxY, groupBBox_1.maxY) - groupBBox_1.y;
79 });
80 var backgroundGroup = view.backgroundGroup, coordinateBBox = view.coordinateBBox;
81 var path = void 0;
82 if (coordinate_1.isRect) {
83 var xScale = view.getXScale();
84 var appendRatio = xScale.isLinear ? 0 : 0.25; // 如果 x 轴是数值类型,如直方图,不需要家额外的宽度
85 var minX = void 0;
86 var minY = void 0;
87 var width = void 0;
88 var height = void 0;
89 if (coordinate_1.isTransposed) {
90 minX = coordinateBBox.minX;
91 minY = Math.min(lastBBox_1.minY, firstBBox_1.minY) - appendRatio * lastBBox_1.height;
92 width = coordinateBBox.width;
93 height = groupBBox_1.height + appendRatio * 2 * lastBBox_1.height;
94 }
95 else {
96 minX = Math.min(firstBBox_1.minX, lastBBox_1.minX) - appendRatio * firstBBox_1.width;
97 minY = Math.min(coordinateBBox.minY, firstBBox_1.minY);
98 width = groupBBox_1.width + appendRatio * 2 * firstBBox_1.width;
99 height = coordinateBBox.height;
100 }
101 path = [
102 ['M', minX, minY],
103 ['L', minX + width, minY],
104 ['L', minX + width, minY + height],
105 ['L', minX, minY + height],
106 ['Z'],
107 ];
108 }
109 else {
110 var firstElement = util_1.head(elements_1);
111 var lastElement = util_1.last(elements_1);
112 var startAngle = graphics_1.getAngle(firstElement.getModel(), coordinate_1).startAngle;
113 var endAngle = graphics_1.getAngle(lastElement.getModel(), coordinate_1).endAngle;
114 var center = coordinate_1.getCenter();
115 var radius = coordinate_1.getRadius();
116 var innterRadius = coordinate_1.innerRadius * radius;
117 path = graphics_1.getSectorPath(center.x, center.y, radius, startAngle, endAngle, innterRadius);
118 }
119 if (this.regionPath) {
120 this.regionPath.attr('path', path);
121 this.regionPath.show();
122 }
123 else {
124 var style = util_1.get(args, 'style', DEFAULT_REGION_PATH_STYLE);
125 this.regionPath = backgroundGroup.addShape({
126 type: 'path',
127 name: 'active-region',
128 capture: false,
129 attrs: tslib_1.__assign(tslib_1.__assign({}, style), { path: path }),
130 });
131 }
132 }
133 }
134 };
135 /**
136 * 隐藏
137 */
138 ActiveRegion.prototype.hide = function () {
139 if (this.regionPath) {
140 this.regionPath.hide();
141 }
142 // this.regionPath = null;
143 this.items = null;
144 };
145 /**
146 * 销毁
147 */
148 ActiveRegion.prototype.destroy = function () {
149 this.hide();
150 if (this.regionPath) {
151 this.regionPath.remove(true);
152 }
153 _super.prototype.destroy.call(this);
154 };
155 return ActiveRegion;
156}(base_1.default));
157exports.default = ActiveRegion;
158//# sourceMappingURL=active-region.js.map
\No newline at end of file