PieView.js 12.1 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.
*/

S
sushuang 已提交
20
import * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
21
import * as graphic from '../../util/graphic';
S
sushuang 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import ChartView from '../../view/Chart';

/**
 * @param {module:echarts/model/Series} seriesModel
 * @param {boolean} hasAnimation
 * @inner
 */
function updateDataSelected(uid, seriesModel, hasAnimation, api) {
    var data = seriesModel.getData();
    var dataIndex = this.dataIndex;
    var name = data.getName(dataIndex);
    var selectedOffset = seriesModel.get('selectedOffset');

    api.dispatchAction({
        type: 'pieToggleSelect',
        from: uid,
        name: name,
        seriesId: seriesModel.id
    });
L
lang 已提交
41

S
sushuang 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    data.each(function (idx) {
        toggleItemSelected(
            data.getItemGraphicEl(idx),
            data.getItemLayout(idx),
            seriesModel.isSelected(data.getName(idx)),
            selectedOffset,
            hasAnimation
        );
    });
}

/**
 * @param {module:zrender/graphic/Sector} el
 * @param {Object} layout
 * @param {boolean} isSelected
 * @param {number} selectedOffset
 * @param {boolean} hasAnimation
 * @inner
 */
function toggleItemSelected(el, layout, isSelected, selectedOffset, hasAnimation) {
    var midAngle = (layout.startAngle + layout.endAngle) / 2;

    var dx = Math.cos(midAngle);
    var dy = Math.sin(midAngle);

    var offset = isSelected ? selectedOffset : 0;
    var position = [dx * offset, dy * offset];

    hasAnimation
        // animateTo will stop revious animation like update transition
        ? el.animate()
            .when(200, {
                position: position
            })
            .start('bounceOut')
        : el.attr('position', position);
}

/**
 * Piece of pie including Sector, Label, LabelLine
 * @constructor
 * @extends {module:zrender/graphic/Group}
 */
function PiePiece(data, idx) {

    graphic.Group.call(this);

    var sector = new graphic.Sector({
        z2: 2
    });
    var polyline = new graphic.Polyline();
    var text = new graphic.Text();
    this.add(sector);
    this.add(polyline);
    this.add(text);

    this.updateData(data, idx, true);
}
L
lang 已提交
100

S
sushuang 已提交
101
var piePieceProto = PiePiece.prototype;
L
lang 已提交
102

S
sushuang 已提交
103
piePieceProto.updateData = function (data, idx, firstCreate) {
L
lang 已提交
104

S
sushuang 已提交
105
    var sector = this.childAt(0);
S
Tweak  
sushuang 已提交
106 107
    var labelLine = this.childAt(1);
    var labelText = this.childAt(2);
L
lang 已提交
108

S
sushuang 已提交
109 110 111 112 113
    var seriesModel = data.hostModel;
    var itemModel = data.getItemModel(idx);
    var layout = data.getItemLayout(idx);
    var sectorShape = zrUtil.extend({}, layout);
    sectorShape.label = null;
L
lang 已提交
114

S
sushuang 已提交
115 116
    if (firstCreate) {
        sector.setShape(sectorShape);
L
lang 已提交
117

S
sushuang 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        var animationType = seriesModel.getShallow('animationType');
        if (animationType === 'scale') {
            sector.shape.r = layout.r0;
            graphic.initProps(sector, {
                shape: {
                    r: layout.r
                }
            }, seriesModel, idx);
        }
        // Expansion
        else {
            sector.shape.endAngle = layout.startAngle;
            graphic.updateProps(sector, {
                shape: {
                    endAngle: layout.endAngle
                }
            }, seriesModel, idx);
        }
L
lang 已提交
136

S
sushuang 已提交
137 138 139 140 141 142
    }
    else {
        graphic.updateProps(sector, {
            shape: sectorShape
        }, seriesModel, idx);
    }
143

S
sushuang 已提交
144 145
    // Update common style
    var visualColor = data.getItemVisual(idx, 'color');
146

S
sushuang 已提交
147 148 149 150 151 152
    sector.useStyle(
        zrUtil.defaults(
            {
                lineJoin: 'bevel',
                fill: visualColor
            },
S
sushuang 已提交
153
            itemModel.getModel('itemStyle').getItemStyle()
S
sushuang 已提交
154 155
        )
    );
S
sushuang 已提交
156
    sector.hoverStyle = itemModel.getModel('emphasis.itemStyle').getItemStyle();
S
sushuang 已提交
157 158 159 160 161 162 163 164

    var cursorStyle = itemModel.getShallow('cursor');
    cursorStyle && sector.attr('cursor', cursorStyle);

    // Toggle selected
    toggleItemSelected(
        this,
        data.getItemLayout(idx),
165
        seriesModel.isSelected(null, idx),
S
sushuang 已提交
166 167 168 169
        seriesModel.get('selectedOffset'),
        seriesModel.get('animation')
    );

170 171 172 173 174
    this._updateLabel(data, idx);

    this.highDownOnUpdate = (itemModel.get('hoverAnimation') && seriesModel.isAnimationEnabled())
        ? function (fromState, toState) {
            if (toState === 'emphasis') {
S
Tweak  
sushuang 已提交
175 176 177
                labelLine.ignore = labelLine.hoverIgnore;
                labelText.ignore = labelText.hoverIgnore;

178 179 180 181 182 183 184 185
                // Sector may has animation of updating data. Force to move to the last frame
                // Or it may stopped on the wrong shape
                sector.stopAnimation(true);
                sector.animateTo({
                    shape: {
                        r: layout.r + seriesModel.get('hoverOffset')
                    }
                }, 300, 'elasticOut');
186
            }
187
            else {
S
Tweak  
sushuang 已提交
188 189 190
                labelLine.ignore = labelLine.normalIgnore;
                labelText.ignore = labelText.normalIgnore;

191 192 193 194 195 196
                sector.stopAnimation(true);
                sector.animateTo({
                    shape: {
                        r: layout.r
                    }
                }, 300, 'elasticOut');
197
            }
198 199
        }
        : null;
L
lang 已提交
200

S
sushuang 已提交
201 202
    graphic.setHoverStyle(this);
};
L
lang 已提交
203

