UNPKG

14 kBJavaScriptView Raw
1import { __assign, __extends, __spreadArray } from "tslib";
2import { Component, jsx, computeLayout } from '@antv/f-engine';
3import { isFunction, find } from '@antv/util';
4// view 的默认配置
5var defaultStyle = {
6 showTitle: false,
7 showCrosshairs: false,
8 crosshairsType: 'y',
9 crosshairsStyle: {
10 stroke: 'rgba(0, 0, 0, 0.25)',
11 lineWidth: '2px'
12 },
13 showTooltipMarker: false,
14 markerBackgroundStyle: {
15 fill: '#CCD6EC',
16 opacity: 0.3,
17 padding: '6px'
18 },
19 tooltipMarkerStyle: {
20 fill: '#fff',
21 lineWidth: '3px'
22 },
23 background: {
24 radius: '4px',
25 fill: 'rgba(0, 0, 0, 0.65)',
26 padding: ['6px', '10px']
27 },
28 titleStyle: {
29 fontSize: '24px',
30 fill: '#fff',
31 textAlign: 'start',
32 textBaseline: 'top'
33 },
34 nameStyle: {
35 fontSize: '24px',
36 fill: 'rgba(255, 255, 255, 0.65)',
37 textAlign: 'start',
38 textBaseline: 'middle'
39 },
40 valueStyle: {
41 fontSize: '24px',
42 fill: '#fff',
43 textAlign: 'start',
44 textBaseline: 'middle'
45 },
46 joinString: ': ',
47 showItemMarker: true,
48 itemMarkerStyle: {
49 width: '12px',
50 radius: '6px',
51 symbol: 'circle',
52 lineWidth: '2px',
53 stroke: '#fff'
54 },
55 layout: 'horizontal',
56 snap: false,
57 xTipTextStyle: {
58 fontSize: '24px',
59 fill: '#fff'
60 },
61 yTipTextStyle: {
62 fontSize: '24px',
63 fill: '#fff'
64 },
65 xTipBackground: {
66 radius: '4px',
67 fill: 'rgba(0, 0, 0, 0.65)',
68 padding: ['6px', '10px'],
69 marginLeft: '-50%',
70 marginTop: '6px'
71 },
72 yTipBackground: {
73 radius: '4px',
74 fill: 'rgba(0, 0, 0, 0.65)',
75 padding: ['6px', '10px'],
76 marginLeft: '-100%',
77 marginTop: '-50%'
78 }
79};
80function directionEnabled(mode, dir) {
81 if (mode === undefined) {
82 return true;
83 } else if (typeof mode === 'string') {
84 return mode.indexOf(dir) !== -1;
85 }
86 return false;
87}
88var RenderItemMarker = function RenderItemMarker(props) {
89 var records = props.records,
90 coord = props.coord,
91 context = props.context,
92 markerBackgroundStyle = props.markerBackgroundStyle;
93 var point = coord.convertPoint({
94 x: 1,
95 y: 1
96 });
97 var padding = context.px2hd(markerBackgroundStyle.padding || '6px');
98 var xPoints = __spreadArray(__spreadArray([], records.map(function (record) {
99 return record.xMin;
100 }), true), records.map(function (record) {
101 return record.xMax;
102 }), true);
103 var yPoints = __spreadArray(__spreadArray([], records.map(function (record) {
104 return record.yMin;
105 }), true), records.map(function (record) {
106 return record.yMax;
107 }), true);
108 if (coord.transposed) {
109 xPoints.push(point.x);
110 } else {
111 yPoints.push(point.y);
112 }
113 var xMin = Math.min.apply(null, xPoints);
114 var xMax = Math.max.apply(null, xPoints);
115 var yMin = Math.min.apply(null, yPoints);
116 var yMax = Math.max.apply(null, yPoints);
117 var x = coord.transposed ? xMin : xMin - padding;
118 var y = coord.transposed ? yMin - padding : yMin;
119 var width = coord.transposed ? xMax - xMin : xMax - xMin + 2 * padding;
120 var height = coord.transposed ? yMax - yMin + 2 * padding : yMax - yMin;
121 return jsx("rect", {
122 style: __assign({
123 x: x,
124 y: y,
125 width: width,
126 height: height
127 }, markerBackgroundStyle)
128 });
129};
130var RenderCrosshairs = function RenderCrosshairs(props) {
131 var records = props.records,
132 coord = props.coord,
133 chart = props.chart,
134 crosshairsType = props.crosshairsType,
135 crosshairsStyle = props.crosshairsStyle,
136 xPositionType = props.xPositionType,
137 yPositionType = props.yPositionType;
138 var coordLeft = coord.left,
139 coordTop = coord.top,
140 coordRight = coord.right,
141 coordBottom = coord.bottom,
142 center = coord.center;
143 var firstRecord = records[0];
144 var x = firstRecord.x,
145 y = firstRecord.y,
146 origin = firstRecord.origin,
147 xField = firstRecord.xField,
148 coordData = firstRecord.coord;
149 if (coord.isPolar) {
150 // 极坐标下的辅助线
151 var xScale = chart.getScale(xField);
152 var ticks = xScale.getTicks();
153 var tick = find(ticks, function (tick) {
154 return origin[xField] === tick.tickValue;
155 });
156 var end = coord.convertPoint({
157 x: tick.value,
158 y: 1
159 });
160 return jsx("line", {
161 style: __assign({
162 x1: center.x,
163 y1: center.y,
164 x2: end.x,
165 y2: end.y
166 }, crosshairsStyle)
167 });
168 }
169 return jsx("group", null, directionEnabled(crosshairsType, 'x') ? jsx("line", {
170 style: __assign({
171 x1: coordLeft,
172 y1: yPositionType === 'coord' ? coordData.y : y,
173 x2: coordRight,
174 y2: yPositionType === 'coord' ? coordData.y : y
175 }, crosshairsStyle)
176 }) : null, directionEnabled(crosshairsType, 'y') ? jsx("line", {
177 style: __assign({
178 x1: xPositionType === 'coord' ? coordData.x : x,
179 y1: coordTop,
180 x2: xPositionType === 'coord' ? coordData.x : x,
181 y2: coordBottom
182 }, crosshairsStyle)
183 }) : null);
184};
185var RenderXTip = function RenderXTip(props) {
186 var records = props.records,
187 coord = props.coord,
188 xTip = props.xTip,
189 xPositionType = props.xPositionType,
190 xTipTextStyle = props.xTipTextStyle,
191 xTipBackground = props.xTipBackground;
192 var coordBottom = coord.bottom;
193 var firstRecord = records[0];
194 var x = firstRecord.x,
195 coordData = firstRecord.coord;
196 var xFirstText = firstRecord.name;
197 return jsx("rect", {
198 style: __assign({
199 display: 'flex',
200 left: xPositionType === 'coord' ? coordData.x : x,
201 top: coordBottom
202 }, xTipBackground)
203 }, jsx("text", {
204 style: __assign(__assign({}, xTipTextStyle), {
205 text: xPositionType === 'coord' ? coordData.xText : isFunction(xTip) ? xTip(xFirstText) : xFirstText
206 })
207 }));
208};
209var RenderYTip = function RenderYTip(props) {
210 var records = props.records,
211 coord = props.coord,
212 yTip = props.yTip,
213 yPositionType = props.yPositionType,
214 yTipTextStyle = props.yTipTextStyle,
215 yTipBackground = props.yTipBackground;
216 var coordLeft = coord.left;
217 var firstRecord = records[0];
218 var y = firstRecord.y,
219 coordData = firstRecord.coord;
220 var yFirstText = firstRecord.value;
221 return jsx("rect", {
222 style: __assign({
223 display: 'flex',
224 left: coordLeft,
225 top: yPositionType === 'coord' ? coordData.y : y
226 }, yTipBackground)
227 }, jsx("text", {
228 style: __assign(__assign({}, yTipTextStyle), {
229 text: yPositionType === 'coord' ? coordData.yText : isFunction(yTip) ? yTip(yFirstText) : yFirstText
230 })
231 }));
232};
233// tooltip 内容框
234var RenderLabel = /** @class */function (_super) {
235 __extends(RenderLabel, _super);
236 function RenderLabel() {
237 return _super !== null && _super.apply(this, arguments) || this;
238 }
239 RenderLabel.prototype.render = function () {
240 var _a = this.props,
241 records = _a.records,
242 background = _a.background,
243 showItemMarker = _a.showItemMarker,
244 itemMarkerStyle = _a.itemMarkerStyle,
245 customText = _a.customText,
246 nameStyle = _a.nameStyle,
247 valueStyle = _a.valueStyle,
248 joinString = _a.joinString,
249 arrowWidth = _a.arrowWidth,
250 x = _a.x,
251 coord = _a.coord;
252 // 显示内容
253 var labelView = function labelView(left, top) {
254 return jsx("group", {
255 style: {
256 display: 'flex'
257 }
258 }, jsx("group", {
259 style: __assign({
260 display: 'flex',
261 flexDirection: 'row',
262 flexWrap: 'wrap',
263 padding: [0, 0, 0, '6px'],
264 left: left,
265 top: top
266 }, background)
267 }, records.map(function (record) {
268 var name = record.name,
269 value = record.value;
270 return jsx("group", {
271 style: {
272 display: 'flex',
273 flexDirection: 'row',
274 alignItems: 'center',
275 padding: [0, '6px', 0, 0]
276 }
277 }, showItemMarker ? jsx("marker", {
278 style: __assign(__assign({
279 width: itemMarkerStyle.width,
280 marginRight: '6px'
281 }, itemMarkerStyle), {
282 fill: record.color
283 })
284 }) : null, customText && isFunction(customText) ? customText(record) : jsx("group", {
285 style: {
286 display: 'flex',
287 flexDirection: 'row'
288 }
289 }, jsx("text", {
290 style: __assign(__assign({}, nameStyle), {
291 text: value ? "".concat(name).concat(joinString) : name
292 })
293 }), jsx("text", {
294 style: __assign(__assign({}, valueStyle), {
295 text: value
296 })
297 })));
298 })), jsx("group", null, jsx("polygon", {
299 style: {
300 points: [[x - arrowWidth, top], [x + arrowWidth, top], [x, top + arrowWidth]],
301 fill: background.fill
302 }
303 })));
304 };
305 // 计算显示位置
306 var layout = computeLayout(this, labelView(0, 0)).layout; // 获取内容区大小
307 var coordLeft = coord.left,
308 coordTop = coord.top,
309 coordRight = coord.right;
310 var width = layout.width,
311 height = layout.height;
312 var halfWidth = width / 2;
313 // 让 tooltip 限制在 coord 的显示范围内
314 var advanceLeft = x - halfWidth;
315 var advanceTop = coordTop - height;
316 var left = advanceLeft < coordLeft ? coordLeft : advanceLeft > coordRight - width ? coordRight - width : advanceLeft;
317 var top = advanceTop < 0 ? 0 : advanceTop;
318 return labelView(left, top);
319 };
320 return RenderLabel;
321}(Component);
322var TooltipView = /** @class */function (_super) {
323 __extends(TooltipView, _super);
324 function TooltipView() {
325 return _super !== null && _super.apply(this, arguments) || this;
326 }
327 TooltipView.prototype.render = function () {
328 var _a = this,
329 props = _a.props,
330 context = _a.context;
331 var records = props.records,
332 coord = props.coord;
333 var firstRecord = records[0];
334 var x = firstRecord.x,
335 coordData = firstRecord.coord;
336 var chart = props.chart,
337 customBackground = props.background,
338 _b = props.showTooltipMarker,
339 showTooltipMarker = _b === void 0 ? defaultStyle.showTooltipMarker : _b,
340 _c = props.markerBackgroundStyle,
341 markerBackgroundStyle = _c === void 0 ? defaultStyle.markerBackgroundStyle : _c,
342 _d = props.showItemMarker,
343 showItemMarker = _d === void 0 ? defaultStyle.showItemMarker : _d,
344 customItemMarkerStyle = props.itemMarkerStyle,
345 nameStyle = props.nameStyle,
346 valueStyle = props.valueStyle,
347 _e = props.joinString,
348 joinString = _e === void 0 ? defaultStyle.joinString : _e,
349 _f = props.showCrosshairs,
350 showCrosshairs = _f === void 0 ? defaultStyle.showCrosshairs : _f,
351 crosshairsStyle = props.crosshairsStyle,
352 _g = props.crosshairsType,
353 crosshairsType = _g === void 0 ? defaultStyle.crosshairsType : _g,
354 _h = props.snap,
355 snap = _h === void 0 ? defaultStyle.snap : _h,
356 _j = props.tooltipMarkerStyle,
357 tooltipMarkerStyle = _j === void 0 ? defaultStyle.tooltipMarkerStyle : _j,
358 showXTip = props.showXTip,
359 xPositionType = props.xPositionType,
360 showYTip = props.showYTip,
361 yPositionType = props.yPositionType,
362 xTip = props.xTip,
363 yTip = props.yTip,
364 _k = props.xTipTextStyle,
365 xTipTextStyle = _k === void 0 ? defaultStyle.xTipTextStyle : _k,
366 _l = props.yTipTextStyle,
367 yTipTextStyle = _l === void 0 ? defaultStyle.yTipTextStyle : _l,
368 _m = props.xTipBackground,
369 xTipBackground = _m === void 0 ? defaultStyle.xTipBackground : _m,
370 _o = props.yTipBackground,
371 yTipBackground = _o === void 0 ? defaultStyle.yTipBackground : _o,
372 _p = props.custom,
373 custom = _p === void 0 ? false : _p,
374 customText = props.customText;
375 var itemMarkerStyle = __assign(__assign({}, customItemMarkerStyle), defaultStyle.itemMarkerStyle);
376 var background = __assign(__assign({}, defaultStyle.background), customBackground);
377 var arrowWidth = context.px2hd('6px');
378 return jsx("group", null, showTooltipMarker ? jsx(RenderItemMarker, {
379 coord: coord,
380 context: context,
381 records: records,
382 markerBackgroundStyle: markerBackgroundStyle
383 }) : null, showCrosshairs ? jsx(RenderCrosshairs, {
384 chart: chart,
385 coord: coord,
386 records: records,
387 xPositionType: xPositionType,
388 yPositionType: yPositionType,
389 crosshairsType: crosshairsType,
390 crosshairsStyle: __assign(__assign({}, defaultStyle.crosshairsStyle), crosshairsStyle)
391 }) : null, snap ? records.map(function (item) {
392 var x = item.x,
393 y = item.y,
394 color = item.color,
395 shape = item.shape;
396 return jsx("circle", {
397 style: __assign(__assign({
398 cx: xPositionType === 'coord' ? coordData.x : x,
399 cy: yPositionType === 'coord' ? coordData.y : y,
400 r: '6px',
401 stroke: color,
402 fill: color
403 }, shape), tooltipMarkerStyle)
404 });
405 }) : null, showXTip && jsx(RenderXTip, {
406 records: records,
407 coord: coord,
408 xTip: xTip,
409 xPositionType: xPositionType,
410 xTipTextStyle: __assign(__assign({}, defaultStyle.xTipTextStyle), xTipTextStyle),
411 xTipBackground: __assign(__assign({}, defaultStyle.xTipBackground), xTipBackground)
412 }), showYTip && jsx(RenderYTip, {
413 records: records,
414 coord: coord,
415 yTip: yTip,
416 yPositionType: yPositionType,
417 yTipTextStyle: __assign(__assign({}, defaultStyle.yTipTextStyle), yTipTextStyle),
418 yTipBackground: __assign(__assign({}, defaultStyle.yTipBackground), yTipBackground)
419 }), !custom && jsx(RenderLabel, {
420 records: records,
421 coord: coord,
422 itemMarkerStyle: itemMarkerStyle,
423 customText: customText,
424 showItemMarker: showItemMarker,
425 x: x,
426 arrowWidth: arrowWidth,
427 background: background,
428 nameStyle: __assign(__assign({}, defaultStyle.nameStyle), nameStyle),
429 valueStyle: __assign(__assign({}, defaultStyle.valueStyle), valueStyle),
430 joinString: joinString
431 }));
432 };
433 return TooltipView;
434}(Component);
435export default TooltipView;
\No newline at end of file