TooltipRichContent.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 Text = require("zrender/lib/graphic/Text");
  21. var graphicUtil = require("../../util/graphic");
  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 Group from 'zrender/src/container/Group';
  41. function makeStyleCoord(out, zr, zrX, zrY) {
  42. out[0] = zrX;
  43. out[1] = zrY;
  44. out[2] = out[0] / zr.getWidth(); // The ratio of left to width
  45. out[3] = out[1] / zr.getHeight(); // The ratio of top to height
  46. }
  47. /**
  48. * @alias module:echarts/component/tooltip/TooltipRichContent
  49. * @constructor
  50. */
  51. function TooltipRichContent(api) {
  52. var zr = this._zr = api.getZr();
  53. this._styleCoord = [0, 0, 0, 0]; // [left, top, left/width, top/height]
  54. makeStyleCoord(this._styleCoord, zr, api.getWidth() / 2, api.getHeight() / 2);
  55. this._show = false;
  56. /**
  57. * @private
  58. */
  59. this._hideTimeout;
  60. }
  61. TooltipRichContent.prototype = {
  62. constructor: TooltipRichContent,
  63. /**
  64. * @private
  65. * @type {boolean}
  66. */
  67. _enterable: true,
  68. /**
  69. * Update when tooltip is rendered
  70. */
  71. update: function (tooltipModel) {
  72. var alwaysShowContent = tooltipModel.get('alwaysShowContent');
  73. alwaysShowContent && this._moveTooltipIfResized();
  74. },
  75. /**
  76. * when `alwaysShowContent` is true,
  77. * we should move the tooltip after chart resized
  78. */
  79. _moveTooltipIfResized: function () {
  80. var ratioX = this._styleCoord[2]; // The ratio of left to width
  81. var ratioY = this._styleCoord[3]; // The ratio of top to height
  82. var realX = ratioX * this._zr.getWidth();
  83. var realY = ratioY * this._zr.getHeight();
  84. this.moveTo(realX, realY);
  85. },
  86. show: function (tooltipModel) {
  87. if (this._hideTimeout) {
  88. clearTimeout(this._hideTimeout);
  89. }
  90. this.el.attr('show', true);
  91. this._show = true;
  92. },
  93. /**
  94. * Set tooltip content
  95. *
  96. * @param {string} content rich text string of content
  97. * @param {Object} markerRich rich text style
  98. * @param {Object} tooltipModel tooltip model
  99. */
  100. setContent: function (content, markerRich, tooltipModel) {
  101. if (this.el) {
  102. this._zr.remove(this.el);
  103. }
  104. var markers = {};
  105. var text = content;
  106. var prefix = '{marker';
  107. var suffix = '|}';
  108. var startId = text.indexOf(prefix);
  109. while (startId >= 0) {
  110. var endId = text.indexOf(suffix);
  111. var name = text.substr(startId + prefix.length, endId - startId - prefix.length);
  112. if (name.indexOf('sub') > -1) {
  113. markers['marker' + name] = {
  114. textWidth: 4,
  115. textHeight: 4,
  116. textBorderRadius: 2,
  117. textBackgroundColor: markerRich[name],
  118. // TODO: textOffset is not implemented for rich text
  119. textOffset: [3, 0]
  120. };
  121. } else {
  122. markers['marker' + name] = {
  123. textWidth: 10,
  124. textHeight: 10,
  125. textBorderRadius: 5,
  126. textBackgroundColor: markerRich[name]
  127. };
  128. }
  129. text = text.substr(endId + 1);
  130. startId = text.indexOf('{marker');
  131. }
  132. var textStyleModel = tooltipModel.getModel('textStyle');
  133. var fontSize = textStyleModel.get('fontSize');
  134. var lineHeight = tooltipModel.get('textLineHeight');
  135. if (lineHeight == null) {
  136. lineHeight = Math.round(fontSize * 3 / 2);
  137. }
  138. this.el = new Text({
  139. style: graphicUtil.setTextStyle({}, textStyleModel, {
  140. rich: markers,
  141. text: content,
  142. textBackgroundColor: tooltipModel.get('backgroundColor'),
  143. textBorderRadius: tooltipModel.get('borderRadius'),
  144. textFill: tooltipModel.get('textStyle.color'),
  145. textPadding: tooltipModel.get('padding'),
  146. textLineHeight: lineHeight
  147. }),
  148. z: tooltipModel.get('z')
  149. });
  150. this._zr.add(this.el);
  151. var self = this;
  152. this.el.on('mouseover', function () {
  153. // clear the timeout in hideLater and keep showing tooltip
  154. if (self._enterable) {
  155. clearTimeout(self._hideTimeout);
  156. self._show = true;
  157. }
  158. self._inContent = true;
  159. });
  160. this.el.on('mouseout', function () {
  161. if (self._enterable) {
  162. if (self._show) {
  163. self.hideLater(self._hideDelay);
  164. }
  165. }
  166. self._inContent = false;
  167. });
  168. },
  169. setEnterable: function (enterable) {
  170. this._enterable = enterable;
  171. },
  172. getSize: function () {
  173. var bounding = this.el.getBoundingRect();
  174. return [bounding.width, bounding.height];
  175. },
  176. moveTo: function (x, y) {
  177. if (this.el) {
  178. var styleCoord = this._styleCoord;
  179. makeStyleCoord(styleCoord, this._zr, x, y);
  180. this.el.attr('position', [styleCoord[0], styleCoord[1]]);
  181. }
  182. },
  183. hide: function () {
  184. if (this.el) {
  185. this.el.hide();
  186. }
  187. this._show = false;
  188. },
  189. hideLater: function (time) {
  190. if (this._show && !(this._inContent && this._enterable)) {
  191. if (time) {
  192. this._hideDelay = time; // Set show false to avoid invoke hideLater multiple times
  193. this._show = false;
  194. this._hideTimeout = setTimeout(zrUtil.bind(this.hide, this), time);
  195. } else {
  196. this.hide();
  197. }
  198. }
  199. },
  200. isShow: function () {
  201. return this._show;
  202. },
  203. dispose: function () {
  204. clearTimeout(this._hideTimeout);
  205. if (this.el) {
  206. this._zr.remove(this.el);
  207. }
  208. },
  209. getOuterSize: function () {
  210. var size = this.getSize();
  211. return {
  212. width: size[0],
  213. height: size[1]
  214. };
  215. }
  216. };
  217. var _default = TooltipRichContent;
  218. module.exports = _default;