TooltipView.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 echarts = require("../../echarts");
  20. var zrUtil = require("zrender/lib/core/util");
  21. var env = require("zrender/lib/core/env");
  22. var TooltipContent = require("./TooltipContent");
  23. var TooltipRichContent = require("./TooltipRichContent");
  24. var formatUtil = require("../../util/format");
  25. var numberUtil = require("../../util/number");
  26. var graphic = require("../../util/graphic");
  27. var findPointFromSeries = require("../axisPointer/findPointFromSeries");
  28. var layoutUtil = require("../../util/layout");
  29. var Model = require("../../model/Model");
  30. var globalListener = require("../axisPointer/globalListener");
  31. var axisHelper = require("../../coord/axisHelper");
  32. var axisPointerViewHelper = require("../axisPointer/viewHelper");
  33. var _model = require("../../util/model");
  34. var getTooltipRenderMode = _model.getTooltipRenderMode;
  35. /*
  36. * Licensed to the Apache Software Foundation (ASF) under one
  37. * or more contributor license agreements. See the NOTICE file
  38. * distributed with this work for additional information
  39. * regarding copyright ownership. The ASF licenses this file
  40. * to you under the Apache License, Version 2.0 (the
  41. * "License"); you may not use this file except in compliance
  42. * with the License. You may obtain a copy of the License at
  43. *
  44. * http://www.apache.org/licenses/LICENSE-2.0
  45. *
  46. * Unless required by applicable law or agreed to in writing,
  47. * software distributed under the License is distributed on an
  48. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  49. * KIND, either express or implied. See the License for the
  50. * specific language governing permissions and limitations
  51. * under the License.
  52. */
  53. var bind = zrUtil.bind;
  54. var each = zrUtil.each;
  55. var parsePercent = numberUtil.parsePercent;
  56. var proxyRect = new graphic.Rect({
  57. shape: {
  58. x: -1,
  59. y: -1,
  60. width: 2,
  61. height: 2
  62. }
  63. });
  64. var _default = echarts.extendComponentView({
  65. type: 'tooltip',
  66. init: function (ecModel, api) {
  67. if (env.node) {
  68. return;
  69. }
  70. var tooltipModel = ecModel.getComponent('tooltip');
  71. var renderMode = tooltipModel.get('renderMode');
  72. this._renderMode = getTooltipRenderMode(renderMode);
  73. var tooltipContent;
  74. if (this._renderMode === 'html') {
  75. tooltipContent = new TooltipContent(api.getDom(), api, {
  76. appendToBody: tooltipModel.get('appendToBody', true)
  77. });
  78. this._newLine = '<br/>';
  79. } else {
  80. tooltipContent = new TooltipRichContent(api);
  81. this._newLine = '\n';
  82. }
  83. this._tooltipContent = tooltipContent;
  84. },
  85. render: function (tooltipModel, ecModel, api) {
  86. if (env.node) {
  87. return;
  88. } // Reset
  89. this.group.removeAll();
  90. /**
  91. * @private
  92. * @type {module:echarts/component/tooltip/TooltipModel}
  93. */
  94. this._tooltipModel = tooltipModel;
  95. /**
  96. * @private
  97. * @type {module:echarts/model/Global}
  98. */
  99. this._ecModel = ecModel;
  100. /**
  101. * @private
  102. * @type {module:echarts/ExtensionAPI}
  103. */
  104. this._api = api;
  105. /**
  106. * Should be cleaned when render.
  107. * @private
  108. * @type {Array.<Array.<Object>>}
  109. */
  110. this._lastDataByCoordSys = null;
  111. /**
  112. * @private
  113. * @type {boolean}
  114. */
  115. this._alwaysShowContent = tooltipModel.get('alwaysShowContent');
  116. var tooltipContent = this._tooltipContent;
  117. tooltipContent.update(tooltipModel);
  118. tooltipContent.setEnterable(tooltipModel.get('enterable'));
  119. this._initGlobalListener();
  120. this._keepShow();
  121. },
  122. _initGlobalListener: function () {
  123. var tooltipModel = this._tooltipModel;
  124. var triggerOn = tooltipModel.get('triggerOn');
  125. globalListener.register('itemTooltip', this._api, bind(function (currTrigger, e, dispatchAction) {
  126. // If 'none', it is not controlled by mouse totally.
  127. if (triggerOn !== 'none') {
  128. if (triggerOn.indexOf(currTrigger) >= 0) {
  129. this._tryShow(e, dispatchAction);
  130. } else if (currTrigger === 'leave') {
  131. this._hide(dispatchAction);
  132. }
  133. }
  134. }, this));
  135. },
  136. _keepShow: function () {
  137. var tooltipModel = this._tooltipModel;
  138. var ecModel = this._ecModel;
  139. var api = this._api; // Try to keep the tooltip show when refreshing
  140. if (this._lastX != null && this._lastY != null // When user is willing to control tooltip totally using API,
  141. // self.manuallyShowTip({x, y}) might cause tooltip hide,
  142. // which is not expected.
  143. && tooltipModel.get('triggerOn') !== 'none') {
  144. var self = this;
  145. clearTimeout(this._refreshUpdateTimeout);
  146. this._refreshUpdateTimeout = setTimeout(function () {
  147. // Show tip next tick after other charts are rendered
  148. // In case highlight action has wrong result
  149. // FIXME
  150. !api.isDisposed() && self.manuallyShowTip(tooltipModel, ecModel, api, {
  151. x: self._lastX,
  152. y: self._lastY
  153. });
  154. });
  155. }
  156. },
  157. /**
  158. * Show tip manually by
  159. * dispatchAction({
  160. * type: 'showTip',
  161. * x: 10,
  162. * y: 10
  163. * });
  164. * Or
  165. * dispatchAction({
  166. * type: 'showTip',
  167. * seriesIndex: 0,
  168. * dataIndex or dataIndexInside or name
  169. * });
  170. *
  171. * TODO Batch
  172. */
  173. manuallyShowTip: function (tooltipModel, ecModel, api, payload) {
  174. if (payload.from === this.uid || env.node) {
  175. return;
  176. }
  177. var dispatchAction = makeDispatchAction(payload, api); // Reset ticket
  178. this._ticket = ''; // When triggered from axisPointer.
  179. var dataByCoordSys = payload.dataByCoordSys;
  180. if (payload.tooltip && payload.x != null && payload.y != null) {
  181. var el = proxyRect;
  182. el.position = [payload.x, payload.y];
  183. el.update();
  184. el.tooltip = payload.tooltip; // Manually show tooltip while view is not using zrender elements.
  185. this._tryShow({
  186. offsetX: payload.x,
  187. offsetY: payload.y,
  188. target: el
  189. }, dispatchAction);
  190. } else if (dataByCoordSys) {
  191. this._tryShow({
  192. offsetX: payload.x,
  193. offsetY: payload.y,
  194. position: payload.position,
  195. dataByCoordSys: payload.dataByCoordSys,
  196. tooltipOption: payload.tooltipOption
  197. }, dispatchAction);
  198. } else if (payload.seriesIndex != null) {
  199. if (this._manuallyAxisShowTip(tooltipModel, ecModel, api, payload)) {
  200. return;
  201. }
  202. var pointInfo = findPointFromSeries(payload, ecModel);
  203. var cx = pointInfo.point[0];
  204. var cy = pointInfo.point[1];
  205. if (cx != null && cy != null) {
  206. this._tryShow({
  207. offsetX: cx,
  208. offsetY: cy,
  209. position: payload.position,
  210. target: pointInfo.el
  211. }, dispatchAction);
  212. }
  213. } else if (payload.x != null && payload.y != null) {
  214. // FIXME
  215. // should wrap dispatchAction like `axisPointer/globalListener` ?
  216. api.dispatchAction({
  217. type: 'updateAxisPointer',
  218. x: payload.x,
  219. y: payload.y
  220. });
  221. this._tryShow({
  222. offsetX: payload.x,
  223. offsetY: payload.y,
  224. position: payload.position,
  225. target: api.getZr().findHover(payload.x, payload.y).target
  226. }, dispatchAction);
  227. }
  228. },
  229. manuallyHideTip: function (tooltipModel, ecModel, api, payload) {
  230. var tooltipContent = this._tooltipContent;
  231. if (!this._alwaysShowContent && this._tooltipModel) {
  232. tooltipContent.hideLater(this._tooltipModel.get('hideDelay'));
  233. }
  234. this._lastX = this._lastY = null;
  235. if (payload.from !== this.uid) {
  236. this._hide(makeDispatchAction(payload, api));
  237. }
  238. },
  239. // Be compatible with previous design, that is, when tooltip.type is 'axis' and
  240. // dispatchAction 'showTip' with seriesIndex and dataIndex will trigger axis pointer
  241. // and tooltip.
  242. _manuallyAxisShowTip: function (tooltipModel, ecModel, api, payload) {
  243. var seriesIndex = payload.seriesIndex;
  244. var dataIndex = payload.dataIndex;
  245. var coordSysAxesInfo = ecModel.getComponent('axisPointer').coordSysAxesInfo;
  246. if (seriesIndex == null || dataIndex == null || coordSysAxesInfo == null) {
  247. return;
  248. }
  249. var seriesModel = ecModel.getSeriesByIndex(seriesIndex);
  250. if (!seriesModel) {
  251. return;
  252. }
  253. var data = seriesModel.getData();
  254. var tooltipModel = buildTooltipModel([data.getItemModel(dataIndex), seriesModel, (seriesModel.coordinateSystem || {}).model, tooltipModel]);
  255. if (tooltipModel.get('trigger') !== 'axis') {
  256. return;
  257. }
  258. api.dispatchAction({
  259. type: 'updateAxisPointer',
  260. seriesIndex: seriesIndex,
  261. dataIndex: dataIndex,
  262. position: payload.position
  263. });
  264. return true;
  265. },
  266. _tryShow: function (e, dispatchAction) {
  267. var el = e.target;
  268. var tooltipModel = this._tooltipModel;
  269. if (!tooltipModel) {
  270. return;
  271. } // Save mouse x, mouse y. So we can try to keep showing the tip if chart is refreshed
  272. this._lastX = e.offsetX;
  273. this._lastY = e.offsetY;
  274. var dataByCoordSys = e.dataByCoordSys;
  275. if (dataByCoordSys && dataByCoordSys.length) {
  276. this._showAxisTooltip(dataByCoordSys, e);
  277. } // Always show item tooltip if mouse is on the element with dataIndex
  278. else if (el && el.dataIndex != null) {
  279. this._lastDataByCoordSys = null;
  280. this._showSeriesItemTooltip(e, el, dispatchAction);
  281. } // Tooltip provided directly. Like legend.
  282. else if (el && el.tooltip) {
  283. this._lastDataByCoordSys = null;
  284. this._showComponentItemTooltip(e, el, dispatchAction);
  285. } else {
  286. this._lastDataByCoordSys = null;
  287. this._hide(dispatchAction);
  288. }
  289. },
  290. _showOrMove: function (tooltipModel, cb) {
  291. // showDelay is used in this case: tooltip.enterable is set
  292. // as true. User intent to move mouse into tooltip and click
  293. // something. `showDelay` makes it easier to enter the content
  294. // but tooltip do not move immediately.
  295. var delay = tooltipModel.get('showDelay');
  296. cb = zrUtil.bind(cb, this);
  297. clearTimeout(this._showTimout);
  298. delay > 0 ? this._showTimout = setTimeout(cb, delay) : cb();
  299. },
  300. _showAxisTooltip: function (dataByCoordSys, e) {
  301. var ecModel = this._ecModel;
  302. var globalTooltipModel = this._tooltipModel;
  303. var point = [e.offsetX, e.offsetY];
  304. var singleDefaultHTML = [];
  305. var singleParamsList = [];
  306. var singleTooltipModel = buildTooltipModel([e.tooltipOption, globalTooltipModel]);
  307. var renderMode = this._renderMode;
  308. var newLine = this._newLine;
  309. var markers = {};
  310. each(dataByCoordSys, function (itemCoordSys) {
  311. // var coordParamList = [];
  312. // var coordDefaultHTML = [];
  313. // var coordTooltipModel = buildTooltipModel([
  314. // e.tooltipOption,
  315. // itemCoordSys.tooltipOption,
  316. // ecModel.getComponent(itemCoordSys.coordSysMainType, itemCoordSys.coordSysIndex),
  317. // globalTooltipModel
  318. // ]);
  319. // var displayMode = coordTooltipModel.get('displayMode');
  320. // var paramsList = displayMode === 'single' ? singleParamsList : [];
  321. each(itemCoordSys.dataByAxis, function (item) {
  322. var axisModel = ecModel.getComponent(item.axisDim + 'Axis', item.axisIndex);
  323. var axisValue = item.value;
  324. var seriesDefaultHTML = [];
  325. if (!axisModel || axisValue == null) {
  326. return;
  327. }
  328. var valueLabel = axisPointerViewHelper.getValueLabel(axisValue, axisModel.axis, ecModel, item.seriesDataIndices, item.valueLabelOpt);
  329. zrUtil.each(item.seriesDataIndices, function (idxItem) {
  330. var series = ecModel.getSeriesByIndex(idxItem.seriesIndex);
  331. var dataIndex = idxItem.dataIndexInside;
  332. var dataParams = series && series.getDataParams(dataIndex);
  333. dataParams.axisDim = item.axisDim;
  334. dataParams.axisIndex = item.axisIndex;
  335. dataParams.axisType = item.axisType;
  336. dataParams.axisId = item.axisId;
  337. dataParams.axisValue = axisHelper.getAxisRawValue(axisModel.axis, axisValue);
  338. dataParams.axisValueLabel = valueLabel;
  339. if (dataParams) {
  340. singleParamsList.push(dataParams);
  341. var seriesTooltip = series.formatTooltip(dataIndex, true, null, renderMode);
  342. var html;
  343. if (zrUtil.isObject(seriesTooltip)) {
  344. html = seriesTooltip.html;
  345. var newMarkers = seriesTooltip.markers;
  346. zrUtil.merge(markers, newMarkers);
  347. } else {
  348. html = seriesTooltip;
  349. }
  350. seriesDefaultHTML.push(html);
  351. }
  352. }); // Default tooltip content
  353. // FIXME
  354. // (1) should be the first data which has name?
  355. // (2) themeRiver, firstDataIndex is array, and first line is unnecessary.
  356. var firstLine = valueLabel;
  357. if (renderMode !== 'html') {
  358. singleDefaultHTML.push(seriesDefaultHTML.join(newLine));
  359. } else {
  360. singleDefaultHTML.push((firstLine ? formatUtil.encodeHTML(firstLine) + newLine : '') + seriesDefaultHTML.join(newLine));
  361. }
  362. });
  363. }, this); // In most case, the second axis is shown upper than the first one.
  364. singleDefaultHTML.reverse();
  365. singleDefaultHTML = singleDefaultHTML.join(this._newLine + this._newLine);
  366. var positionExpr = e.position;
  367. this._showOrMove(singleTooltipModel, function () {
  368. if (this._updateContentNotChangedOnAxis(dataByCoordSys)) {
  369. this._updatePosition(singleTooltipModel, positionExpr, point[0], point[1], this._tooltipContent, singleParamsList);
  370. } else {
  371. this._showTooltipContent(singleTooltipModel, singleDefaultHTML, singleParamsList, Math.random(), point[0], point[1], positionExpr, undefined, markers);
  372. }
  373. }); // Do not trigger events here, because this branch only be entered
  374. // from dispatchAction.
  375. },
  376. _showSeriesItemTooltip: function (e, el, dispatchAction) {
  377. var ecModel = this._ecModel; // Use dataModel in element if possible
  378. // Used when mouseover on a element like markPoint or edge
  379. // In which case, the data is not main data in series.
  380. var seriesIndex = el.seriesIndex;
  381. var seriesModel = ecModel.getSeriesByIndex(seriesIndex); // For example, graph link.
  382. var dataModel = el.dataModel || seriesModel;
  383. var dataIndex = el.dataIndex;
  384. var dataType = el.dataType;
  385. var data = dataModel.getData(dataType);
  386. var tooltipModel = buildTooltipModel([data.getItemModel(dataIndex), dataModel, seriesModel && (seriesModel.coordinateSystem || {}).model, this._tooltipModel]);
  387. var tooltipTrigger = tooltipModel.get('trigger');
  388. if (tooltipTrigger != null && tooltipTrigger !== 'item') {
  389. return;
  390. }
  391. var params = dataModel.getDataParams(dataIndex, dataType);
  392. var seriesTooltip = dataModel.formatTooltip(dataIndex, false, dataType, this._renderMode);
  393. var defaultHtml;
  394. var markers;
  395. if (zrUtil.isObject(seriesTooltip)) {
  396. defaultHtml = seriesTooltip.html;
  397. markers = seriesTooltip.markers;
  398. } else {
  399. defaultHtml = seriesTooltip;
  400. markers = null;
  401. }
  402. var asyncTicket = 'item_' + dataModel.name + '_' + dataIndex;
  403. this._showOrMove(tooltipModel, function () {
  404. this._showTooltipContent(tooltipModel, defaultHtml, params, asyncTicket, e.offsetX, e.offsetY, e.position, e.target, markers);
  405. }); // FIXME
  406. // duplicated showtip if manuallyShowTip is called from dispatchAction.
  407. dispatchAction({
  408. type: 'showTip',
  409. dataIndexInside: dataIndex,
  410. dataIndex: data.getRawIndex(dataIndex),
  411. seriesIndex: seriesIndex,
  412. from: this.uid
  413. });
  414. },
  415. _showComponentItemTooltip: function (e, el, dispatchAction) {
  416. var tooltipOpt = el.tooltip;
  417. if (typeof tooltipOpt === 'string') {
  418. var content = tooltipOpt;
  419. tooltipOpt = {
  420. content: content,
  421. // Fixed formatter
  422. formatter: content
  423. };
  424. }
  425. var subTooltipModel = new Model(tooltipOpt, this._tooltipModel, this._ecModel);
  426. var defaultHtml = subTooltipModel.get('content');
  427. var asyncTicket = Math.random(); // Do not check whether `trigger` is 'none' here, because `trigger`
  428. // only works on coordinate system. In fact, we have not found case
  429. // that requires setting `trigger` nothing on component yet.
  430. this._showOrMove(subTooltipModel, function () {
  431. this._showTooltipContent(subTooltipModel, defaultHtml, subTooltipModel.get('formatterParams') || {}, asyncTicket, e.offsetX, e.offsetY, e.position, el);
  432. }); // If not dispatch showTip, tip may be hide triggered by axis.
  433. dispatchAction({
  434. type: 'showTip',
  435. from: this.uid
  436. });
  437. },
  438. _showTooltipContent: function (tooltipModel, defaultHtml, params, asyncTicket, x, y, positionExpr, el, markers) {
  439. // Reset ticket
  440. this._ticket = '';
  441. if (!tooltipModel.get('showContent') || !tooltipModel.get('show')) {
  442. return;
  443. }
  444. var tooltipContent = this._tooltipContent;
  445. var formatter = tooltipModel.get('formatter');
  446. positionExpr = positionExpr || tooltipModel.get('position');
  447. var html = defaultHtml;
  448. if (formatter && typeof formatter === 'string') {
  449. html = formatUtil.formatTpl(formatter, params, true);
  450. } else if (typeof formatter === 'function') {
  451. var callback = bind(function (cbTicket, html) {
  452. if (cbTicket === this._ticket) {
  453. tooltipContent.setContent(html, markers, tooltipModel);
  454. this._updatePosition(tooltipModel, positionExpr, x, y, tooltipContent, params, el);
  455. }
  456. }, this);
  457. this._ticket = asyncTicket;
  458. html = formatter(params, asyncTicket, callback);
  459. }
  460. tooltipContent.setContent(html, markers, tooltipModel);
  461. tooltipContent.show(tooltipModel);
  462. this._updatePosition(tooltipModel, positionExpr, x, y, tooltipContent, params, el);
  463. },
  464. /**
  465. * @param {string|Function|Array.<number>|Object} positionExpr
  466. * @param {number} x Mouse x
  467. * @param {number} y Mouse y
  468. * @param {boolean} confine Whether confine tooltip content in view rect.
  469. * @param {Object|<Array.<Object>} params
  470. * @param {module:zrender/Element} el target element
  471. * @param {module:echarts/ExtensionAPI} api
  472. * @return {Array.<number>}
  473. */
  474. _updatePosition: function (tooltipModel, positionExpr, x, y, content, params, el) {
  475. var viewWidth = this._api.getWidth();
  476. var viewHeight = this._api.getHeight();
  477. positionExpr = positionExpr || tooltipModel.get('position');
  478. var contentSize = content.getSize();
  479. var align = tooltipModel.get('align');
  480. var vAlign = tooltipModel.get('verticalAlign');
  481. var rect = el && el.getBoundingRect().clone();
  482. el && rect.applyTransform(el.transform);
  483. if (typeof positionExpr === 'function') {
  484. // Callback of position can be an array or a string specify the position
  485. positionExpr = positionExpr([x, y], params, content.el, rect, {
  486. viewSize: [viewWidth, viewHeight],
  487. contentSize: contentSize.slice()
  488. });
  489. }
  490. if (zrUtil.isArray(positionExpr)) {
  491. x = parsePercent(positionExpr[0], viewWidth);
  492. y = parsePercent(positionExpr[1], viewHeight);
  493. } else if (zrUtil.isObject(positionExpr)) {
  494. positionExpr.width = contentSize[0];
  495. positionExpr.height = contentSize[1];
  496. var layoutRect = layoutUtil.getLayoutRect(positionExpr, {
  497. width: viewWidth,
  498. height: viewHeight
  499. });
  500. x = layoutRect.x;
  501. y = layoutRect.y;
  502. align = null; // When positionExpr is left/top/right/bottom,
  503. // align and verticalAlign will not work.
  504. vAlign = null;
  505. } // Specify tooltip position by string 'top' 'bottom' 'left' 'right' around graphic element
  506. else if (typeof positionExpr === 'string' && el) {
  507. var pos = calcTooltipPosition(positionExpr, rect, contentSize);
  508. x = pos[0];
  509. y = pos[1];
  510. } else {
  511. var pos = refixTooltipPosition(x, y, content, viewWidth, viewHeight, align ? null : 20, vAlign ? null : 20);
  512. x = pos[0];
  513. y = pos[1];
  514. }
  515. align && (x -= isCenterAlign(align) ? contentSize[0] / 2 : align === 'right' ? contentSize[0] : 0);
  516. vAlign && (y -= isCenterAlign(vAlign) ? contentSize[1] / 2 : vAlign === 'bottom' ? contentSize[1] : 0);
  517. if (tooltipModel.get('confine')) {
  518. var pos = confineTooltipPosition(x, y, content, viewWidth, viewHeight);
  519. x = pos[0];
  520. y = pos[1];
  521. }
  522. content.moveTo(x, y);
  523. },
  524. // FIXME
  525. // Should we remove this but leave this to user?
  526. _updateContentNotChangedOnAxis: function (dataByCoordSys) {
  527. var lastCoordSys = this._lastDataByCoordSys;
  528. var contentNotChanged = !!lastCoordSys && lastCoordSys.length === dataByCoordSys.length;
  529. contentNotChanged && each(lastCoordSys, function (lastItemCoordSys, indexCoordSys) {
  530. var lastDataByAxis = lastItemCoordSys.dataByAxis || {};
  531. var thisItemCoordSys = dataByCoordSys[indexCoordSys] || {};
  532. var thisDataByAxis = thisItemCoordSys.dataByAxis || [];
  533. contentNotChanged &= lastDataByAxis.length === thisDataByAxis.length;
  534. contentNotChanged && each(lastDataByAxis, function (lastItem, indexAxis) {
  535. var thisItem = thisDataByAxis[indexAxis] || {};
  536. var lastIndices = lastItem.seriesDataIndices || [];
  537. var newIndices = thisItem.seriesDataIndices || [];
  538. contentNotChanged &= lastItem.value === thisItem.value && lastItem.axisType === thisItem.axisType && lastItem.axisId === thisItem.axisId && lastIndices.length === newIndices.length;
  539. contentNotChanged && each(lastIndices, function (lastIdxItem, j) {
  540. var newIdxItem = newIndices[j];
  541. contentNotChanged &= lastIdxItem.seriesIndex === newIdxItem.seriesIndex && lastIdxItem.dataIndex === newIdxItem.dataIndex;
  542. });
  543. });
  544. });
  545. this._lastDataByCoordSys = dataByCoordSys;
  546. return !!contentNotChanged;
  547. },
  548. _hide: function (dispatchAction) {
  549. // Do not directly hideLater here, because this behavior may be prevented
  550. // in dispatchAction when showTip is dispatched.
  551. // FIXME
  552. // duplicated hideTip if manuallyHideTip is called from dispatchAction.
  553. this._lastDataByCoordSys = null;
  554. dispatchAction({
  555. type: 'hideTip',
  556. from: this.uid
  557. });
  558. },
  559. dispose: function (ecModel, api) {
  560. if (env.node) {
  561. return;
  562. }
  563. this._tooltipContent.dispose();
  564. globalListener.unregister('itemTooltip', api);
  565. }
  566. });
  567. /**
  568. * @param {Array.<Object|module:echarts/model/Model>} modelCascade
  569. * From top to bottom. (the last one should be globalTooltipModel);
  570. */
  571. function buildTooltipModel(modelCascade) {
  572. var resultModel = modelCascade.pop();
  573. while (modelCascade.length) {
  574. var tooltipOpt = modelCascade.pop();
  575. if (tooltipOpt) {
  576. if (Model.isInstance(tooltipOpt)) {
  577. tooltipOpt = tooltipOpt.get('tooltip', true);
  578. } // In each data item tooltip can be simply write:
  579. // {
  580. // value: 10,
  581. // tooltip: 'Something you need to know'
  582. // }
  583. if (typeof tooltipOpt === 'string') {
  584. tooltipOpt = {
  585. formatter: tooltipOpt
  586. };
  587. }
  588. resultModel = new Model(tooltipOpt, resultModel, resultModel.ecModel);
  589. }
  590. }
  591. return resultModel;
  592. }
  593. function makeDispatchAction(payload, api) {
  594. return payload.dispatchAction || zrUtil.bind(api.dispatchAction, api);
  595. }
  596. function refixTooltipPosition(x, y, content, viewWidth, viewHeight, gapH, gapV) {
  597. var size = content.getOuterSize();
  598. var width = size.width;
  599. var height = size.height;
  600. if (gapH != null) {
  601. if (x + width + gapH > viewWidth) {
  602. x -= width + gapH;
  603. } else {
  604. x += gapH;
  605. }
  606. }
  607. if (gapV != null) {
  608. if (y + height + gapV > viewHeight) {
  609. y -= height + gapV;
  610. } else {
  611. y += gapV;
  612. }
  613. }
  614. return [x, y];
  615. }
  616. function confineTooltipPosition(x, y, content, viewWidth, viewHeight) {
  617. var size = content.getOuterSize();
  618. var width = size.width;
  619. var height = size.height;
  620. x = Math.min(x + width, viewWidth) - width;
  621. y = Math.min(y + height, viewHeight) - height;
  622. x = Math.max(x, 0);
  623. y = Math.max(y, 0);
  624. return [x, y];
  625. }
  626. function calcTooltipPosition(position, rect, contentSize) {
  627. var domWidth = contentSize[0];
  628. var domHeight = contentSize[1];
  629. var gap = 5;
  630. var x = 0;
  631. var y = 0;
  632. var rectWidth = rect.width;
  633. var rectHeight = rect.height;
  634. switch (position) {
  635. case 'inside':
  636. x = rect.x + rectWidth / 2 - domWidth / 2;
  637. y = rect.y + rectHeight / 2 - domHeight / 2;
  638. break;
  639. case 'top':
  640. x = rect.x + rectWidth / 2 - domWidth / 2;
  641. y = rect.y - domHeight - gap;
  642. break;
  643. case 'bottom':
  644. x = rect.x + rectWidth / 2 - domWidth / 2;
  645. y = rect.y + rectHeight + gap;
  646. break;
  647. case 'left':
  648. x = rect.x - domWidth - gap;
  649. y = rect.y + rectHeight / 2 - domHeight / 2;
  650. break;
  651. case 'right':
  652. x = rect.x + rectWidth + gap;
  653. y = rect.y + rectHeight / 2 - domHeight / 2;
  654. }
  655. return [x, y];
  656. }
  657. function isCenterAlign(align) {
  658. return align === 'center' || align === 'middle';
  659. }
  660. module.exports = _default;