TooltipContent.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. var zrUtil = require("zrender/lib/core/util");
  20. var zrColor = require("zrender/lib/tool/color");
  21. var eventUtil = require("zrender/lib/core/event");
  22. var domUtil = require("zrender/lib/core/dom");
  23. var env = require("zrender/lib/core/env");
  24. var formatUtil = require("../../util/format");
  25. /*
  26. * Licensed to the Apache Software Foundation (ASF) under one
  27. * or more contributor license agreements. See the NOTICE file
  28. * distributed with this work for additional information
  29. * regarding copyright ownership. The ASF licenses this file
  30. * to you under the Apache License, Version 2.0 (the
  31. * "License"); you may not use this file except in compliance
  32. * with the License. You may obtain a copy of the License at
  33. *
  34. * http://www.apache.org/licenses/LICENSE-2.0
  35. *
  36. * Unless required by applicable law or agreed to in writing,
  37. * software distributed under the License is distributed on an
  38. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  39. * KIND, either express or implied. See the License for the
  40. * specific language governing permissions and limitations
  41. * under the License.
  42. */
  43. var each = zrUtil.each;
  44. var toCamelCase = formatUtil.toCamelCase;
  45. var vendors = ['', '-webkit-', '-moz-', '-o-'];
  46. var gCssText = 'position:absolute;display:block;border-style:solid;white-space:nowrap;z-index:9999999;';
  47. /**
  48. * @param {number} duration
  49. * @return {string}
  50. * @inner
  51. */
  52. function assembleTransition(duration) {
  53. var transitionCurve = 'cubic-bezier(0.23, 1, 0.32, 1)';
  54. var transitionText = 'left ' + duration + 's ' + transitionCurve + ',' + 'top ' + duration + 's ' + transitionCurve;
  55. return zrUtil.map(vendors, function (vendorPrefix) {
  56. return vendorPrefix + 'transition:' + transitionText;
  57. }).join(';');
  58. }
  59. /**
  60. * @param {Object} textStyle
  61. * @return {string}
  62. * @inner
  63. */
  64. function assembleFont(textStyleModel) {
  65. var cssText = [];
  66. var fontSize = textStyleModel.get('fontSize');
  67. var color = textStyleModel.getTextColor();
  68. color && cssText.push('color:' + color);
  69. cssText.push('font:' + textStyleModel.getFont());
  70. var lineHeight = textStyleModel.get('lineHeight');
  71. if (lineHeight == null) {
  72. lineHeight = Math.round(fontSize * 3 / 2);
  73. }
  74. fontSize && cssText.push('line-height:' + lineHeight + 'px');
  75. var shadowColor = textStyleModel.get('textShadowColor');
  76. var shadowBlur = textStyleModel.get('textShadowBlur') || 0;
  77. var shadowOffsetX = textStyleModel.get('textShadowOffsetX') || 0;
  78. var shadowOffsetY = textStyleModel.get('textShadowOffsetY') || 0;
  79. shadowBlur && cssText.push('text-shadow:' + shadowOffsetX + 'px ' + shadowOffsetY + 'px ' + shadowBlur + 'px ' + shadowColor);
  80. each(['decoration', 'align'], function (name) {
  81. var val = textStyleModel.get(name);
  82. val && cssText.push('text-' + name + ':' + val);
  83. });
  84. return cssText.join(';');
  85. }
  86. /**
  87. * @param {Object} tooltipModel
  88. * @return {string}
  89. * @inner
  90. */
  91. function assembleCssText(tooltipModel) {
  92. var cssText = [];
  93. var transitionDuration = tooltipModel.get('transitionDuration');
  94. var backgroundColor = tooltipModel.get('backgroundColor');
  95. var textStyleModel = tooltipModel.getModel('textStyle');
  96. var padding = tooltipModel.get('padding'); // Animation transition. Do not animate when transitionDuration is 0.
  97. transitionDuration && cssText.push(assembleTransition(transitionDuration));
  98. if (backgroundColor) {
  99. if (env.canvasSupported) {
  100. cssText.push('background-Color:' + backgroundColor);
  101. } else {
  102. // for ie
  103. cssText.push('background-Color:#' + zrColor.toHex(backgroundColor));
  104. cssText.push('filter:alpha(opacity=70)');
  105. }
  106. } // Border style
  107. each(['width', 'color', 'radius'], function (name) {
  108. var borderName = 'border-' + name;
  109. var camelCase = toCamelCase(borderName);
  110. var val = tooltipModel.get(camelCase);
  111. val != null && cssText.push(borderName + ':' + val + (name === 'color' ? '' : 'px'));
  112. }); // Text style
  113. cssText.push(assembleFont(textStyleModel)); // Padding
  114. if (padding != null) {
  115. cssText.push('padding:' + formatUtil.normalizeCssArray(padding).join('px ') + 'px');
  116. }
  117. return cssText.join(';') + ';';
  118. } // If not able to make, do not modify the input `out`.
  119. function makeStyleCoord(out, zr, appendToBody, zrX, zrY) {
  120. var zrPainter = zr && zr.painter;
  121. if (appendToBody) {
  122. var zrViewportRoot = zrPainter && zrPainter.getViewportRoot();
  123. if (zrViewportRoot) {
  124. // Some APPs might use scale on body, so we support CSS transform here.
  125. domUtil.transformLocalCoord(out, zrViewportRoot, document.body, zrX, zrY);
  126. }
  127. } else {
  128. out[0] = zrX;
  129. out[1] = zrY; // xy should be based on canvas root. But tooltipContent is
  130. // the sibling of canvas root. So padding of ec container
  131. // should be considered here.
  132. var viewportRootOffset = zrPainter && zrPainter.getViewportRootOffset();
  133. if (viewportRootOffset) {
  134. out[0] += viewportRootOffset.offsetLeft;
  135. out[1] += viewportRootOffset.offsetTop;
  136. }
  137. }
  138. out[2] = out[0] / zr.getWidth(); // The ratio of left to width
  139. out[3] = out[1] / zr.getHeight(); // The ratio of top to height
  140. }
  141. /**
  142. * @alias module:echarts/component/tooltip/TooltipContent
  143. * @param {HTMLElement} container
  144. * @param {ExtensionAPI} api
  145. * @param {Object} [opt]
  146. * @param {boolean} [opt.appendToBody]
  147. * `false`: the DOM element will be inside the container. Default value.
  148. * `true`: the DOM element will be appended to HTML body, which avoid
  149. * some overflow clip but intrude outside of the container.
  150. * @constructor
  151. */
  152. function TooltipContent(container, api, opt) {
  153. if (env.wxa) {
  154. return null;
  155. }
  156. var el = document.createElement('div');
  157. el.domBelongToZr = true;
  158. this.el = el;
  159. var zr = this._zr = api.getZr();
  160. var appendToBody = this._appendToBody = opt && opt.appendToBody;
  161. this._styleCoord = [0, 0, 0, 0]; // [left, top, left/width, top/height]
  162. makeStyleCoord(this._styleCoord, zr, appendToBody, api.getWidth() / 2, api.getHeight() / 2);
  163. if (appendToBody) {
  164. document.body.appendChild(el);
  165. } else {
  166. container.appendChild(el);
  167. }
  168. this._container = container;
  169. this._show = false;
  170. /**
  171. * @private
  172. */
  173. this._hideTimeout; // FIXME
  174. // Is it needed to trigger zr event manually if
  175. // the browser do not support `pointer-events: none`.
  176. var self = this;
  177. el.onmouseenter = function () {
  178. // clear the timeout in hideLater and keep showing tooltip
  179. if (self._enterable) {
  180. clearTimeout(self._hideTimeout);
  181. self._show = true;
  182. }
  183. self._inContent = true;
  184. };
  185. el.onmousemove = function (e) {
  186. e = e || window.event;
  187. if (!self._enterable) {
  188. // `pointer-events: none` is set to tooltip content div
  189. // if `enterable` is set as `false`, and `el.onmousemove`
  190. // can not be triggered. But in browser that do not
  191. // support `pointer-events`, we need to do this:
  192. // Try trigger zrender event to avoid mouse
  193. // in and out shape too frequently
  194. var handler = zr.handler;
  195. var zrViewportRoot = zr.painter.getViewportRoot();
  196. eventUtil.normalizeEvent(zrViewportRoot, e, true);
  197. handler.dispatch('mousemove', e);
  198. }
  199. };
  200. el.onmouseleave = function () {
  201. if (self._enterable) {
  202. if (self._show) {
  203. self.hideLater(self._hideDelay);
  204. }
  205. }
  206. self._inContent = false;
  207. };
  208. }
  209. TooltipContent.prototype = {
  210. constructor: TooltipContent,
  211. /**
  212. * @private
  213. * @type {boolean}
  214. */
  215. _enterable: true,
  216. /**
  217. * Update when tooltip is rendered
  218. */
  219. update: function (tooltipModel) {
  220. // FIXME
  221. // Move this logic to ec main?
  222. var container = this._container;
  223. var stl = container.currentStyle || document.defaultView.getComputedStyle(container);
  224. var domStyle = container.style;
  225. if (domStyle.position !== 'absolute' && stl.position !== 'absolute') {
  226. domStyle.position = 'relative';
  227. }
  228. var alwaysShowContent = tooltipModel.get('alwaysShowContent');
  229. alwaysShowContent && this._moveTooltipIfResized(); // Hide the tooltip
  230. // PENDING
  231. // this.hide();
  232. },
  233. /**
  234. * when `alwaysShowContent` is true,
  235. * we should move the tooltip after chart resized
  236. */
  237. _moveTooltipIfResized: function () {
  238. var ratioX = this._styleCoord[2]; // The ratio of left to width
  239. var ratioY = this._styleCoord[3]; // The ratio of top to height
  240. var realX = ratioX * this._zr.getWidth();
  241. var realY = ratioY * this._zr.getHeight();
  242. this.moveTo(realX, realY);
  243. },
  244. show: function (tooltipModel) {
  245. clearTimeout(this._hideTimeout);
  246. var el = this.el;
  247. var styleCoord = this._styleCoord;
  248. el.style.cssText = gCssText + assembleCssText(tooltipModel) // Because of the reason described in:
  249. // http://stackoverflow.com/questions/21125587/css3-transition-not-working-in-chrome-anymore
  250. // we should set initial value to `left` and `top`.
  251. + ';left:' + styleCoord[0] + 'px;top:' + styleCoord[1] + 'px;' + (tooltipModel.get('extraCssText') || '');
  252. el.style.display = el.innerHTML ? 'block' : 'none'; // If mouse occasionally move over the tooltip, a mouseout event will be
  253. // triggered by canvas, and cause some unexpectable result like dragging
  254. // stop, "unfocusAdjacency". Here `pointer-events: none` is used to solve
  255. // it. Although it is not supported by IE8~IE10, fortunately it is a rare
  256. // scenario.
  257. el.style.pointerEvents = this._enterable ? 'auto' : 'none';
  258. this._show = true;
  259. },
  260. setContent: function (content) {
  261. this.el.innerHTML = content == null ? '' : content;
  262. },
  263. setEnterable: function (enterable) {
  264. this._enterable = enterable;
  265. },
  266. getSize: function () {
  267. var el = this.el;
  268. return [el.clientWidth, el.clientHeight];
  269. },
  270. moveTo: function (zrX, zrY) {
  271. var styleCoord = this._styleCoord;
  272. makeStyleCoord(styleCoord, this._zr, this._appendToBody, zrX, zrY);
  273. var style = this.el.style;
  274. style.left = styleCoord[0] + 'px';
  275. style.top = styleCoord[1] + 'px';
  276. },
  277. hide: function () {
  278. this.el.style.display = 'none';
  279. this._show = false;
  280. },
  281. hideLater: function (time) {
  282. if (this._show && !(this._inContent && this._enterable)) {
  283. if (time) {
  284. this._hideDelay = time; // Set show false to avoid invoke hideLater multiple times
  285. this._show = false;
  286. this._hideTimeout = setTimeout(zrUtil.bind(this.hide, this), time);
  287. } else {
  288. this.hide();
  289. }
  290. }
  291. },
  292. isShow: function () {
  293. return this._show;
  294. },
  295. dispose: function () {
  296. this.el.parentNode.removeChild(this.el);
  297. },
  298. getOuterSize: function () {
  299. var width = this.el.clientWidth;
  300. var height = this.el.clientHeight; // Consider browser compatibility.
  301. // IE8 does not support getComputedStyle.
  302. if (document.defaultView && document.defaultView.getComputedStyle) {
  303. var stl = document.defaultView.getComputedStyle(this.el);
  304. if (stl) {
  305. width += parseInt(stl.borderLeftWidth, 10) + parseInt(stl.borderRightWidth, 10);
  306. height += parseInt(stl.borderTopWidth, 10) + parseInt(stl.borderBottomWidth, 10);
  307. }
  308. }
  309. return {
  310. width: width,
  311. height: height
  312. };
  313. }
  314. };
  315. var _default = TooltipContent;
  316. module.exports = _default;