LineView.js 14.1 KB
Newer Older
L
Add pie  
lang 已提交
1 2
// TODO Smooth
// TODO '-' data
L
lang 已提交
3 4 5 6
define(function(require) {

    'use strict';

L
lang 已提交
7
    var zrUtil = require('zrender/core/util');
L
lang 已提交
8
    var vector = require('zrender/core/vector');
L
lang 已提交
9
    var DataSymbol = require('../helper/DataSymbol');
10
    var lineAnimationDiff = require('./lineAnimationDiff');
11
    var graphic = require('../../util/graphic');
L
lang 已提交
12
    var AreaPath = require('./Area');
13 14 15 16 17 18 19 20 21 22 23 24 25 26

    function isPointsSame(points1, points2) {
        if (points1.length !== points2.length) {
            return;
        }
        for (var i = 0; i < points1.length; i++) {
            var p1 = points1[i].point;
            var p2 = points2[i].point;
            if (p1[0] !== p2[0] || p1[1] !== p2[1]) {
                return;
            }
        }
        return true;
    }
L
lang 已提交
27

L
lang 已提交
28 29 30 31 32 33 34 35 36 37 38 39
    function getAxisExtentWithGap(axis) {
        var extent = axis.getExtent();
        if (axis.onBand) {
            // Remove extra 1px to avoid line miter in clipped edge
            var halfBandWidth = axis.getBandWidth() / 2 - 1;
            var dir = extent[1] > extent[0] ? 1 : -1;
            extent[0] += dir * halfBandWidth;
            extent[1] -= dir * halfBandWidth;
        }
        return extent;
    }

L
lang 已提交
40 41 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
    function getDataArray(coordSys, data, points) {
        var dimensions = coordSys.type === 'cartesian2d' ? ['x', 'y'] : ['radius', 'angle'];
        return data.map(dimensions, function (x, y, idx) {
            return {
                x: x,
                y: y,
                point: points[idx],
                name: data.getName(idx),
                idx: idx,
                rawIdx: data.getRawIndex(idx)
            };
        });
    }

    /**
     * @param {module:echarts/coord/cartesian/Cartesian2D|module:echarts/coord/polar/Polar} coordSys
     * @param {module:echarts/data/List} data
     * @param {Array.<Array.<number>>} points
     * @private
     */
    function getStackedOnPoints(coordSys, data, points) {
        var stackedOnData = data.stackedOn;
        if (stackedOnData) {
            return stackedOnData.map(stackedOnData.getItemLayout, true);
        }
        else {
            var valueAxis = coordSys.getOtherAxis(coordSys.getBaseAxis());
            var valueStart = valueAxis.getExtent()[0];
            var dim = valueAxis.dim;
            var baseCoordOffset = dim === 'x' || dim === 'radius' ? 1 : 0;
            return zrUtil.map(points, function (point, idx) {
                var pt = [];
                pt[baseCoordOffset] = point[baseCoordOffset];
                pt[1 - baseCoordOffset] = valueStart;
                return pt;
            });
        }
    }

L
lang 已提交
79 80 81 82
    return require('../../echarts').extendChartView({

        type: 'line',

L
lang 已提交
83
        init: function () {
L
lang 已提交
84 85 86
            var dataSymbol = new DataSymbol();
            this.group.add(dataSymbol.group);
            this._dataSymbol = dataSymbol;
L
lang 已提交
87
        },
L
lang 已提交
88

89
        render: function (seriesModel, ecModel) {
L
lang 已提交
90
            var coordSys = seriesModel.coordinateSystem;
L
lang 已提交
91
            var group = this.group;
L
lang 已提交
92
            var data = seriesModel.getData();
L
lang 已提交
93 94 95 96 97
            var lineStyleModel = seriesModel.getModel('itemStyle.normal.lineStyle');
            var areaStyleModel = seriesModel.getModel('itemStyle.normal.areaStyle');

            var points = data.map(data.getItemLayout, true);
            var dataArray = getDataArray(coordSys, data, points);
98

L
lang 已提交
99
            var isCoordSysPolar = coordSys.type === 'polar';
L
lang 已提交
100
            var prevCoordSys = this._coordSys;
L
lang 已提交
101

L
lang 已提交
102
            var dataSymbol = this._dataSymbol;
L
lang 已提交
103
            var polyline = this._polyline;
L
lang 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
            var polygon = this._polygon;

            var hasAnimation = ecModel.get('animation');

            var isAreaChart = !areaStyleModel.isEmpty();
            var stackedOnPoints = getStackedOnPoints(
                coordSys, data, points
            );

            var stackedOnDataArray = data.stackedOn ? getDataArray(
                    coordSys, data.stackedOn, stackedOnPoints
                ) : zrUtil.map(stackedOnPoints, function (pt) {
                    return {
                        point: pt
                    };
                });
L
lang 已提交
120

L
lang 已提交
121
            // Initialization animation or coordinate system changed
L
lang 已提交
122 123 124
            if (
                !(polyline
                && prevCoordSys.type === coordSys.type
L
lang 已提交
125
                && hasAnimation)
L
lang 已提交
126
            ) {
L
lang 已提交
127 128 129 130 131 132 133 134 135
                dataSymbol.updateData(data, hasAnimation);

                polyline = this._newPolyline(group, points, coordSys, hasAnimation);
                if (isAreaChart) {
                    polygon = this._newPolygon(
                        group, points,
                        stackedOnPoints,
                        coordSys, hasAnimation
                    );
L
lang 已提交
136
                }
L
lang 已提交
137 138
            }
            else {
L
lang 已提交
139

L
lang 已提交
140 141
                dataSymbol.updateData(data, false);

L
lang 已提交
142
                // Update clipPath
L
lang 已提交
143 144 145 146 147 148 149
                // FIXME Clip path used by more than one elements
                polyline.setClipPath(
                    this._createClipShape(coordSys)
                );
                polygon && polygon.setClipPath(
                    this._createClipShape(coordSys)
                );
L
lang 已提交
150

151 152
                // In the case data zoom triggerred refreshing frequently
                // Data may not change if line has a category axis. So it should animate nothing
153 154 155
                if (!isPointsSame(this._dataArray, dataArray)
                    || !isPointsSame(this._stackedOnDataArray, stackedOnDataArray)
                ) {
L
lang 已提交
156
                    this._updateAnimation(
L
lang 已提交
157
                        data, dataArray, stackedOnDataArray, coordSys
L
lang 已提交
158
                    );
159
                }
L
lang 已提交
160
                // Add back
L
lang 已提交
161
                group.add(polyline);
L
lang 已提交
162
                group.add(polygon);
L
lang 已提交
163 164
            }

L
lang 已提交
165
            polyline.setStyle(zrUtil.extend(
L
lang 已提交
166
                lineStyleModel.getLineStyle(),
L
lang 已提交
167 168 169 170 171
                {
                    stroke: data.getVisual('color'),
                    lineJoin: 'bevel'
                }
            ));
L
lang 已提交
172 173 174 175 176 177 178 179 180 181
            if (polygon) {
                polygon.style.opacity = 0.7;
                polygon.setStyle(zrUtil.extend(
                    areaStyleModel.getAreaStyle(),
                    {
                        fill: data.getVisual('color'),
                        lineJoin: 'bevel'
                    }
                ));
            }
L
lang 已提交
182

L
lang 已提交
183
            this._data = data;
184

L
lang 已提交
185
            // Save the coordinate system and data for transition animation when data changed
L
lang 已提交
186 187
            this._dataArray = dataArray;
            this._stackedOnDataArray = stackedOnDataArray;
L
lang 已提交
188
            this._coordSys = coordSys;
L
lang 已提交
189 190 191

            !isCoordSysPolar && !seriesModel.get('showAllSymbol')
                && this._updateSymbolDisplay(data, coordSys);
192 193
        },

L
lang 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        /**
         * @param {module:zrender/container/Group} group
         * @param {Array.<Array.<number>>} points
         * @param {module:echarts/coord/cartesian/Cartesian2D|module:echarts/coord/polar/Polar} coordSys
         * @param {boolean} hasAnimation
         * @private
         */
        _newPolyline: function (group, points, coordSys, hasAnimation) {
            var polyline = this._polyline;
            // Remove previous created polyline
            if (polyline) {
                group.remove(polyline);
            }

            polyline = new graphic.Polyline({
                shape: {
                    points: points
                },
L
lang 已提交
212 213
                silent: true,
                z2: 10
L
lang 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
            });

            var clipPath = this._createClipShape(coordSys, hasAnimation);
            polyline.setClipPath(clipPath);

            group.add(polyline);

            this._polyline = polyline;

            return polyline;
        },

        /**
         * @param {module:zrender/container/Group} group
         * @param {Array.<Array.<number>>} stackedOnPoints
         * @param {Array.<Array.<number>>} points
         * @param {module:echarts/coord/cartesian/Cartesian2D|module:echarts/coord/polar/Polar} coordSys
         * @param {boolean} hasAnimation
         * @private
         */
        _newPolygon: function (group, points, stackedOnPoints, coordSys, hasAnimation) {
            var polygon = this._polygon;
            // Remove previous created polygon
            if (polygon) {
                group.remove(polygon);
            }

            polygon = new AreaPath({
                shape: {
                    points: points,
                    stackedOnPoints: stackedOnPoints
                },
                silent: true
            });

            var clipPath = this._createClipShape(coordSys, hasAnimation);
            polygon.setClipPath(clipPath);

            group.add(polygon);

            this._polygon = polygon;
            return polygon;
        },
L
lang 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
        /**
         * @private
         */
        _updateSymbolDisplay: function (data, coordSys) {
            var categoryAxis = coordSys.getAxesByScale('ordinal')[0]
            // `getLabelInterval` is provided by echarts/component/axis
            if (categoryAxis && categoryAxis.getLabelInterval) {
                var labelInterval = categoryAxis.getLabelInterval();
                data.eachItemGraphicEl(function (el, idx) {
                    el.ignore = (typeof labelInterval === 'function')
                        && !labelInterval(idx, categoryAxis.scale.getItem(idx))
                        || idx % (labelInterval + 1);
                });
            }
        },

        /**
         * @private
         */
L
lang 已提交
276
        _updateAnimation: function (data, dataArray, stackedOnDataArray, coordSys) {
L
lang 已提交
277
            var polyline = this._polyline;
L
lang 已提交
278 279
            var polygon = this._polygon;

L
lang 已提交
280
            var diff = lineAnimationDiff(
L
lang 已提交
281 282 283
                this._dataArray, dataArray,
                this._stackedOnDataArray, stackedOnDataArray,
                this._coordSys, coordSys
L
lang 已提交
284
            );
L
lang 已提交
285 286 287 288 289 290 291
            polyline.shape.points = diff.current;
            polyline.animateTo({
                shape: {
                    points: diff.next
                }
            }, 300, 'cubicOut');

L
lang 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304
            if (polygon) {
                var polygonShape = polygon.shape;
                polygonShape.points = diff.current;
                polygonShape.stackedOnPoints = diff.stackedOnCurrent;

                polygon.animateTo({
                    shape: {
                        points: diff.next,
                        stackedOnPoints: diff.stackedOnNext
                    }
                }, 300, 'cubicOut');
            }

L
lang 已提交
305 306
            var updatedDataInfo = [];
            var addedDataIndices = [];
L
lang 已提交
307 308 309
            var diffStatus = diff.status;

            for (var i = 0; i < diffStatus.length; i++) {
L
lang 已提交
310 311
                var cmd = diffStatus[i].cmd;
                if (cmd === '=') {
L
lang 已提交
312 313 314 315 316 317 318
                    var el = data.getItemGraphicEl(diffStatus[i].idx1);
                    if (el) {
                        updatedDataInfo.push({
                            el: el,
                            ptIdx: i    // Index of points
                        });
                    }
L
lang 已提交
319 320 321
                }
                else if (cmd === '+') {
                    addedDataIndices.push(diffStatus[i].idx);
L
lang 已提交
322 323 324 325
                }
            }

            if (polyline.animators) {
L
lang 已提交
326 327
                for (var i = 0; i < addedDataIndices.length; i++) {
                    var el = data.getItemGraphicEl(addedDataIndices[i]);
L
lang 已提交
328
                    if (el) {
L
lang 已提交
329
                        el.scale = [0, 0];
L
lang 已提交
330
                        el.animateTo({
L
lang 已提交
331
                            scale: [1, 1]
L
lang 已提交
332 333
                        }, 300, 300, 'cubicOut');
                    }
L
lang 已提交
334
                }
L
lang 已提交
335
                polyline.animators[0].during(function () {
L
lang 已提交
336 337
                    for (var i = 0; i < updatedDataInfo.length; i++) {
                        var el = updatedDataInfo[i].el;
L
lang 已提交
338
                        vector.copy(
L
lang 已提交
339 340
                            el.position,
                            // synchronizing with the point on line
L
lang 已提交
341
                            polyline.shape.points[updatedDataInfo[i].ptIdx]
L
lang 已提交
342
                        );
L
lang 已提交
343
                        el.dirty();
L
lang 已提交
344 345 346
                    }
                });
            }
L
lang 已提交
347 348
        },

L
lang 已提交
349 350 351 352 353 354 355
        _createClipShape: function (coordSys, hasAnimation) {
            return coordSys.type === 'polar'
                ? this._createPolarClipShape(coordSys, hasAnimation)
                : this._createGridClipShape(coordSys, hasAnimation);
        },

        _createGridClipShape: function (cartesian, animation) {
L
lang 已提交
356 357
            var xExtent = getAxisExtentWithGap(cartesian.getAxis('x'));
            var yExtent = getAxisExtentWithGap(cartesian.getAxis('y'));
L
lang 已提交
358

359
            var clipPath = new graphic.Rect({
L
lang 已提交
360 361 362
                shape: {
                    x: xExtent[0],
                    y: yExtent[0],
L
lang 已提交
363
                    width: xExtent[1] - xExtent[0],
L
lang 已提交
364 365 366 367
                    height: yExtent[1] - yExtent[0]
                }
            });

L
lang 已提交
368
            if (animation) {
L
lang 已提交
369
                clipPath.shape[cartesian.getBaseAxis().isHorizontal() ? 'width' : 'height'] = 0;
L
lang 已提交
370 371 372 373 374
                clipPath.animateTo({
                    shape: {
                        width: xExtent[1] - xExtent[0],
                        height: yExtent[1] - yExtent[0]
                    }
L
lang 已提交
375
                }, 1500);
L
lang 已提交
376
            }
L
lang 已提交
377 378 379 380

            return clipPath;
        },

381
        _createPolarClipShape: function (polar, animation) {
L
lang 已提交
382 383 384 385 386
            // var angleAxis = polar.getAngleAxis();
            var radiusAxis = polar.getRadiusAxis();

            var radiusExtent = radiusAxis.getExtent();

L
lang 已提交
387 388
            var PI2 = Math.PI * 2;

389
            var clipPath = new graphic.Sector({
L
lang 已提交
390 391 392 393 394 395
                shape: {
                    cx: polar.cx,
                    cy: polar.cy,
                    r0: radiusExtent[0],
                    r: radiusExtent[1],
                    startAngle: 0,
L
lang 已提交
396
                    endAngle: PI2
L
lang 已提交
397 398 399
                }
            });

400 401 402 403
            if (animation) {
                clipPath.shape.endAngle = 0;
                clipPath.animateTo({
                    shape: {
L
lang 已提交
404
                        endAngle: PI2
405 406 407
                    }
                }, 1500, animation);
            }
L
lang 已提交
408 409

            return clipPath;
L
lang 已提交
410 411 412
        },

        remove: function () {
L
lang 已提交
413 414 415
            var group = this.group;
            group.remove(this._polyline);
            group.remove(this._polygon);
L
lang 已提交
416
            this._dataSymbol.remove(true);
L
lang 已提交
417 418 419
        }
    });
});