TooltipRichContent.js 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

20
import * as zrUtil from 'zrender/src/core/util';
21
// import Group from 'zrender/src/container/Group';
22
import Text from 'zrender/src/graphic/Text';
23
import * as graphicUtil from '../../util/graphic';
24

L
liulinboyi 已提交
25 26 27 28 29 30 31 32

function makeStyleCoord(out, zr, zrX, zrY) {
    out[0] = zrX;
    out[1] = zrY;
    out[2] = out[0] / zr.getWidth(); // The ratio of left to width
    out[3] = out[1] / zr.getHeight(); // The ratio of top to height
}

33 34 35 36 37 38
/**
 * @alias module:echarts/component/tooltip/TooltipRichContent
 * @constructor
 */
function TooltipRichContent(api) {

L
liulinboyi 已提交
39 40 41 42 43
    var zr = this._zr = api.getZr();

    this._styleCoord = [0, 0, 0, 0]; // [left, top, left/width, top/height]

    makeStyleCoord(this._styleCoord, zr, api.getWidth() / 2, api.getHeight() / 2);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    this._show = false;

    /**
     * @private
     */
    this._hideTimeout;
}

TooltipRichContent.prototype = {

    constructor: TooltipRichContent,

    /**
     * @private
     * @type {boolean}
     */
    _enterable: true,

    /**
     * Update when tooltip is rendered
     */
L
liulinboyi 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    update: function (tooltipModel) {
        var alwaysShowContent = tooltipModel.get('alwaysShowContent');
        alwaysShowContent && this._moveTooltipIfResized();
    },

    /**
     * when `alwaysShowContent` is true,
     * we should move the tooltip after chart resized
     */
    _moveTooltipIfResized: function () {
        var ratioX = this._styleCoord[2]; // The ratio of left to width
        var ratioY = this._styleCoord[3]; // The ratio of top to height
        var realX = ratioX * this._zr.getWidth();
        var realY = ratioY * this._zr.getHeight();
        this.moveTo(realX, realY);
81 82 83 84 85 86 87 88 89 90 91
    },

    show: function (tooltipModel) {
        if (this._hideTimeout) {
            clearTimeout(this._hideTimeout);
        }

        this.el.attr('show', true);
        this._show = true;
    },

92 93 94 95 96 97 98
    /**
     * Set tooltip content
     *
     * @param {string} content rich text string of content
     * @param {Object} markerRich rich text style
     * @param {Object} tooltipModel tooltip model
     */
99 100 101 102 103 104 105 106 107 108 109 110 111
    setContent: function (content, markerRich, tooltipModel) {
        if (this.el) {
            this._zr.remove(this.el);
        }

        var markers = {};
        var text = content;
        var prefix = '{marker';
        var suffix = '|}';
        var startId = text.indexOf(prefix);
        while (startId >= 0) {
            var endId = text.indexOf(suffix);
            var name = text.substr(startId + prefix.length, endId - startId - prefix.length);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
            if (name.indexOf('sub') > -1) {
                markers['marker' + name] = {
                    textWidth: 4,
                    textHeight: 4,
                    textBorderRadius: 2,
                    textBackgroundColor: markerRich[name],
                    // TODO: textOffset is not implemented for rich text
                    textOffset: [3, 0]
                };
            }
            else {
                markers['marker' + name] = {
                    textWidth: 10,
                    textHeight: 10,
                    textBorderRadius: 5,
                    textBackgroundColor: markerRich[name]
                };
            }
130 131 132 133 134

            text = text.substr(endId + 1);
            startId = text.indexOf('{marker');
        }

135 136 137 138 139 140 141
        var textStyleModel = tooltipModel.getModel('textStyle');
        var fontSize = textStyleModel.get('fontSize');
        var lineHeight = tooltipModel.get('textLineHeight');
        if (lineHeight == null) {
            lineHeight = Math.round(fontSize * 3 / 2);
        }

142
        this.el = new Text({
143
            style: graphicUtil.setTextStyle({}, textStyleModel, {
144 145 146 147 148
                rich: markers,
                text: content,
                textBackgroundColor: tooltipModel.get('backgroundColor'),
                textBorderRadius: tooltipModel.get('borderRadius'),
                textFill: tooltipModel.get('textStyle.color'),
149 150 151
                textPadding: tooltipModel.get('padding'),
                textLineHeight: lineHeight
            }),
152 153 154
            z: tooltipModel.get('z')
        });
        this._zr.add(this.el);
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

        var self = this;
        this.el.on('mouseover', function () {
            // clear the timeout in hideLater and keep showing tooltip
            if (self._enterable) {
                clearTimeout(self._hideTimeout);
                self._show = true;
            }
            self._inContent = true;
        });
        this.el.on('mouseout', function () {
            if (self._enterable) {
                if (self._show) {
                    self.hideLater(self._hideDelay);
                }
            }
            self._inContent = false;
        });
173 174 175 176 177 178 179 180 181 182 183 184 185
    },

    setEnterable: function (enterable) {
        this._enterable = enterable;
    },

    getSize: function () {
        var bounding = this.el.getBoundingRect();
        return [bounding.width, bounding.height];
    },

    moveTo: function (x, y) {
        if (this.el) {
L
liulinboyi 已提交
186 187 188
            var styleCoord = this._styleCoord;
            makeStyleCoord(styleCoord, this._zr, x, y);
            this.el.attr('position', [styleCoord[0], styleCoord[1]]);
189 190 191 192
        }
    },

    hide: function () {
193 194 195
        if (this.el) {
            this.el.hide();
        }
196
        this._show = false;
197 198 199
    },

    hideLater: function (time) {
200 201 202
        if (this._show && !(this._inContent && this._enterable)) {
            if (time) {
                this._hideDelay = time;
L
liulinboyi 已提交
203
                // Set show false to avoid invoke hideLater multiple times
204 205 206 207 208 209 210
                this._show = false;
                this._hideTimeout = setTimeout(zrUtil.bind(this.hide, this), time);
            }
            else {
                this.hide();
            }
        }
211 212 213 214 215 216
    },

    isShow: function () {
        return this._show;
    },

217 218 219 220 221 222 223 224
    dispose: function () {
        clearTimeout(this._hideTimeout);

        if (this.el) {
            this._zr.remove(this.el);
        }
    },

225
    getOuterSize: function () {
S
tweak  
sushuang 已提交
226
        var size = this.getSize();
227 228 229 230
        return {
            width: size[0],
            height: size[1]
        };
231 232 233 234
    }
};

export default TooltipRichContent;