S
sushuang 已提交
204
piePieceProto._updateLabel = function (data, idx) {
205

S
sushuang 已提交
206 207
    var labelLine = this.childAt(1);
    var labelText = this.childAt(2);
L
lang 已提交
208

S
sushuang 已提交
209 210 211 212 213
    var seriesModel = data.hostModel;
    var itemModel = data.getItemModel(idx);
    var layout = data.getItemLayout(idx);
    var labelLayout = layout.label;
    var visualColor = data.getItemVisual(idx, 'color');
214 215 216 217 218 219

    if (!labelLayout) {
        labelText.ignore = labelText.normalIgnore = labelText.hoverIgnore =
        labelLine.ignore = labelLine.normalIgnore = labelLine.hoverIgnore = true;
        return;
    }
S
sushuang 已提交
220 221 222 223 224 225

    graphic.updateProps(labelLine, {
        shape: {
            points: labelLayout.linePoints || [
                [labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y]
            ]
L
lang 已提交
226
        }
S
sushuang 已提交
227 228 229 230 231 232
    }, seriesModel, idx);

    graphic.updateProps(labelText, {
        style: {
            x: labelLayout.x,
            y: labelLayout.y
L
lang 已提交
233
        }
S
sushuang 已提交
234 235 236 237 238 239 240
    }, seriesModel, idx);
    labelText.attr({
        rotation: labelLayout.rotation,
        origin: [labelLayout.x, labelLayout.y],
        z2: 10
    });

241 242 243 244
    var labelModel = itemModel.getModel('label');
    var labelHoverModel = itemModel.getModel('emphasis.label');
    var labelLineModel = itemModel.getModel('labelLine');
    var labelLineHoverModel = itemModel.getModel('emphasis.labelLine');
S
sushuang 已提交
245 246 247 248 249 250 251
    var visualColor = data.getItemVisual(idx, 'color');

    graphic.setLabelStyle(
        labelText.style, labelText.hoverStyle = {}, labelModel, labelHoverModel,
        {
            labelFetcher: data.hostModel,
            labelDataIndex: idx,
S
sushuang 已提交
252
            defaultText: data.getName(idx),
S
sushuang 已提交
253 254 255 256 257 258 259
            autoColor: visualColor,
            useInsideStyle: !!labelLayout.inside
        },
        {
            textAlign: labelLayout.textAlign,
            textVerticalAlign: labelLayout.verticalAlign,
            opacity: data.getItemVisual(idx, 'opacity')
L
lang 已提交
260
        }
S
sushuang 已提交
261
    );
L
lang 已提交
262

263 264
    labelText.ignore = labelText.normalIgnore = !labelModel.get('show');
    labelText.hoverIgnore = !labelHoverModel.get('show');
L
lang 已提交
265

266 267
    labelLine.ignore = labelLine.normalIgnore = !labelLineModel.get('show');
    labelLine.hoverIgnore = !labelLineHoverModel.get('show');
L
lang 已提交
268

S
sushuang 已提交
269 270 271 272 273 274
    // Default use item visual color
    labelLine.setStyle({
        stroke: visualColor,
        opacity: data.getItemVisual(idx, 'opacity')
    });
    labelLine.setStyle(labelLineModel.getModel('lineStyle').getLineStyle());
L
lang 已提交
275

S
sushuang 已提交
276
    labelLine.hoverStyle = labelLineHoverModel.getModel('lineStyle').getLineStyle();
L
lang 已提交
277

S
sushuang 已提交
278 279 280 281 282 283 284 285
    var smooth = labelLineModel.get('smooth');
    if (smooth && smooth === true) {
        smooth = 0.4;
    }
    labelLine.setShape({
        smooth: smooth
    });
};
L
lang 已提交
286

S
sushuang 已提交
287
zrUtil.inherits(PiePiece, graphic.Group);
L
lang 已提交
288 289


