UNPKG

3.04 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import { jsx, Component } from '@antv/f-engine';
3import { isString, isNil, isFunction } from '@antv/util';
4import { computeLayout } from '@antv/f-engine';
5export default (function (View) {
6 return /** @class */function (_super) {
7 __extends(Guide, _super);
8 function Guide(props) {
9 return _super.call(this, props) || this;
10 }
11 Guide.prototype.getGuideBBox = function () {
12 var node = computeLayout(this, this.render());
13 var layout = node.layout;
14 if (!layout) return;
15 return layout;
16 };
17 // 解析record里的模板字符串,如min、max、50%...
18 Guide.prototype.parseReplaceStr = function (value, scale) {
19 var replaceMap = {
20 min: 0,
21 max: 1,
22 median: 0.5
23 };
24 // 传入的是 min、max、median 的
25 if (!isNil(replaceMap[value])) {
26 return replaceMap[value];
27 }
28 // 传入的是 xx%
29 if (isString(value) && value.indexOf('%') != -1 && !isNaN(Number(value.slice(0, -1)))) {
30 var rateValue = Number(value.slice(0, -1));
31 var percent = rateValue / 100;
32 return percent;
33 }
34 return scale.scale(value);
35 };
36 Guide.prototype.parsePoint = function (record) {
37 var props = this.props;
38 var chart = props.chart,
39 coord = props.coord;
40 var xScale = chart.getXScales()[0];
41 // 只取第一个yScale
42 var yScale = chart.getYScales()[0];
43 // 解析 record 为归一化后的坐标
44 var x = this.parseReplaceStr(record[xScale.field], xScale);
45 var y = this.parseReplaceStr(record[yScale.field], yScale);
46 return coord.convertPoint({
47 x: x,
48 y: y
49 });
50 };
51 Guide.prototype.convertPoints = function (records) {
52 var _this = this;
53 return records.map(function (record) {
54 return _this.parsePoint(record);
55 });
56 };
57 Guide.prototype.getGuideTheme = function () {
58 var context = this.context;
59 var theme = context.theme;
60 return theme.guide;
61 };
62 Guide.prototype.render = function () {
63 var _a = this,
64 props = _a.props,
65 context = _a.context;
66 var coord = props.coord,
67 _b = props.records,
68 records = _b === void 0 ? [] : _b,
69 animation = props.animation,
70 chart = props.chart,
71 style = props.style,
72 _onClick = props.onClick;
73 var width = context.width,
74 height = context.height;
75 var points = this.convertPoints(records);
76 var theme = this.getGuideTheme();
77 return jsx("group", {
78 onClick: function onClick(ev) {
79 _onClick && _onClick(ev);
80 }
81 }, jsx(View, __assign({
82 points: points,
83 theme: theme,
84 coord: coord
85 }, props, {
86 canvasWidth: width,
87 canvasHeight: height,
88 style: isFunction(style) ? style(points, chart) : style,
89 animation: isFunction(animation) ? animation(points, chart) : animation
90 })));
91 };
92 return Guide;
93 }(Component);
94});
\No newline at end of file