TooltipHTMLContent.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { isString, indexOf, each, bind, isArray, isDom } from 'zrender/lib/core/util';
  41. import { toHex } from 'zrender/lib/tool/color';
  42. import { normalizeEvent } from 'zrender/lib/core/event';
  43. import { transformLocalCoord } from 'zrender/lib/core/dom';
  44. import env from 'zrender/lib/core/env';
  45. import { convertToColorString, toCamelCase, normalizeCssArray } from '../../util/format';
  46. import { shouldTooltipConfine, toCSSVendorPrefix, getComputedStyle, TRANSFORM_VENDOR, TRANSITION_VENDOR } from './helper';
  47. import { getPaddingFromTooltipModel } from './tooltipMarkup';
  48. /* global document, window */
  49. var CSS_TRANSITION_VENDOR = toCSSVendorPrefix(TRANSITION_VENDOR, 'transition');
  50. var CSS_TRANSFORM_VENDOR = toCSSVendorPrefix(TRANSFORM_VENDOR, 'transform'); // eslint-disable-next-line
  51. var gCssText = "position:absolute;display:block;border-style:solid;white-space:nowrap;z-index:9999999;" + (env.transform3dSupported ? 'will-change:transform;' : '');
  52. function mirrorPos(pos) {
  53. pos = pos === 'left' ? 'right' : pos === 'right' ? 'left' : pos === 'top' ? 'bottom' : 'top';
  54. return pos;
  55. }
  56. function assembleArrow(tooltipModel, borderColor, arrowPosition) {
  57. if (!isString(arrowPosition) || arrowPosition === 'inside') {
  58. return '';
  59. }
  60. var backgroundColor = tooltipModel.get('backgroundColor');
  61. var borderWidth = tooltipModel.get('borderWidth');
  62. borderColor = convertToColorString(borderColor);
  63. var arrowPos = mirrorPos(arrowPosition);
  64. var arrowSize = Math.max(Math.round(borderWidth) * 1.5, 6);
  65. var positionStyle = '';
  66. var transformStyle = CSS_TRANSFORM_VENDOR + ':';
  67. var rotateDeg;
  68. if (indexOf(['left', 'right'], arrowPos) > -1) {
  69. positionStyle += 'top:50%';
  70. transformStyle += "translateY(-50%) rotate(" + (rotateDeg = arrowPos === 'left' ? -225 : -45) + "deg)";
  71. } else {
  72. positionStyle += 'left:50%';
  73. transformStyle += "translateX(-50%) rotate(" + (rotateDeg = arrowPos === 'top' ? 225 : 45) + "deg)";
  74. }
  75. var rotateRadian = rotateDeg * Math.PI / 180;
  76. var arrowWH = arrowSize + borderWidth;
  77. var rotatedWH = arrowWH * Math.abs(Math.cos(rotateRadian)) + arrowWH * Math.abs(Math.sin(rotateRadian));
  78. var arrowOffset = Math.round(((rotatedWH - Math.SQRT2 * borderWidth) / 2 + Math.SQRT2 * borderWidth - (rotatedWH - arrowWH) / 2) * 100) / 100;
  79. positionStyle += ";" + arrowPos + ":-" + arrowOffset + "px";
  80. var borderStyle = borderColor + " solid " + borderWidth + "px;";
  81. var styleCss = ["position:absolute;width:" + arrowSize + "px;height:" + arrowSize + "px;", positionStyle + ";" + transformStyle + ";", "border-bottom:" + borderStyle, "border-right:" + borderStyle, "background-color:" + backgroundColor + ";"];
  82. return "<div style=\"" + styleCss.join('') + "\"></div>";
  83. }
  84. function assembleTransition(duration, onlyFade) {
  85. var transitionCurve = 'cubic-bezier(0.23,1,0.32,1)';
  86. var transitionOption = " " + duration / 2 + "s " + transitionCurve;
  87. var transitionText = "opacity" + transitionOption + ",visibility" + transitionOption;
  88. if (!onlyFade) {
  89. transitionOption = " " + duration + "s " + transitionCurve;
  90. transitionText += env.transformSupported ? "," + CSS_TRANSFORM_VENDOR + transitionOption : ",left" + transitionOption + ",top" + transitionOption;
  91. }
  92. return CSS_TRANSITION_VENDOR + ':' + transitionText;
  93. }
  94. function assembleTransform(x, y, toString) {
  95. // If using float on style, the final width of the dom might
  96. // keep changing slightly while mouse move. So `toFixed(0)` them.
  97. var x0 = x.toFixed(0) + 'px';
  98. var y0 = y.toFixed(0) + 'px'; // not support transform, use `left` and `top` instead.
  99. if (!env.transformSupported) {
  100. return toString ? "top:" + y0 + ";left:" + x0 + ";" : [['top', y0], ['left', x0]];
  101. } // support transform
  102. var is3d = env.transform3dSupported;
  103. var translate = "translate" + (is3d ? '3d' : '') + "(" + x0 + "," + y0 + (is3d ? ',0' : '') + ")";
  104. return toString ? 'top:0;left:0;' + CSS_TRANSFORM_VENDOR + ':' + translate + ';' : [['top', 0], ['left', 0], [TRANSFORM_VENDOR, translate]];
  105. }
  106. /**
  107. * @param {Object} textStyle
  108. * @return {string}
  109. * @inner
  110. */
  111. function assembleFont(textStyleModel) {
  112. var cssText = [];
  113. var fontSize = textStyleModel.get('fontSize');
  114. var color = textStyleModel.getTextColor();
  115. color && cssText.push('color:' + color);
  116. cssText.push('font:' + textStyleModel.getFont());
  117. fontSize // @ts-ignore, leave it to the tooltip refactor.
  118. && cssText.push('line-height:' + Math.round(fontSize * 3 / 2) + 'px');
  119. var shadowColor = textStyleModel.get('textShadowColor');
  120. var shadowBlur = textStyleModel.get('textShadowBlur') || 0;
  121. var shadowOffsetX = textStyleModel.get('textShadowOffsetX') || 0;
  122. var shadowOffsetY = textStyleModel.get('textShadowOffsetY') || 0;
  123. shadowColor && shadowBlur && cssText.push('text-shadow:' + shadowOffsetX + 'px ' + shadowOffsetY + 'px ' + shadowBlur + 'px ' + shadowColor);
  124. each(['decoration', 'align'], function (name) {
  125. var val = textStyleModel.get(name);
  126. val && cssText.push('text-' + name + ':' + val);
  127. });
  128. return cssText.join(';');
  129. }
  130. function assembleCssText(tooltipModel, enableTransition, onlyFade) {
  131. var cssText = [];
  132. var transitionDuration = tooltipModel.get('transitionDuration');
  133. var backgroundColor = tooltipModel.get('backgroundColor');
  134. var shadowBlur = tooltipModel.get('shadowBlur');
  135. var shadowColor = tooltipModel.get('shadowColor');
  136. var shadowOffsetX = tooltipModel.get('shadowOffsetX');
  137. var shadowOffsetY = tooltipModel.get('shadowOffsetY');
  138. var textStyleModel = tooltipModel.getModel('textStyle');
  139. var padding = getPaddingFromTooltipModel(tooltipModel, 'html');
  140. var boxShadow = shadowOffsetX + "px " + shadowOffsetY + "px " + shadowBlur + "px " + shadowColor;
  141. cssText.push('box-shadow:' + boxShadow); // Animation transition. Do not animate when transitionDuration is 0.
  142. enableTransition && transitionDuration && cssText.push(assembleTransition(transitionDuration, onlyFade));
  143. if (backgroundColor) {
  144. if (env.canvasSupported) {
  145. cssText.push('background-color:' + backgroundColor);
  146. } else {
  147. // for ie
  148. cssText.push('background-color:#' + toHex(backgroundColor));
  149. cssText.push('filter:alpha(opacity=70)');
  150. }
  151. } // Border style
  152. each(['width', 'color', 'radius'], function (name) {
  153. var borderName = 'border-' + name;
  154. var camelCase = toCamelCase(borderName);
  155. var val = tooltipModel.get(camelCase);
  156. val != null && cssText.push(borderName + ':' + val + (name === 'color' ? '' : 'px'));
  157. }); // Text style
  158. cssText.push(assembleFont(textStyleModel)); // Padding
  159. if (padding != null) {
  160. cssText.push('padding:' + normalizeCssArray(padding).join('px ') + 'px');
  161. }
  162. return cssText.join(';') + ';';
  163. } // If not able to make, do not modify the input `out`.
  164. function makeStyleCoord(out, zr, appendToBody, zrX, zrY) {
  165. var zrPainter = zr && zr.painter;
  166. if (appendToBody) {
  167. var zrViewportRoot = zrPainter && zrPainter.getViewportRoot();
  168. if (zrViewportRoot) {
  169. // Some APPs might use scale on body, so we support CSS transform here.
  170. transformLocalCoord(out, zrViewportRoot, document.body, zrX, zrY);
  171. }
  172. } else {
  173. out[0] = zrX;
  174. out[1] = zrY; // xy should be based on canvas root. But tooltipContent is
  175. // the sibling of canvas root. So padding of ec container
  176. // should be considered here.
  177. var viewportRootOffset = zrPainter && zrPainter.getViewportRootOffset();
  178. if (viewportRootOffset) {
  179. out[0] += viewportRootOffset.offsetLeft;
  180. out[1] += viewportRootOffset.offsetTop;
  181. }
  182. }
  183. out[2] = out[0] / zr.getWidth();
  184. out[3] = out[1] / zr.getHeight();
  185. }
  186. var TooltipHTMLContent =
  187. /** @class */
  188. function () {
  189. function TooltipHTMLContent(container, api, opt) {
  190. this._show = false;
  191. this._styleCoord = [0, 0, 0, 0];
  192. this._enterable = true;
  193. this._firstShow = true;
  194. this._longHide = true;
  195. if (env.wxa) {
  196. return null;
  197. }
  198. var el = document.createElement('div'); // TODO: TYPE
  199. el.domBelongToZr = true;
  200. this.el = el;
  201. var zr = this._zr = api.getZr();
  202. var appendToBody = this._appendToBody = opt && opt.appendToBody;
  203. makeStyleCoord(this._styleCoord, zr, appendToBody, api.getWidth() / 2, api.getHeight() / 2);
  204. if (appendToBody) {
  205. document.body.appendChild(el);
  206. } else {
  207. container.appendChild(el);
  208. }
  209. this._container = container; // FIXME
  210. // Is it needed to trigger zr event manually if
  211. // the browser do not support `pointer-events: none`.
  212. var self = this;
  213. el.onmouseenter = function () {
  214. // clear the timeout in hideLater and keep showing tooltip
  215. if (self._enterable) {
  216. clearTimeout(self._hideTimeout);
  217. self._show = true;
  218. }
  219. self._inContent = true;
  220. };
  221. el.onmousemove = function (e) {
  222. e = e || window.event;
  223. if (!self._enterable) {
  224. // `pointer-events: none` is set to tooltip content div
  225. // if `enterable` is set as `false`, and `el.onmousemove`
  226. // can not be triggered. But in browser that do not
  227. // support `pointer-events`, we need to do this:
  228. // Try trigger zrender event to avoid mouse
  229. // in and out shape too frequently
  230. var handler = zr.handler;
  231. var zrViewportRoot = zr.painter.getViewportRoot();
  232. normalizeEvent(zrViewportRoot, e, true);
  233. handler.dispatch('mousemove', e);
  234. }
  235. };
  236. el.onmouseleave = function () {
  237. // set `_inContent` to `false` before `hideLater`
  238. self._inContent = false;
  239. if (self._enterable) {
  240. if (self._show) {
  241. self.hideLater(self._hideDelay);
  242. }
  243. }
  244. };
  245. }
  246. /**
  247. * Update when tooltip is rendered
  248. */
  249. TooltipHTMLContent.prototype.update = function (tooltipModel) {
  250. // FIXME
  251. // Move this logic to ec main?
  252. var container = this._container;
  253. var position = getComputedStyle(container, 'position');
  254. var domStyle = container.style;
  255. if (domStyle.position !== 'absolute' && position !== 'absolute') {
  256. domStyle.position = 'relative';
  257. } // move tooltip if chart resized
  258. var alwaysShowContent = tooltipModel.get('alwaysShowContent');
  259. alwaysShowContent && this._moveIfResized(); // update className
  260. this.el.className = tooltipModel.get('className') || ''; // Hide the tooltip
  261. // PENDING
  262. // this.hide();
  263. };
  264. TooltipHTMLContent.prototype.show = function (tooltipModel, nearPointColor) {
  265. clearTimeout(this._hideTimeout);
  266. clearTimeout(this._longHideTimeout);
  267. var el = this.el;
  268. var style = el.style;
  269. var styleCoord = this._styleCoord;
  270. if (!el.innerHTML) {
  271. style.display = 'none';
  272. } else {
  273. style.cssText = gCssText + assembleCssText(tooltipModel, !this._firstShow, this._longHide) // initial transform
  274. + assembleTransform(styleCoord[0], styleCoord[1], true) + ("border-color:" + convertToColorString(nearPointColor) + ";") + (tooltipModel.get('extraCssText') || '') // If mouse occasionally move over the tooltip, a mouseout event will be
  275. // triggered by canvas, and cause some unexpectable result like dragging
  276. // stop, "unfocusAdjacency". Here `pointer-events: none` is used to solve
  277. // it. Although it is not supported by IE8~IE10, fortunately it is a rare
  278. // scenario.
  279. + (";pointer-events:" + (this._enterable ? 'auto' : 'none'));
  280. }
  281. this._show = true;
  282. this._firstShow = false;
  283. this._longHide = false;
  284. };
  285. TooltipHTMLContent.prototype.setContent = function (content, markers, tooltipModel, borderColor, arrowPosition) {
  286. var el = this.el;
  287. if (content == null) {
  288. el.innerHTML = '';
  289. return;
  290. }
  291. var arrow = '';
  292. if (isString(arrowPosition) && tooltipModel.get('trigger') === 'item' && !shouldTooltipConfine(tooltipModel)) {
  293. arrow = assembleArrow(tooltipModel, borderColor, arrowPosition);
  294. }
  295. if (isString(content)) {
  296. el.innerHTML = content + arrow;
  297. } else if (content) {
  298. // Clear previous
  299. el.innerHTML = '';
  300. if (!isArray(content)) {
  301. content = [content];
  302. }
  303. for (var i = 0; i < content.length; i++) {
  304. if (isDom(content[i]) && content[i].parentNode !== el) {
  305. el.appendChild(content[i]);
  306. }
  307. } // no arrow if empty
  308. if (arrow && el.childNodes.length) {
  309. // no need to create a new parent element, but it's not supported by IE 10 and older.
  310. // const arrowEl = document.createRange().createContextualFragment(arrow);
  311. var arrowEl = document.createElement('div');
  312. arrowEl.innerHTML = arrow;
  313. el.appendChild(arrowEl);
  314. }
  315. }
  316. };
  317. TooltipHTMLContent.prototype.setEnterable = function (enterable) {
  318. this._enterable = enterable;
  319. };
  320. TooltipHTMLContent.prototype.getSize = function () {
  321. var el = this.el;
  322. return [el.offsetWidth, el.offsetHeight];
  323. };
  324. TooltipHTMLContent.prototype.moveTo = function (zrX, zrY) {
  325. var styleCoord = this._styleCoord;
  326. makeStyleCoord(styleCoord, this._zr, this._appendToBody, zrX, zrY);
  327. if (styleCoord[0] != null && styleCoord[1] != null) {
  328. var style_1 = this.el.style;
  329. var transforms = assembleTransform(styleCoord[0], styleCoord[1]);
  330. each(transforms, function (transform) {
  331. style_1[transform[0]] = transform[1];
  332. });
  333. }
  334. };
  335. /**
  336. * when `alwaysShowContent` is true,
  337. * move the tooltip after chart resized
  338. */
  339. TooltipHTMLContent.prototype._moveIfResized = function () {
  340. // The ratio of left to width
  341. var ratioX = this._styleCoord[2]; // The ratio of top to height
  342. var ratioY = this._styleCoord[3];
  343. this.moveTo(ratioX * this._zr.getWidth(), ratioY * this._zr.getHeight());
  344. };
  345. TooltipHTMLContent.prototype.hide = function () {
  346. var _this = this;
  347. var style = this.el.style;
  348. style.visibility = 'hidden';
  349. style.opacity = '0';
  350. env.transform3dSupported && (style.willChange = '');
  351. this._show = false;
  352. this._longHideTimeout = setTimeout(function () {
  353. return _this._longHide = true;
  354. }, 500);
  355. };
  356. TooltipHTMLContent.prototype.hideLater = function (time) {
  357. if (this._show && !(this._inContent && this._enterable)) {
  358. if (time) {
  359. this._hideDelay = time; // Set show false to avoid invoke hideLater multiple times
  360. this._show = false;
  361. this._hideTimeout = setTimeout(bind(this.hide, this), time);
  362. } else {
  363. this.hide();
  364. }
  365. }
  366. };
  367. TooltipHTMLContent.prototype.isShow = function () {
  368. return this._show;
  369. };
  370. TooltipHTMLContent.prototype.dispose = function () {
  371. this.el.parentNode.removeChild(this.el);
  372. };
  373. return TooltipHTMLContent;
  374. }();
  375. export default TooltipHTMLContent;