From 55de79f167fcde587e06850987b4be5452502761 Mon Sep 17 00:00:00 2001 From: Ovilia Date: Fri, 6 Sep 2019 17:29:48 +0800 Subject: [PATCH] feat: support expansion animation for data update; fix #11029 --- src/chart/pie/PieSeries.js | 5 +- src/chart/pie/PieView.js | 78 +++++++++++------ test/pie-animation.html | 169 +++++++++++++++++++++++++++++++++++++ 3 files changed, 227 insertions(+), 25 deletions(-) create mode 100644 test/pie-animation.html diff --git a/src/chart/pie/PieSeries.js b/src/chart/pie/PieSeries.js index 63035d62d9..42a44e61f9 100644 --- a/src/chart/pie/PieSeries.js +++ b/src/chart/pie/PieSeries.js @@ -170,9 +170,12 @@ var PieSeries = echarts.extendSeriesModel({ borderWidth: 1 }, - // Animation type canbe expansion, scale + // Animation type. Valid values: expansion, scale animationType: 'expansion', + // Animation type when update. Valid values: transition, expansion + animationTypeUpdate: 'transition', + animationEasing: 'cubicOut' } }); diff --git a/src/chart/pie/PieView.js b/src/chart/pie/PieView.js index 1c06b7a8e3..b766c3d398 100644 --- a/src/chart/pie/PieView.js +++ b/src/chart/pie/PieView.js @@ -112,6 +112,8 @@ piePieceProto.updateData = function (data, idx, firstCreate) { var sectorShape = zrUtil.extend({}, layout); sectorShape.label = null; + var animationTypeUpdate = seriesModel.getShallow('animationTypeUpdate'); + if (firstCreate) { sector.setShape(sectorShape); @@ -136,9 +138,15 @@ piePieceProto.updateData = function (data, idx, firstCreate) { } else { - graphic.updateProps(sector, { - shape: sectorShape - }, seriesModel, idx); + if (animationTypeUpdate === 'expansion') { + // Sectors are set to be target shape and an overlaying clipPath is used for animation + sector.setShape(sectorShape); + } else { + // Transition animation from the old shape + graphic.updateProps(sector, { + shape: sectorShape + }, seriesModel, idx); + } } // Update common style @@ -167,7 +175,9 @@ piePieceProto.updateData = function (data, idx, firstCreate) { seriesModel.get('animation') ); - this._updateLabel(data, idx); + // Label and text animation should be applied only for transition type animation when update + var withAnimation = !firstCreate && animationTypeUpdate === 'transition'; + this._updateLabel(data, idx, withAnimation); this.highDownOnUpdate = (itemModel.get('hoverAnimation') && seriesModel.isAnimationEnabled()) ? function (fromState, toState) { @@ -201,7 +211,7 @@ piePieceProto.updateData = function (data, idx, firstCreate) { graphic.setHoverStyle(this); }; -piePieceProto._updateLabel = function (data, idx) { +piePieceProto._updateLabel = function (data, idx, withAnimation) { var labelLine = this.childAt(1); var labelText = this.childAt(2); @@ -218,20 +228,33 @@ piePieceProto._updateLabel = function (data, idx) { return; } - graphic.updateProps(labelLine, { - shape: { - points: labelLayout.linePoints || [ - [labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y] - ] - } - }, seriesModel, idx); + var targetLineShape = { + points: labelLayout.linePoints || [ + [labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y], [labelLayout.x, labelLayout.y] + ] + }; + var targetTextStyle = { + x: labelLayout.x, + y: labelLayout.y + }; + if (withAnimation) { + graphic.updateProps(labelLine, { + shape: targetLineShape + }, seriesModel, idx); + + graphic.updateProps(labelText, { + style: targetTextStyle + }, seriesModel, idx); + } + else { + labelLine.attr({ + shape: targetLineShape + }); + labelText.attr({ + style: targetTextStyle + }); + } - graphic.updateProps(labelText, { - style: { - x: labelLayout.x, - y: labelLayout.y - } - }, seriesModel, idx); labelText.attr({ rotation: labelLayout.rotation, origin: [labelLayout.x, labelLayout.y], @@ -309,6 +332,7 @@ var PieView = ChartView.extend({ var hasAnimation = ecModel.get('animation'); var isFirstRender = !oldData; var animationType = seriesModel.get('animationType'); + var animationTypeUpdate = seriesModel.get('animationTypeUpdate'); var onSectorClick = zrUtil.curry( updateDataSelected, this.uid, seriesModel, hasAnimation, api @@ -334,6 +358,12 @@ var PieView = ChartView.extend({ .update(function (newIdx, oldIdx) { var piePiece = oldData.getItemGraphicEl(oldIdx); + if (!isFirstRender && animationTypeUpdate !== 'transition') { + piePiece.eachChild(function (child) { + child.stopAnimation(true); + }); + } + piePiece.updateData(data, newIdx); piePiece.off('click'); @@ -348,9 +378,8 @@ var PieView = ChartView.extend({ .execute(); if ( - hasAnimation && isFirstRender && data.count() > 0 - // Default expansion animation - && animationType !== 'scale' + hasAnimation && data.count() > 0 + && (isFirstRender ? animationType !== 'scale' : animationTypeUpdate !== 'transition') ) { var shape = data.getItemLayout(0); for (var s = 1; isNaN(shape.startAngle) && s < data.count(); ++s) { @@ -361,7 +390,7 @@ var PieView = ChartView.extend({ var removeClipPath = zrUtil.bind(group.removeClipPath, group); group.setClipPath(this._createClipPath( - shape.cx, shape.cy, r, shape.startAngle, shape.clockwise, removeClipPath, seriesModel + shape.cx, shape.cy, r, shape.startAngle, shape.clockwise, removeClipPath, seriesModel, isFirstRender )); } else { @@ -375,7 +404,7 @@ var PieView = ChartView.extend({ dispose: function () {}, _createClipPath: function ( - cx, cy, r, startAngle, clockwise, cb, seriesModel + cx, cy, r, startAngle, clockwise, cb, seriesModel, isFirstRender ) { var clipPath = new graphic.Sector({ shape: { @@ -389,7 +418,8 @@ var PieView = ChartView.extend({ } }); - graphic.initProps(clipPath, { + var initOrUpdate = isFirstRender ? graphic.initProps : graphic.updateProps; + initOrUpdate(clipPath, { shape: { endAngle: startAngle + (clockwise ? 1 : -1) * Math.PI * 2 } diff --git a/test/pie-animation.html b/test/pie-animation.html new file mode 100644 index 0000000000..679b50bbb8 --- /dev/null +++ b/test/pie-animation.html @@ -0,0 +1,169 @@ + + + + + + + + + + + + + + +
Animation: Transition from previous state to new state, in 10 seconds after clicking Go
+ +
+ +
Animation: Transition all over again, in 10 seconds after clicking Go
+ +
+ + + + + + + + + + + \ No newline at end of file