From bc292f72251b50d1149a3fbba090021289fbdd48 Mon Sep 17 00:00:00 2001 From: liulinboyi <814921718@qq.com> Date: Mon, 22 Jun 2020 20:39:11 +0800 Subject: [PATCH] fix: #12812 --- src/component/tooltip/TooltipContent.js | 20 +- src/component/tooltip/TooltipRichContent.js | 35 +- src/component/tooltip/TooltipView.js | 2 +- test/tooltip-windowResize.html | 383 ++++++++++++++++++++ 4 files changed, 433 insertions(+), 7 deletions(-) create mode 100644 test/tooltip-windowResize.html diff --git a/src/component/tooltip/TooltipContent.js b/src/component/tooltip/TooltipContent.js index 6cdbc3f282b..f49c57af2cf 100644 --- a/src/component/tooltip/TooltipContent.js +++ b/src/component/tooltip/TooltipContent.js @@ -145,6 +145,8 @@ function makeStyleCoord(out, zr, appendToBody, zrX, zrY) { out[1] += viewportRootOffset.offsetTop; } } + out[2] = out[0] / zr.getWidth(); // The ratio of left to width + out[3] = out[1] / zr.getHeight(); // The ratio of top to height } /** @@ -169,7 +171,7 @@ function TooltipContent(container, api, opt) { var zr = this._zr = api.getZr(); var appendToBody = this._appendToBody = opt && opt.appendToBody; - this._styleCoord = [0, 0]; + this._styleCoord = [0, 0, 0, 0]; // [left, top, left/width, top/height] makeStyleCoord(this._styleCoord, zr, appendToBody, api.getWidth() / 2, api.getHeight() / 2); @@ -240,7 +242,7 @@ TooltipContent.prototype = { /** * Update when tooltip is rendered */ - update: function () { + update: function (tooltipModel) { // FIXME // Move this logic to ec main? var container = this._container; @@ -250,11 +252,25 @@ TooltipContent.prototype = { if (domStyle.position !== 'absolute' && stl.position !== 'absolute') { domStyle.position = 'relative'; } + var alwaysShowContent = tooltipModel.get('alwaysShowContent'); + alwaysShowContent && this._moveTooltip(); // Hide the tooltip // PENDING // this.hide(); }, + /** + * when alwaysShowContent is true change or rotation window size and restore will move tooltip + * @private + */ + _moveTooltip: function () { + var ratioX = this._styleCoord[2]; // The ratio of left to width + var ratioY = this._styleCoord[3]; // The ratio of top to height + var realX = ratioX * this._zr.getWidth(); + var realY = ratioY * this._zr.getHeight(); + this.moveTo(realX, realY); + }, + show: function (tooltipModel) { clearTimeout(this._hideTimeout); var el = this.el; diff --git a/src/component/tooltip/TooltipRichContent.js b/src/component/tooltip/TooltipRichContent.js index 34c0f7516a6..c6f57160e45 100644 --- a/src/component/tooltip/TooltipRichContent.js +++ b/src/component/tooltip/TooltipRichContent.js @@ -21,13 +21,25 @@ import * as zrUtil from 'zrender/src/core/util'; // import Group from 'zrender/src/container/Group'; import Text from 'zrender/src/graphic/Text'; + +function makeStyleCoord(out, zr, zrX, zrY) { + out[0] = zrX; + out[1] = zrY; + out[2] = out[0] / zr.getWidth(); // The ratio of left to width + out[3] = out[1] / zr.getHeight(); // The ratio of top to height +} + /** * @alias module:echarts/component/tooltip/TooltipRichContent * @constructor */ function TooltipRichContent(api) { - this._zr = api.getZr(); + var zr = this._zr = api.getZr(); + + this._styleCoord = [0, 0, 0, 0]; // [left, top, left/width, top/height] + + makeStyleCoord(this._styleCoord, zr, api.getWidth() / 2, api.getHeight() / 2); this._show = false; @@ -50,8 +62,21 @@ TooltipRichContent.prototype = { /** * Update when tooltip is rendered */ - update: function () { - // noop + update: function (tooltipModel) { + var alwaysShowContent = tooltipModel.get('alwaysShowContent'); + alwaysShowContent && this._moveTooltip(); + }, + + /** + * when alwaysShowContent is true change or rotation window size and restore will move tooltip + * @private + */ + _moveTooltip: function () { + var ratioX = this._styleCoord[2]; // The ratio of left to width + var ratioY = this._styleCoord[3]; // The ratio of top to height + var realX = ratioX * this._zr.getWidth(); + var realY = ratioY * this._zr.getHeight(); + this.moveTo(realX, realY); }, show: function (tooltipModel) { @@ -150,7 +175,9 @@ TooltipRichContent.prototype = { moveTo: function (x, y) { if (this.el) { - this.el.attr('position', [x, y]); + var styleCoord = this._styleCoord; + makeStyleCoord(styleCoord, this._zr, x, y); + this.el.attr('position', [styleCoord[0], styleCoord[1]]); } }, diff --git a/src/component/tooltip/TooltipView.js b/src/component/tooltip/TooltipView.js index a7c980f96f1..f8963a36021 100644 --- a/src/component/tooltip/TooltipView.js +++ b/src/component/tooltip/TooltipView.js @@ -109,7 +109,7 @@ export default echarts.extendComponentView({ this._alwaysShowContent = tooltipModel.get('alwaysShowContent'); var tooltipContent = this._tooltipContent; - tooltipContent.update(); + tooltipContent.update(tooltipModel); tooltipContent.setEnterable(tooltipModel.get('enterable')); this._initGlobalListener(); diff --git a/test/tooltip-windowResize.html b/test/tooltip-windowResize.html new file mode 100644 index 00000000000..6e45e55c6e7 --- /dev/null +++ b/test/tooltip-windowResize.html @@ -0,0 +1,383 @@ + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +