TooltipView.js 24.1 KB
Newer Older
L
lang 已提交
1 2 3 4 5 6
define(function (require) {

    var TooltipContent = require('./TooltipContent');
    var zrUtil = require('zrender/core/util');
    var formatUtil = require('../../util/format');
    var numberUtil = require('../../util/number');
7
    var modelUtil = require('../../util/model');
P
pah100 已提交
8
    var findPointFromSeries = require('../axisPointer/findPointFromSeries');
L
lang 已提交
9
    var parsePercent = numberUtil.parsePercent;
L
lang 已提交
10
    var env = require('zrender/core/env');
L
lang 已提交
11
    var Model = require('../../model/Model');
P
pah100 已提交
12
    var globalListener = require('../axisPointer/globalListener');
L
lang 已提交
13

P
pah100 已提交
14 15
    var bind = zrUtil.bind;
    var each = zrUtil.each;
L
lang 已提交
16 17 18 19 20 21 22


    require('../../echarts').extendComponentView({

        type: 'tooltip',

        init: function (ecModel, api) {
23
            if (env.node) {
L
lang 已提交
24 25
                return;
            }
L
lang 已提交
26 27 28 29 30
            var tooltipContent = new TooltipContent(api.getDom(), api);
            this._tooltipContent = tooltipContent;
        },

        render: function (tooltipModel, ecModel, api) {
31
            if (env.node) {
L
lang 已提交
32 33
                return;
            }
34

L
lang 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
            // Reset
            this.group.removeAll();

            /**
             * @private
             * @type {module:echarts/component/tooltip/TooltipModel}
             */
            this._tooltipModel = tooltipModel;

            /**
             * @private
             * @type {module:echarts/model/Global}
             */
            this._ecModel = ecModel;

            /**
             * @private
             * @type {module:echarts/ExtensionAPI}
             */
            this._api = api;

            /**
             * @private
P
pah100 已提交
58
             * @type {Array.<Array.<Object>>}
L
lang 已提交
59
             */
P
pah100 已提交
60
            this._lastDataByCoordSys;
P
pah100 已提交
61 62 63 64 65 66

            /**
             * @private
             * @type {boolean}
             */
            this._alwaysShowContent = tooltipModel.get('alwaysShowContent');
L
lang 已提交
67 68 69

            var tooltipContent = this._tooltipContent;
            tooltipContent.update();
P
pah100 已提交
70
            tooltipContent.setEnterable(tooltipModel.get('enterable'));
L
lang 已提交
71

P
pah100 已提交
72
            this._initGlobalListener();
L
lang 已提交
73

P
pah100 已提交
74 75
            this._keepShow();
        },
L
lang 已提交
76

P
pah100 已提交
77 78
        _initGlobalListener: function () {
            var tooltipModel = this._tooltipModel;
79
            var triggerOns = modelUtil.normalizeToArray(tooltipModel.get('triggerOn'));
1
100pah 已提交
80

P
pah100 已提交
81 82 83 84
            globalListener.register(
                'itemTooltip',
                this._api,
                bind(function (currTrigger, e, dispatchAction) {
85
                    if (zrUtil.indexOf(triggerOns, currTrigger) >= 0) {
P
pah100 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99
                        this._tryShow(e, dispatchAction);
                    }
                    else if (currTrigger === 'leave') {
                        this._hide(dispatchAction);
                    }
                }, this)
            );
        },

        _keepShow: function () {
            var tooltipModel = this._tooltipModel;
            var ecModel = this._ecModel;
            var api = this._api;

100
            // Try to keep the tooltip show when refreshing
1
100pah 已提交
101 102 103
            if (this._lastX != null
                && this._lastY != null
                // When user is willing to control tooltip totally using API,
1
100pah 已提交
104
                // self.manuallyShowTip({x, y}) might cause tooltip hide,
1
100pah 已提交
105
                // which is not expected.
P
pah100 已提交
106
                && tooltipModel.get('triggerOn') !== 'none'
1
100pah 已提交
107
            ) {
L
tweak  
lang 已提交
108 109 110 111 112 113
                var self = this;
                clearTimeout(this._refreshUpdateTimeout);
                this._refreshUpdateTimeout = setTimeout(function () {
                    // Show tip next tick after other charts are rendered
                    // In case highlight action has wrong result
                    // FIXME
1
100pah 已提交
114
                    self.manuallyShowTip(tooltipModel, ecModel, api, {
L
tweak  
lang 已提交
115 116 117
                        x: self._lastX,
                        y: self._lastY
                    });
118 119
                });
            }
L
lang 已提交
120 121 122 123
        },

        /**
         * Show tip manually by
124 125 126 127 128
         * dispatchAction({
         *     type: 'showTip',
         *     x: 10,
         *     y: 10
         * });
L
lang 已提交
129
         * Or
130
         * dispatchAction({
L
lang 已提交
131 132
         *      type: 'showTip',
         *      seriesIndex: 0,
133 134
         *      dataIndex or dataIndexInside or name
         * });
135 136
         *
         *  TODO Batch
L
lang 已提交
137
         */
1
100pah 已提交
138
        manuallyShowTip: function (tooltipModel, ecModel, api, payload) {
P
pah100 已提交
139
            if (payload.from === this.uid || env.node) {
P
pah100 已提交
140 141 142 143 144 145 146 147
                return;
            }

            var dispatchAction = makeDispatchAction(payload, api);

            // Reset ticket
            this._ticket = '';

1
100pah 已提交
148
            var seriesIndex = payload.seriesIndex;
P
pah100 已提交
149 150 151 152
            var dataByCoordSys = payload.dataByCoordSys;

            // When triggered from axisPointer.
            if (dataByCoordSys) {
P
pah100 已提交
153 154 155 156 157
                this._tryShow({
                    offsetX: payload.x,
                    offsetY: payload.y,
                    position: payload.position,
                    event: {},
P
pah100 已提交
158
                    dataByCoordSys: payload.dataByCoordSys,
P
pah100 已提交
159 160 161
                    tooltipOption: payload.tooltipOption
                }, dispatchAction);
            }
P
pah100 已提交
162 163 164 165
            else if (seriesIndex != null) {
                var pointInfo = findPointFromSeries(payload, ecModel);
                var cx = pointInfo.point[0];
                var cy = pointInfo.point[1];
P
pah100 已提交
166 167 168 169 170
                if (cx != null && cy != null) {
                    this._tryShow({
                        offsetX: cx,
                        offsetY: cy,
                        position: payload.position,
P
pah100 已提交
171
                        target: pointInfo.el,
P
pah100 已提交
172 173
                        event: {}
                    }, dispatchAction);
L
lang 已提交
174 175
                }
            }
P
pah100 已提交
176 177 178 179 180 181 182 183 184
            else if (payload.x != null && payload.y != null) {
                this._tryShow({
                    offsetX: payload.x,
                    offsetY: payload.y,
                    position: payload.position,
                    target: api.getZr().handler.findHover(payload.x, payload.y),
                    event: {}
                }, dispatchAction);
            }
L
lang 已提交
185 186
        },

1
100pah 已提交
187
        manuallyHideTip: function (tooltipModel, ecModel, api, payload) {
P
pah100 已提交
188
            var tooltipContent = this._tooltipContent;
189

P
pah100 已提交
190 191 192
            if (!this._alwaysShowContent) {
                tooltipContent.hideLater(this._tooltipModel.get('hideDelay'));
            }
L
lang 已提交
193

P
pah100 已提交
194
            this._lastX = this._lastY = null;
L
lang 已提交
195

P
pah100 已提交
196 197 198
            if (payload.from !== this.uid) {
                this._hide(makeDispatchAction(payload, api));
            }
L
lang 已提交
199 200
        },

P
pah100 已提交
201
        _tryShow: function (e, dispatchAction) {
L
lang 已提交
202 203 204 205 206 207 208
            var el = e.target;
            var tooltipModel = this._tooltipModel;

            if (!tooltipModel) {
                return;
            }

209 210 211 212
            // Save mouse x, mouse y. So we can try to keep showing the tip if chart is refreshed
            this._lastX = e.offsetX;
            this._lastY = e.offsetY;

P
pah100 已提交
213 214 215
            var dataByCoordSys = e.dataByCoordSys;
            if (dataByCoordSys && dataByCoordSys.length) {
                this._showAxisTooltip(dataByCoordSys, e);
P
pah100 已提交
216
            }
L
lang 已提交
217
            // Always show item tooltip if mouse is on the element with dataIndex
P
pah100 已提交
218
            else if (el && el.dataIndex != null) {
P
pah100 已提交
219
                this._lastDataByCoordSys = null;
P
pah100 已提交
220
                this._showSeriesItemTooltip(e, el, dispatchAction);
L
lang 已提交
221
            }
P
pah100 已提交
222
            // Tooltip provided directly. Like legend.
L
lang 已提交
223
            else if (el && el.tooltip) {
P
pah100 已提交
224
                this._lastDataByCoordSys = null;
P
pah100 已提交
225
                this._showComponentItemTooltip(e, el, dispatchAction);
L
lang 已提交
226
            }
L
lang 已提交
227
            else {
P
pah100 已提交
228
                this._lastDataByCoordSys = null;
P
pah100 已提交
229
                this._hide(dispatchAction);
L
lang 已提交
230 231 232
            }
        },

233 234 235 236 237 238 239 240 241 242 243 244 245
        _showOrMove: function (tooltipModel, cb) {
            // showDelay is used in this case: tooltip.enterable is set
            // as true. User intent to move mouse into tooltip and click
            // something. `showDelay` makes it easyer to enter the content
            // but tooltip do not move immediately.
            var delay = tooltipModel.get('showDelay');
            cb = zrUtil.bind(cb, this);
            clearTimeout(this._showTimout);
            delay > 0
                ? (this._showTimout = setTimeout(cb, delay))
                : cb();
        },

P
pah100 已提交
246
        _showAxisTooltip: function (dataByCoordSys, e) {
P
pah100 已提交
247
            var ecModel = this._ecModel;
P
pah100 已提交
248
            var globalTooltipModel = this._tooltipModel;
P
pah100 已提交
249
            var point = [e.offsetX, e.offsetY];
P
pah100 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
            var singleDefaultHTML = [];
            var singleParamsList = [];
            var singleTooltipModel = buildTooltipModel([
                e.tooltipOption,
                globalTooltipModel
            ]);

            each(dataByCoordSys, function (itemCoordSys) {
                // var coordParamList = [];
                // var coordDefaultHTML = [];
                // var coordTooltipModel = buildTooltipModel([
                //     e.tooltipOption,
                //     itemCoordSys.tooltipOption,
                //     ecModel.getComponent(itemCoordSys.coordSysMainType, itemCoordSys.coordSysIndex),
                //     globalTooltipModel
                // ]);
                // var displayMode = coordTooltipModel.get('displayMode');
                // var paramsList = displayMode === 'single' ? singleParamsList : [];

                each(itemCoordSys.dataByAxis, function (item) {
                    var axisModel = ecModel.getComponent(item.axisDim + 'Axis', item.axisIndex);
                    var axisValue = item.value;
                    var seriesDefaultHTML = [];

                    if (!axisModel || axisValue == null) {
                        return;
                    }
L
lang 已提交
277

P
pah100 已提交
278 279 280 281 282 283 284 285 286
                    zrUtil.each(item.seriesDataIndices, function (idxItem) {
                        var series = ecModel.getSeriesByIndex(idxItem.seriesIndex);
                        var dataIndex = idxItem.dataIndexInside;
                        var dataParams = series && series.getDataParams(dataIndex);
                        if (dataParams) {
                            singleParamsList.push(dataParams);
                            seriesDefaultHTML.push(series.formatTooltip(dataIndex, true));
                        }
                    });
1
100pah 已提交
287

P
pah100 已提交
288 289 290 291 292 293 294 295 296
                    // Default tooltip content
                    // FIXME
                    // (1) shold be the first data which has name?
                    // (2) themeRiver, firstDataIndex is array, and first line is unnecessary.
                    var firstLine = item.valueLabel;
                    singleDefaultHTML.push(
                        (firstLine ? formatUtil.encodeHTML(firstLine) + '<br />' : '')
                        + seriesDefaultHTML.join('<br />')
                    );
L
lang 已提交
297
                });
P
pah100 已提交
298
            }, this);
D
deqingli 已提交
299

P
pah100 已提交
300
            // In most case, the second axis is shown upper than the first one.
P
pah100 已提交
301 302
            singleDefaultHTML.reverse();
            singleDefaultHTML = singleDefaultHTML.join('<br /><br />');
P
pah100 已提交
303 304

            var positionExpr = e.position;
P
pah100 已提交
305 306
            this._showOrMove(singleTooltipModel, function () {
                if (this._updateContentNotChangedOnAxis(dataByCoordSys)) {
307
                    this._updatePosition(
P
pah100 已提交
308
                        singleTooltipModel,
309 310 311
                        positionExpr,
                        point[0], point[1],
                        this._tooltipContent,
P
pah100 已提交
312
                        singleParamsList
313 314 315 316
                    );
                }
                else {
                    this._showTooltipContent(
P
pah100 已提交
317
                        singleTooltipModel, singleDefaultHTML, singleParamsList, Math.random(),
318 319 320 321
                        point[0], point[1], positionExpr
                    );
                }
            });
P
pah100 已提交
322 323 324

            // Do not trigger events here, because this branch only be entered
            // from dispatchAction.
L
lang 已提交
325 326
        },

P
pah100 已提交
327 328 329 330 331 332 333
        _showSeriesItemTooltip: function (e, el, dispatchAction) {
            var ecModel = this._ecModel;
            // Use dataModel in element if possible
            // Used when mouseover on a element like markPoint or edge
            // In which case, the data is not main data in series.
            var seriesIndex = el.seriesIndex;
            var seriesModel = ecModel.getSeriesByIndex(seriesIndex);
L
lang 已提交
334

P
pah100 已提交
335 336 337 338 339
            // For example, graph link.
            var dataModel = el.dataModel || seriesModel;
            var dataIndex = el.dataIndex;
            var dataType = el.dataType;
            var data = dataModel.getData();
L
lang 已提交
340

P
pah100 已提交
341 342 343 344 345 346
            var tooltipModel = buildTooltipModel([
                data.getItemModel(dataIndex),
                dataModel,
                seriesModel && (seriesModel.coordinateSystem || {}).model,
                this._tooltipModel
            ]);
L
lang 已提交
347

P
pah100 已提交
348 349
            var tooltipTrigger = tooltipModel.get('trigger');
            if (tooltipTrigger != null && tooltipTrigger !== 'item') {
350 351 352
                return;
            }

P
pah100 已提交
353 354 355
            var params = dataModel.getDataParams(dataIndex, dataType);
            var defaultHtml = dataModel.formatTooltip(dataIndex, false, dataType);
            var asyncTicket = 'item_' + dataModel.name + '_' + dataIndex;
L
lang 已提交
356

357 358 359 360 361 362
            this._showOrMove(tooltipModel, function () {
                this._showTooltipContent(
                    tooltipModel, defaultHtml, params, asyncTicket,
                    e.offsetX, e.offsetY, e.position, e.target
                );
            });
P
pah100 已提交
363 364 365 366

            // FIXME
            // duplicated showtip if manuallyShowTip is called from dispatchAction.
            dispatchAction({
L
tweak  
lang 已提交
367
                type: 'showTip',
368
                dataIndexInside: dataIndex,
P
pah100 已提交
369 370
                dataIndex: data.getRawIndex(dataIndex),
                seriesIndex: seriesIndex,
L
tweak  
lang 已提交
371 372
                from: this.uid
            });
L
lang 已提交
373 374
        },

P
tweak  
pah100 已提交
375
        _showComponentItemTooltip: function (e, el, dispatchAction) {
P
pah100 已提交
376
            var tooltipOpt = el.tooltip;
377
            if (typeof tooltipOpt === 'string') {
P
pah100 已提交
378
                var content = tooltipOpt;
379
                tooltipOpt = {
P
pah100 已提交
380 381 382
                    content: content,
                    // Fixed formatter
                    formatter: content
383
                };
L
lang 已提交
384
            }
P
pah100 已提交
385 386 387
            var subTooltipModel = new Model(tooltipOpt, this._tooltipModel, this._ecModel);
            var defaultHtml = subTooltipModel.get('content');
            var asyncTicket = Math.random();
L
lang 已提交
388

P
pah100 已提交
389 390 391
            // Do not check whether `trigger` is 'none' here, because `trigger`
            // only works on cooridinate system. In fact, we have not found case
            // that requires setting `trigger` nothing on component yet.
L
lang 已提交
392

393 394 395 396 397 398
            this._showOrMove(subTooltipModel, function () {
                this._showTooltipContent(
                    subTooltipModel, defaultHtml, subTooltipModel.get('formatterParams') || {},
                    asyncTicket, e.offsetX, e.offsetY, e.position, el
                );
            });
P
tweak  
pah100 已提交
399 400 401 402 403 404

            // If not dispatch showTip, tip may be hide triggered by axis.
            dispatchAction({
                type: 'showTip',
                from: this.uid
            });
L
lang 已提交
405 406 407
        },

        _showTooltipContent: function (
P
pah100 已提交
408
            tooltipModel, defaultHtml, params, asyncTicket, x, y, positionExpr, el
L
lang 已提交
409 410 411 412
        ) {
            // Reset ticket
            this._ticket = '';

P
pah100 已提交
413 414 415
            if (!tooltipModel.get('showContent') || !tooltipModel.get('show')) {
                return;
            }
L
lang 已提交
416

P
pah100 已提交
417
            var tooltipContent = this._tooltipContent;
L
lang 已提交
418

P
pah100 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432
            var formatter = tooltipModel.get('formatter');
            positionExpr = positionExpr || tooltipModel.get('position');
            var html = defaultHtml;

            if (formatter && typeof formatter === 'string') {
                html = formatUtil.formatTpl(formatter, params, true);
            }
            else if (typeof formatter === 'function') {
                var callback = bind(function (cbTicket, html) {
                    if (cbTicket === this._ticket) {
                        tooltipContent.setContent(html);
                        this._updatePosition(
                            tooltipModel, positionExpr, x, y, tooltipContent, params, el
                        );
L
lang 已提交
433
                    }
P
pah100 已提交
434 435 436 437
                }, this);
                this._ticket = asyncTicket;
                html = formatter(params, asyncTicket, callback);
            }
L
lang 已提交
438

P
pah100 已提交
439
            tooltipContent.setContent(html);
440
            tooltipContent.show(tooltipModel);
L
lang 已提交
441

P
pah100 已提交
442 443 444
            this._updatePosition(
                tooltipModel, positionExpr, x, y, tooltipContent, params, el
            );
L
lang 已提交
445 446 447
        },

        /**
P
pah100 已提交
448 449 450 451 452 453 454 455
         * @param  {string|Function|Array.<number>} positionExpr
         * @param  {number} x Mouse x
         * @param  {number} y Mouse y
         * @param  {boolean} confine Whether confine tooltip content in view rect.
         * @param  {Object|<Array.<Object>} params
         * @param  {module:zrender/Element} el target element
         * @param  {module:echarts/ExtensionAPI} api
         * @return {Array.<number>}
L
lang 已提交
456
         */
P
pah100 已提交
457 458 459
        _updatePosition: function (tooltipModel, positionExpr, x, y, content, params, el) {
            var viewWidth = this._api.getWidth();
            var viewHeight = this._api.getHeight();
460
            positionExpr = positionExpr || tooltipModel.get('position');
P
pah100 已提交
461 462 463 464 465 466

            var rect = el && el.getBoundingRect().clone();
            el && rect.applyTransform(el.transform);
            if (typeof positionExpr === 'function') {
                // Callback of position can be an array or a string specify the position
                positionExpr = positionExpr([x, y], params, content.el, rect);
L
lang 已提交
467 468
            }

P
pah100 已提交
469 470 471 472 473 474 475
            var contentSize = content.getSize();
            var align = tooltipModel.get('align');
            var vAlign = tooltipModel.get('verticalAlign');

            if (zrUtil.isArray(positionExpr)) {
                x = parsePercent(positionExpr[0], viewWidth);
                y = parsePercent(positionExpr[1], viewHeight);
L
lang 已提交
476
            }
P
pah100 已提交
477 478 479 480 481 482 483
            // Specify tooltip position by string 'top' 'bottom' 'left' 'right' around graphic element
            else if (typeof positionExpr === 'string' && el) {
                var pos = calcTooltipPosition(
                    positionExpr, rect, contentSize
                );
                x = pos[0];
                y = pos[1];
L
lang 已提交
484 485
            }
            else {
P
pah100 已提交
486 487 488 489 490
                var pos = refixTooltipPosition(
                    x, y, content.el, viewWidth, viewHeight, align ? 0 : 20, vAlign ? 0 : 20
                );
                x = pos[0];
                y = pos[1];
L
lang 已提交
491 492
            }

P
pah100 已提交
493 494
            align && (x -= align === 'center' ? contentSize[0] / 2 : align === 'right' ? contentSize[0] : 0);
            vAlign && (y -= vAlign === 'middle' ? contentSize[1] / 2 : vAlign === 'bottom' ? contentSize[1] : 0);
L
lang 已提交
495

P
pah100 已提交
496 497 498 499 500 501
            if (tooltipModel.get('confine')) {
                var pos = confineTooltipPosition(
                    x, y, content.el, viewWidth, viewHeight
                );
                x = pos[0];
                y = pos[1];
L
lang 已提交
502
            }
503

P
pah100 已提交
504 505 506 507 508
            content.moveTo(x, y);
        },

        // FIXME
        // Should we remove this but leave this to user?
P
pah100 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
        _updateContentNotChangedOnAxis: function (dataByCoordSys) {
            var lastCoordSys = this._lastDataByCoordSys;
            var contentNotChanged = !!lastCoordSys
                && lastCoordSys.length === dataByCoordSys.length;

            each(lastCoordSys, function (lastItemCoordSys, indexCoordSys) {
                var lastDataByAxis = lastItemCoordSys.dataByAxis || {};
                var thisItemCoordSys = dataByCoordSys[indexCoordSys] || {};
                var thisDataByAxis = thisItemCoordSys.dataByAxis || [];
                contentNotChanged &= lastDataByAxis.length === thisDataByAxis.length;

                each(lastDataByAxis, function (lastItem, indexAxis) {
                    var thisItem = thisDataByAxis[indexAxis] || {};
                    var lastIndices = lastItem.seriesDataIndices || [];
                    var newIndices = thisItem.seriesDataIndices || [];

P
pah100 已提交
525
                    contentNotChanged &=
P
pah100 已提交
526 527 528 529 530 531 532 533 534 535 536
                        lastItem.value === thisItem.value
                        && lastItem.axisType === thisItem.axisType
                        && lastItem.axisId === thisItem.axisId
                        && lastIndices.length === newIndices.length;

                    each(lastIndices, function (lastIdxItem, j) {
                        var newIdxItem = newIndices[j];
                        contentNotChanged &=
                            lastIdxItem.seriesIndex === newIdxItem.seriesIndex
                            && lastIdxItem.dataIndex === newIdxItem.dataIndex;
                    });
P
pah100 已提交
537 538 539
                });
            });

P
pah100 已提交
540
            this._lastDataByCoordSys = dataByCoordSys;
P
pah100 已提交
541 542 543 544 545 546 547 548 549 550

            return !!contentNotChanged;
        },

        _hide: function (dispatchAction) {
            // Do not directly hideLater here, because this behavior may be prevented
            // in dispatchAction when showTip is dispatched.

            // FIXME
            // duplicated hideTip if manuallyHideTip is called from dispatchAction.
P
pah100 已提交
551
            this._lastDataByCoordSys = null;
P
pah100 已提交
552
            dispatchAction({
553 554 555
                type: 'hideTip',
                from: this.uid
            });
L
lang 已提交
556 557
        },

L
lang 已提交
558
        dispose: function (ecModel, api) {
L
tweak  
lang 已提交
559 560 561
            if (env.node) {
                return;
            }
562
            this._tooltipContent.hide();
P
pah100 已提交
563
            globalListener.disopse(api.getZr(), 'itemTooltip');
L
lang 已提交
564 565
        }
    });
P
pah100 已提交
566 567


P
pah100 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    /**
     * @param {Array.<Object|module:echarts/model/Model>} modelCascade
     * From top to bottom. (the last one should be globalTooltipModel);
     */
    function buildTooltipModel(modelCascade) {
        var resultModel = modelCascade.pop();
        while (modelCascade.length) {
            var tooltipOpt = modelCascade.pop();
            if (tooltipOpt) {
                tooltipOpt = (tooltipOpt instanceof Model) && tooltipOpt.get('tooltip', true);
                // In each data item tooltip can be simply write:
                // {
                //  value: 10,
                //  tooltip: 'Something you need to know'
                // }
                if (typeof tooltipOpt === 'string') {
                    tooltipOpt = {formatter: tooltipOpt};
                }
                resultModel = new Model(tooltipOpt, resultModel, resultModel.ecModel);
            }
P
pah100 已提交
588
        }
P
pah100 已提交
589
        return resultModel;
P
pah100 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
    }

    function makeDispatchAction(payload, api) {
        return payload.dispatchAction || zrUtil.bind(api.dispatchAction, api);
    }

    function refixTooltipPosition(x, y, el, viewWidth, viewHeight, gapH, gapV) {
        var width = el.clientWidth;
        var height = el.clientHeight;

        if (x + width + gapH > viewWidth) {
            x -= width + gapH;
        }
        else {
            x += gapH;
        }
        if (y + height + gapV > viewHeight) {
            y -= height + gapV;
        }
        else {
            y += gapV;
        }
        return [x, y];
    }

    function confineTooltipPosition(x, y, el, viewWidth, viewHeight) {
        var width = el.clientWidth;
        var height = el.clientHeight;

        x = Math.min(x + width, viewWidth) - width;
        y = Math.min(y + height, viewHeight) - height;
        x = Math.max(x, 0);
        y = Math.max(y, 0);

        return [x, y];
    }

    function calcTooltipPosition(position, rect, contentSize) {
        var domWidth = contentSize[0];
        var domHeight = contentSize[1];
        var gap = 5;
        var x = 0;
        var y = 0;
        var rectWidth = rect.width;
        var rectHeight = rect.height;
        switch (position) {
            case 'inside':
                x = rect.x + rectWidth / 2 - domWidth / 2;
                y = rect.y + rectHeight / 2 - domHeight / 2;
                break;
            case 'top':
                x = rect.x + rectWidth / 2 - domWidth / 2;
                y = rect.y - domHeight - gap;
                break;
            case 'bottom':
                x = rect.x + rectWidth / 2 - domWidth / 2;
                y = rect.y + rectHeight + gap;
                break;
            case 'left':
                x = rect.x - domWidth - gap;
                y = rect.y + rectHeight / 2 - domHeight / 2;
                break;
            case 'right':
                x = rect.x + rectWidth + gap;
                y = rect.y + rectHeight / 2 - domHeight / 2;
        }
        return [x, y];
    }

L
lang 已提交
659
});