1 | import { __assign, __extends } from "tslib";
|
2 | import { jsx, Component, isEqual } from '@antv/f-engine';
|
3 | import { isArray, isFunction, find } from '@antv/util';
|
4 | export default (function (View) {
|
5 | return function (_super) {
|
6 | __extends(Tooltip, _super);
|
7 | function Tooltip(props) {
|
8 | var _this = _super.call(this, props) || this;
|
9 | _this._triggerOn = function (ev) {
|
10 | var x = ev.x,
|
11 | y = ev.y;
|
12 | _this.show({
|
13 | x: x,
|
14 | y: y
|
15 | }, ev);
|
16 | };
|
17 | _this._triggerOff = function () {
|
18 | var _a = _this.props.alwaysShow,
|
19 | alwaysShow = _a === void 0 ? false : _a;
|
20 | if (!alwaysShow) {
|
21 | _this.hide();
|
22 | }
|
23 | };
|
24 | _this.state = {
|
25 | records: null
|
26 | };
|
27 | return _this;
|
28 | }
|
29 | Tooltip.prototype.updateCoord = function () {
|
30 | var _a = this,
|
31 | props = _a.props,
|
32 | context = _a.context;
|
33 | var _b = props.padding,
|
34 | padding = _b === void 0 ? '10px' : _b,
|
35 | chart = props.chart;
|
36 | chart.updateCoordFor(this, {
|
37 | position: 'top',
|
38 | width: 0,
|
39 | height: context.px2hd(padding)
|
40 | });
|
41 | };
|
42 | Tooltip.prototype.willMount = function () {
|
43 | this.updateCoord();
|
44 | };
|
45 | Tooltip.prototype.didMount = function () {
|
46 | this._initShow();
|
47 | this._initEvent();
|
48 | };
|
49 | Tooltip.prototype._initEvent = function () {
|
50 | var _a = this.props,
|
51 | chart = _a.chart,
|
52 | _b = _a.triggerOn,
|
53 | triggerOn = _b === void 0 ? 'press' : _b,
|
54 | _c = _a.triggerOff,
|
55 | triggerOff = _c === void 0 ? 'pressend' : _c;
|
56 | chart.on(triggerOn, this._triggerOn);
|
57 | chart.on(triggerOff, this._triggerOff);
|
58 | };
|
59 | Tooltip.prototype.willReceiveProps = function (nextProps) {
|
60 | var nextDefaultItem = nextProps.defaultItem,
|
61 | nextCoord = nextProps.coord;
|
62 | var _a = this.props,
|
63 | lastDefaultItem = _a.defaultItem,
|
64 | lastCoord = _a.coord;
|
65 |
|
66 | if (!isEqual(nextDefaultItem, lastDefaultItem) || !isEqual(nextCoord, lastCoord)) {
|
67 | this._showByData(nextDefaultItem);
|
68 | }
|
69 | };
|
70 | Tooltip.prototype._initShow = function () {
|
71 | var props = this.props;
|
72 | var defaultItem = props.defaultItem;
|
73 | this._showByData(defaultItem);
|
74 | };
|
75 | Tooltip.prototype._showByData = function (dataItem) {
|
76 | var _this = this;
|
77 | if (!dataItem) return;
|
78 | var props = this.props;
|
79 | var chart = props.chart;
|
80 |
|
81 | setTimeout(function () {
|
82 | var snapRecords = chart.getRecords(dataItem, 'xfield');
|
83 | _this.showSnapRecords(snapRecords);
|
84 | }, 0);
|
85 | };
|
86 | Tooltip.prototype.show = function (point, _ev) {
|
87 | var props = this.props;
|
88 | var chart = props.chart;
|
89 | var snapRecords = chart.getSnapRecords(point, true);
|
90 | if (!snapRecords || !snapRecords.length) return;
|
91 | this.showSnapRecords(snapRecords);
|
92 | };
|
93 | Tooltip.prototype.showSnapRecords = function (snapRecords) {
|
94 | var _a = this.props,
|
95 | chart = _a.chart,
|
96 | onChange = _a.onChange;
|
97 | var legendItems = chart.getLegendItems();
|
98 | var _b = snapRecords[0],
|
99 | xField = _b.xField,
|
100 | yField = _b.yField;
|
101 | var xScale = chart.getScale(xField);
|
102 | var yScale = chart.getScale(yField);
|
103 | var records = snapRecords.map(function (record) {
|
104 | var origin = record.origin,
|
105 | xField = record.xField,
|
106 | yField = record.yField;
|
107 | var value = isArray(origin[yField]) ? origin[yField].map(function (v) {
|
108 | return yScale.getText(v);
|
109 | }) : yScale.getText(origin[yField]);
|
110 |
|
111 | var name = yScale.alias;
|
112 | if (!name) {
|
113 | name = xScale.getText(origin[xField]);
|
114 | if (legendItems && legendItems.length) {
|
115 | var item = find(legendItems, function (item) {
|
116 | var field = item.field,
|
117 | tickValue = item.tickValue;
|
118 | return origin[field] === tickValue;
|
119 | });
|
120 | if (item && item.name) {
|
121 | name = item.name;
|
122 | }
|
123 | }
|
124 | }
|
125 | return __assign(__assign({}, record), {
|
126 | name: name,
|
127 | value: "".concat(value)
|
128 | });
|
129 | });
|
130 | if (!isArray(records) || !records.length) {
|
131 | return;
|
132 | }
|
133 | this.setState({
|
134 | records: records
|
135 | });
|
136 | if (isFunction(onChange)) {
|
137 | onChange(records);
|
138 | }
|
139 | };
|
140 | Tooltip.prototype.hide = function () {
|
141 | this.setState({
|
142 | records: null
|
143 | });
|
144 | };
|
145 | Tooltip.prototype.render = function () {
|
146 | var _a = this,
|
147 | props = _a.props,
|
148 | state = _a.state;
|
149 | var visible = props.visible;
|
150 | if (visible === false) {
|
151 | return null;
|
152 | }
|
153 | var records = state.records;
|
154 | return records && records.length && jsx(View, __assign({}, props, {
|
155 | records: records
|
156 | }));
|
157 | };
|
158 | return Tooltip;
|
159 | }(Component);
|
160 | }); |
\ | No newline at end of file |