LineView.js 13.6 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

    function isPointsSame(points1, points2) {
        if (points1.length !== points2.length) {
            return;
        }
        for (var i = 0; i < points1.length; i++) {
L
lang 已提交
19 20
            var p1 = points1[i];
            var p2 = points2[i];
21 22 23 24 25 26
            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
    function sign(val) {
        return val >= 0 ? 1 : -1;
L
lang 已提交
42 43 44 45 46 47 48
    }
    /**
     * @param {module:echarts/coord/cartesian/Cartesian2D|module:echarts/coord/polar/Polar} coordSys
     * @param {module:echarts/data/List} data
     * @param {Array.<Array.<number>>} points
     * @private
     */
L
lang 已提交
49
    function getStackedOnPoints(coordSys, data) {
L
lang 已提交
50 51
        var baseAxis = coordSys.getBaseAxis();
        var valueAxis = coordSys.getOtherAxis(baseAxis);
L
lang 已提交
52 53
        var valueStart = baseAxis.onZero
            ? 0 : valueAxis.scale.getExtent()[0];
L
lang 已提交
54 55

        var valueDim = valueAxis.dim;
L
lang 已提交
56 57 58

        // var dims = coordSys.type === 'cartesian2d' ? ['x', 'y'] : ['radius', 'angle'];
        var baseDataOffset = valueDim === 'x' || valueDim === 'radius' ? 1 : 0;
L
lang 已提交
59 60 61 62

        return data.map([valueDim], function (val, idx) {
            var stackedOnSameSign;
            var stackedOn = data.stackedOn;
L
lang 已提交
63 64 65 66 67 68
            // Find first stacked value with same sign
            while (stackedOn &&
                sign(stackedOn.get(valueDim, idx)) === sign(val)
            ) {
                stackedOnSameSign = stackedOn;
                break;
L
lang 已提交
69
            }
L
lang 已提交
70 71 72 73 74 75
            var stackedData = [];
            stackedData[baseDataOffset] = data.get(baseAxis.dim, idx);
            stackedData[1 - baseDataOffset] = stackedOnSameSign
                ? stackedOnSameSign.get(valueDim, idx, true) : valueStart;

            return coordSys.dataToPoint(stackedData);
L
lang 已提交
76
        }, true);
L
lang 已提交
77 78
    }

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
            var lineStyleModel = seriesModel.getModel('itemStyle.normal.lineStyle');
            var areaStyleModel = seriesModel.getModel('itemStyle.normal.areaStyle');

            var points = data.map(data.getItemLayout, true);
97

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

L
lang 已提交
101
            var dataSymbol = this._dataSymbol;
L
lang 已提交
102
            var polyline = this._polyline;
L
lang 已提交
103 104 105 106 107
            var polygon = this._polygon;

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

            var isAreaChart = !areaStyleModel.isEmpty();
L
lang 已提交
108
            var stackedOnPoints = getStackedOnPoints(coordSys, data);
L
lang 已提交
109

L
lang 已提交
110
            // Initialization animation or coordinate system changed
L
lang 已提交
111 112 113
            if (
                !(polyline
                && prevCoordSys.type === coordSys.type
L
lang 已提交
114
                && hasAnimation)
L
lang 已提交
115
            ) {
L
lang 已提交
116 117 118 119 120 121 122 123 124
                dataSymbol.updateData(data, hasAnimation);

                polyline = this._newPolyline(group, points, coordSys, hasAnimation);
                if (isAreaChart) {
                    polygon = this._newPolygon(
                        group, points,
                        stackedOnPoints,
                        coordSys, hasAnimation
                    );
L
lang 已提交
125
                }
L
lang 已提交
126 127
            }
            else {
L
lang 已提交
128

L
lang 已提交
129 130
                dataSymbol.updateData(data, false);

L
lang 已提交
131
                // Update clipPath
L
lang 已提交
132 133 134 135 136 137 138
                // FIXME Clip path used by more than one elements
                polyline.setClipPath(
                    this._createClipShape(coordSys)
                );
                polygon && polygon.setClipPath(
                    this._createClipShape(coordSys)
                );
L
lang 已提交
139

140 141
                // In the case data zoom triggerred refreshing frequently
                // Data may not change if line has a category axis. So it should animate nothing
L
lang 已提交
142 143 144
                if (!isPointsSame(this._stackedOnPoints, stackedOnPoints)
                    || !isPointsSame(this._points, points)
                ) {
L
lang 已提交
145
                    this._updateAnimation(
L
lang 已提交
146
                        data, stackedOnPoints, coordSys
L
lang 已提交
147
                    );
L
lang 已提交
148
                }
L
lang 已提交
149
                // Add back
L
lang 已提交
150
                group.add(polyline);
L
lang 已提交
151
                group.add(polygon);
L
lang 已提交
152 153
            }

L
lang 已提交
154
            polyline.setStyle(zrUtil.extend(
L
lang 已提交
155
                lineStyleModel.getLineStyle(),
L
lang 已提交
156 157 158 159 160
                {
                    stroke: data.getVisual('color'),
                    lineJoin: 'bevel'
                }
            ));
L
lang 已提交
161 162
            if (polygon) {
                polygon.style.opacity = 0.7;
L
lang 已提交
163
                polygon.setStyle(zrUtil.defaults(
L
lang 已提交
164 165 166 167 168 169 170
                    areaStyleModel.getAreaStyle(),
                    {
                        fill: data.getVisual('color'),
                        lineJoin: 'bevel'
                    }
                ));
            }
L
lang 已提交
171

L
lang 已提交
172
            this._data = data;
173

L
lang 已提交
174
            // Save the coordinate system for transition animation when data changed
L
lang 已提交
175
            this._coordSys = coordSys;
L
lang 已提交
176
            this._stackedOnPoints = stackedOnPoints;
L
lang 已提交
177
            this._points = points;
L
lang 已提交
178 179 180

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

L
lang 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        /**
         * @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 已提交
201 202
                silent: true,
                z2: 10
L
lang 已提交
203 204 205 206 207 208 209 210 211 212 213 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
            });

            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 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
        /**
         * @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 已提交
265
        _updateAnimation: function (data, stackedOnPoints, coordSys) {
L
lang 已提交
266
            var polyline = this._polyline;
L
lang 已提交
267 268
            var polygon = this._polygon;

L
lang 已提交
269
            var diff = lineAnimationDiff(
L
lang 已提交
270 271
                this._data, data,
                this._stackedOnPoints, stackedOnPoints,
L
lang 已提交
272
                this._coordSys, coordSys
L
lang 已提交
273
            );
L
lang 已提交
274 275 276 277 278 279 280
            polyline.shape.points = diff.current;
            polyline.animateTo({
                shape: {
                    points: diff.next
                }
            }, 300, 'cubicOut');

L
lang 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293
            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 已提交
294 295
            var updatedDataInfo = [];
            var addedDataIndices = [];
L
lang 已提交
296 297 298
            var diffStatus = diff.status;

            for (var i = 0; i < diffStatus.length; i++) {
L
lang 已提交
299 300
                var cmd = diffStatus[i].cmd;
                if (cmd === '=') {
L
lang 已提交
301 302 303 304 305 306 307
                    var el = data.getItemGraphicEl(diffStatus[i].idx1);
                    if (el) {
                        updatedDataInfo.push({
                            el: el,
                            ptIdx: i    // Index of points
                        });
                    }
L
lang 已提交
308 309 310
                }
                else if (cmd === '+') {
                    addedDataIndices.push(diffStatus[i].idx);
L
lang 已提交
311 312 313 314
                }
            }

            if (polyline.animators) {
L
lang 已提交
315 316
                for (var i = 0; i < addedDataIndices.length; i++) {
                    var el = data.getItemGraphicEl(addedDataIndices[i]);
L
lang 已提交
317
                    if (el) {
L
lang 已提交
318
                        el.scale = [0, 0];
L
lang 已提交
319
                        el.animateTo({
L
lang 已提交
320
                            scale: [1, 1]
L
lang 已提交
321 322
                        }, 300, 300, 'cubicOut');
                    }
L
lang 已提交
323
                }
L
lang 已提交
324
                polyline.animators[0].during(function () {
L
lang 已提交
325 326
                    for (var i = 0; i < updatedDataInfo.length; i++) {
                        var el = updatedDataInfo[i].el;
L
lang 已提交
327
                        vector.copy(
L
lang 已提交
328 329
                            el.position,
                            // synchronizing with the point on line
L
lang 已提交
330
                            polyline.shape.points[updatedDataInfo[i].ptIdx]
L
lang 已提交
331
                        );
L
lang 已提交
332
                        el.dirty();
L
lang 已提交
333 334 335
                    }
                });
            }
L
lang 已提交
336 337
        },

L
lang 已提交
338 339 340 341 342 343 344
        _createClipShape: function (coordSys, hasAnimation) {
            return coordSys.type === 'polar'
                ? this._createPolarClipShape(coordSys, hasAnimation)
                : this._createGridClipShape(coordSys, hasAnimation);
        },

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

348
            var clipPath = new graphic.Rect({
L
lang 已提交
349 350 351
                shape: {
                    x: xExtent[0],
                    y: yExtent[0],
L
lang 已提交
352
                    width: xExtent[1] - xExtent[0],
L
lang 已提交
353 354 355 356
                    height: yExtent[1] - yExtent[0]
                }
            });

L
lang 已提交
357
            if (animation) {
L
lang 已提交
358
                clipPath.shape[cartesian.getBaseAxis().isHorizontal() ? 'width' : 'height'] = 0;
L
lang 已提交
359 360 361 362 363
                clipPath.animateTo({
                    shape: {
                        width: xExtent[1] - xExtent[0],
                        height: yExtent[1] - yExtent[0]
                    }
L
lang 已提交
364
                }, 1500);
L
lang 已提交
365
            }
L
lang 已提交
366 367 368 369

            return clipPath;
        },

370
        _createPolarClipShape: function (polar, animation) {
L
lang 已提交
371 372 373 374 375
            // var angleAxis = polar.getAngleAxis();
            var radiusAxis = polar.getRadiusAxis();

            var radiusExtent = radiusAxis.getExtent();

L
lang 已提交
376 377
            var PI2 = Math.PI * 2;

378
            var clipPath = new graphic.Sector({
L
lang 已提交
379 380 381 382 383 384
                shape: {
                    cx: polar.cx,
                    cy: polar.cy,
                    r0: radiusExtent[0],
                    r: radiusExtent[1],
                    startAngle: 0,
L
lang 已提交
385
                    endAngle: PI2
L
lang 已提交
386 387 388
                }
            });

389 390 391 392
            if (animation) {
                clipPath.shape.endAngle = 0;
                clipPath.animateTo({
                    shape: {
L
lang 已提交
393
                        endAngle: PI2
394 395 396
                    }
                }, 1500, animation);
            }
L
lang 已提交
397 398

            return clipPath;
L
lang 已提交
399 400 401
        },

        remove: function () {
L
lang 已提交
402 403 404
            var group = this.group;
            group.remove(this._polyline);
            group.remove(this._polygon);
L
lang 已提交
405
            this._dataSymbol.remove(true);
L
lang 已提交
406 407 408
        }
    });
});