S
sushuang 已提交
290
// Pie view
S
sushuang 已提交
291
var PieView = ChartView.extend({
L
lang 已提交
292

S
sushuang 已提交
293
    type: 'pie',
L
lang 已提交
294

S
sushuang 已提交
295 296 297 298
    init: function () {
        var sectorGroup = new graphic.Group();
        this._sectorGroup = sectorGroup;
    },
L
lang 已提交
299

S
sushuang 已提交
300 301 302 303
    render: function (seriesModel, ecModel, api, payload) {
        if (payload && (payload.from === this.uid)) {
            return;
        }
L
lang 已提交
304

S
sushuang 已提交
305 306 307
        var data = seriesModel.getData();
        var oldData = this._data;
        var group = this.group;
L
lang 已提交
308

S
sushuang 已提交
309 310 311
        var hasAnimation = ecModel.get('animation');
        var isFirstRender = !oldData;
        var animationType = seriesModel.get('animationType');
L
lang 已提交
312

S
sushuang 已提交
313 314 315
        var onSectorClick = zrUtil.curry(
            updateDataSelected, this.uid, seriesModel, hasAnimation, api
        );
L
lang 已提交
316

S
sushuang 已提交
317 318 319 320 321 322 323 324 325 326
        var selectedMode = seriesModel.get('selectedMode');
        data.diff(oldData)
            .add(function (idx) {
                var piePiece = new PiePiece(data, idx);
                // Default expansion animation
                if (isFirstRender && animationType !== 'scale') {
                    piePiece.eachChild(function (child) {
                        child.stopAnimation(true);
                    });
                }
L
lang 已提交
327

S
sushuang 已提交
328
                selectedMode && piePiece.on('click', onSectorClick);
L
lang 已提交
329

S
sushuang 已提交
330
                data.setItemGraphicEl(idx, piePiece);
L
lang 已提交
331

S
sushuang 已提交
332 333 334 335
                group.add(piePiece);
            })
            .update(function (newIdx, oldIdx) {
                var piePiece = oldData.getItemGraphicEl(oldIdx);
L
lang 已提交
336

S
sushuang 已提交
337
                piePiece.updateData(data, newIdx);
L
lang 已提交
338

S
sushuang 已提交
339 340 341 342 343 344 345 346 347 348
                piePiece.off('click');
                selectedMode && piePiece.on('click', onSectorClick);
                group.add(piePiece);
                data.setItemGraphicEl(newIdx, piePiece);
            })
            .remove(function (idx) {
                var piePiece = oldData.getItemGraphicEl(idx);
                group.remove(piePiece);
            })
            .execute();
L
lang 已提交
349

S
sushuang 已提交
350 351 352 353 354 355 356
        if (
            hasAnimation && isFirstRender && data.count() > 0
            // Default expansion animation
            && animationType !== 'scale'
        ) {
            var shape = data.getItemLayout(0);
            var r = Math.max(api.getWidth(), api.getHeight()) / 2;
L
lang 已提交
357

S
sushuang 已提交
358 359 360 361 362
            var removeClipPath = zrUtil.bind(group.removeClipPath, group);
            group.setClipPath(this._createClipPath(
                shape.cx, shape.cy, r, shape.startAngle, shape.clockwise, removeClipPath, seriesModel
            ));
        }
363 364 365 366
        else {
            // clipPath is used in first-time animation, so remove it when otherwise. See: #8994
            group.removeClipPath();
        }
1
100pah 已提交
367

S
sushuang 已提交
368 369
        this._data = data;
    },
L
lang 已提交
370

S
sushuang 已提交
371
    dispose: function () {},
L
lang 已提交
372

S
sushuang 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386
    _createClipPath: function (
        cx, cy, r, startAngle, clockwise, cb, seriesModel
    ) {
        var clipPath = new graphic.Sector({
            shape: {
                cx: cx,
                cy: cy,
                r0: 0,
                r: r,
                startAngle: startAngle,
                endAngle: startAngle,
                clockwise: clockwise
            }
        });
1
100pah 已提交
387

S
sushuang 已提交
388 389 390
        graphic.initProps(clipPath, {
            shape: {
                endAngle: startAngle + (clockwise ? 1 : -1) * Math.PI * 2
1
100pah 已提交
391
            }
S
sushuang 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
        }, seriesModel, cb);

        return clipPath;
    },

    /**
     * @implement
     */
    containPoint: function (point, seriesModel) {
        var data = seriesModel.getData();
        var itemLayout = data.getItemLayout(0);
        if (itemLayout) {
            var dx = point[0] - itemLayout.cx;
            var dy = point[1] - itemLayout.cy;
            var radius = Math.sqrt(dx * dx + dy * dy);
            return radius <= itemLayout.r && radius >= itemLayout.r0;
L
lang 已提交
408
        }
S
sushuang 已提交
409
    }
1
100pah 已提交
410

S
sushuang 已提交
411
});
L
lang 已提交
412

C
cuijian 已提交
413
export default PieView;