LineView.js 14.5 KB
Newer Older
L
lang 已提交
1 2 3 4
define(function(require) {

    'use strict';

L
lang 已提交
5
    var zrUtil = require('zrender/core/util');
L
lang 已提交
6
    var vector = require('zrender/core/vector');
L
lang 已提交
7
    var DataSymbol = require('../helper/DataSymbol');
8
    var lineAnimationDiff = require('./lineAnimationDiff');
9
    var graphic = require('../../util/graphic');
L
lang 已提交
10 11

    var polyHelper = require('./poly');
12 13 14 15 16 17

    function isPointsSame(points1, points2) {
        if (points1.length !== points2.length) {
            return;
        }
        for (var i = 0; i < points1.length; i++) {
L
lang 已提交
18 19
            var p1 = points1[i];
            var p2 = points2[i];
20 21 22 23 24 25
            if (p1[0] !== p2[0] || p1[1] !== p2[1]) {
                return;
            }
        }
        return true;
    }
L
lang 已提交
26

L
lang 已提交
27 28 29 30
    function getSmooth(smooth) {
        return typeof (smooth) === 'number' ? smooth : (smooth ? 0.2 : 0);
    }

L
lang 已提交
31 32 33 34 35 36 37 38 39 40 41 42
    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 已提交
43 44
    function sign(val) {
        return val >= 0 ? 1 : -1;
L
lang 已提交
45 46 47 48 49 50 51
    }
    /**
     * @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 已提交
52
    function getStackedOnPoints(coordSys, data) {
L
lang 已提交
53 54
        var baseAxis = coordSys.getBaseAxis();
        var valueAxis = coordSys.getOtherAxis(baseAxis);
L
lang 已提交
55 56
        var valueStart = baseAxis.onZero
            ? 0 : valueAxis.scale.getExtent()[0];
L
lang 已提交
57 58

        var valueDim = valueAxis.dim;
L
lang 已提交
59 60

        var baseDataOffset = valueDim === 'x' || valueDim === 'radius' ? 1 : 0;
L
lang 已提交
61

L
lang 已提交
62
        return data.mapArray([valueDim], function (val, idx) {
L
lang 已提交
63 64
            var stackedOnSameSign;
            var stackedOn = data.stackedOn;
L
lang 已提交
65 66 67 68 69 70
            // Find first stacked value with same sign
            while (stackedOn &&
                sign(stackedOn.get(valueDim, idx)) === sign(val)
            ) {
                stackedOnSameSign = stackedOn;
                break;
L
lang 已提交
71
            }
L
lang 已提交
72 73 74 75 76 77
            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 已提交
78
        }, true);
L
lang 已提交
79 80
    }

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

        type: 'line',

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

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

L
lang 已提交
98
            var points = data.mapArray(data.getItemLayout, true);
99

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

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

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

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

L
lang 已提交
112 113 114 115

            var symbolIgnoreMap = !isCoordSysPolar && !seriesModel.get('showAllSymbol')
                && this._getSymbolIgnored(data, coordSys);

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

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

L
lang 已提交
137 138 139
                dataSymbol.updateData(
                    data, seriesModel, false, symbolIgnoreMap
                );
L
lang 已提交
140

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

150 151
                // 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 已提交
152 153 154
                if (!isPointsSame(this._stackedOnPoints, stackedOnPoints)
                    || !isPointsSame(this._points, points)
                ) {
L
lang 已提交
155
                    this._updateAnimation(
L
lang 已提交
156
                        data, stackedOnPoints, coordSys
L
lang 已提交
157
                    );
L
lang 已提交
158
                }
L
lang 已提交
159
                // Add back
L
lang 已提交
160
                group.add(polyline);
L
lang 已提交
161
                group.add(polygon);
L
lang 已提交
162 163
            }

L
lang 已提交
164
            polyline.setStyle(zrUtil.extend(
L
lang 已提交
165
                lineStyleModel.getLineStyle(),
L
lang 已提交
166 167 168 169 170
                {
                    stroke: data.getVisual('color'),
                    lineJoin: 'bevel'
                }
            ));
L
lang 已提交
171 172 173 174 175

            var smooth = seriesModel.get('smooth');
            smooth = getSmooth(seriesModel.get('smooth'));
            polyline.shape.smooth = smooth;

L
lang 已提交
176
            if (polygon) {
L
lang 已提交
177 178 179 180
                var polygonShape = polygon.shape;
                var stackedOn = data.stackedOn;
                var stackedOnSmooth = 0;

L
lang 已提交
181
                polygon.style.opacity = 0.7;
L
lang 已提交
182
                polygon.setStyle(zrUtil.defaults(
L
lang 已提交
183 184 185 186 187 188
                    areaStyleModel.getAreaStyle(),
                    {
                        fill: data.getVisual('color'),
                        lineJoin: 'bevel'
                    }
                ));
L
lang 已提交
189 190 191 192 193 194 195 196
                polygonShape.smooth = smooth;

                if (stackedOn) {
                    var stackedOnSeries = stackedOn.hostModel;
                    stackedOnSmooth = getSmooth(stackedOnSeries.get('smooth'))
                }

                polygonShape.stackedOnSmooth = stackedOnSmooth;
L
lang 已提交
197
            }
L
lang 已提交
198

L
lang 已提交
199
            this._data = data;
200

L
lang 已提交
201
            // Save the coordinate system for transition animation when data changed
L
lang 已提交
202
            this._coordSys = coordSys;
L
lang 已提交
203
            this._stackedOnPoints = stackedOnPoints;
L
lang 已提交
204
            this._points = points;
205 206
        },

L
lang 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220
        /**
         * @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);
            }

L
lang 已提交
221
            polyline = new polyHelper.Polyline({
L
lang 已提交
222 223 224
                shape: {
                    points: points
                },
L
lang 已提交
225 226
                silent: true,
                z2: 10
L
lang 已提交
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
            });

            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);
            }

L
lang 已提交
254
            polygon = new polyHelper.Polygon({
L
lang 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
                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 已提交
270 271 272
        /**
         * @private
         */
L
lang 已提交
273 274 275
        _getSymbolIgnored: function (data, coordSys) {
            var categoryAxis = coordSys.getAxesByScale('ordinal')[0];
            var ignoreMap;
L
lang 已提交
276 277
            // `getLabelInterval` is provided by echarts/component/axis
            if (categoryAxis && categoryAxis.getLabelInterval) {
L
lang 已提交
278
                ignoreMap = [];
L
lang 已提交
279
                var labelInterval = categoryAxis.getLabelInterval();
L
lang 已提交
280 281
                data.each(function (idx) {
                    ignoreMap[idx] = (typeof labelInterval === 'function')
L
lang 已提交
282 283 284 285
                        && !labelInterval(idx, categoryAxis.scale.getItem(idx))
                        || idx % (labelInterval + 1);
                });
            }
L
lang 已提交
286
            return ignoreMap;
L
lang 已提交
287 288 289 290 291
        },

        /**
         * @private
         */
L
lang 已提交
292
        _updateAnimation: function (data, stackedOnPoints, coordSys) {
L
lang 已提交
293
            var polyline = this._polyline;
L
lang 已提交
294 295
            var polygon = this._polygon;

L
lang 已提交
296
            var diff = lineAnimationDiff(
L
lang 已提交
297 298
                this._data, data,
                this._stackedOnPoints, stackedOnPoints,
L
lang 已提交
299
                this._coordSys, coordSys
L
lang 已提交
300
            );
L
lang 已提交
301 302 303 304 305 306 307
            polyline.shape.points = diff.current;
            polyline.animateTo({
                shape: {
                    points: diff.next
                }
            }, 300, 'cubicOut');

L
lang 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320
            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 已提交
321 322
            var updatedDataInfo = [];
            var addedDataIndices = [];
L
lang 已提交
323 324 325
            var diffStatus = diff.status;

            for (var i = 0; i < diffStatus.length; i++) {
L
lang 已提交
326 327
                var cmd = diffStatus[i].cmd;
                if (cmd === '=') {
L
lang 已提交
328 329 330 331 332 333 334
                    var el = data.getItemGraphicEl(diffStatus[i].idx1);
                    if (el) {
                        updatedDataInfo.push({
                            el: el,
                            ptIdx: i    // Index of points
                        });
                    }
L
lang 已提交
335 336 337
                }
                else if (cmd === '+') {
                    addedDataIndices.push(diffStatus[i].idx);
L
lang 已提交
338 339 340 341
                }
            }

            if (polyline.animators) {
L
lang 已提交
342 343
                for (var i = 0; i < addedDataIndices.length; i++) {
                    var el = data.getItemGraphicEl(addedDataIndices[i]);
L
lang 已提交
344
                    if (el) {
L
lang 已提交
345
                        el.scale = [0, 0];
L
lang 已提交
346
                        el.animateTo({
L
lang 已提交
347
                            scale: [1, 1]
L
lang 已提交
348 349
                        }, 300, 300, 'cubicOut');
                    }
L
lang 已提交
350
                }
L
lang 已提交
351
                polyline.animators[0].during(function () {
L
lang 已提交
352 353
                    for (var i = 0; i < updatedDataInfo.length; i++) {
                        var el = updatedDataInfo[i].el;
L
lang 已提交
354
                        vector.copy(
L
lang 已提交
355 356
                            el.position,
                            // synchronizing with the point on line
L
lang 已提交
357
                            polyline.shape.points[updatedDataInfo[i].ptIdx]
L
lang 已提交
358
                        );
L
lang 已提交
359
                        el.dirty();
L
lang 已提交
360 361 362
                    }
                });
            }
L
lang 已提交
363 364
        },

L
lang 已提交
365 366 367 368 369 370 371
        _createClipShape: function (coordSys, hasAnimation) {
            return coordSys.type === 'polar'
                ? this._createPolarClipShape(coordSys, hasAnimation)
                : this._createGridClipShape(coordSys, hasAnimation);
        },

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

375
            var clipPath = new graphic.Rect({
L
lang 已提交
376 377 378
                shape: {
                    x: xExtent[0],
                    y: yExtent[0],
L
lang 已提交
379
                    width: xExtent[1] - xExtent[0],
L
lang 已提交
380 381 382 383
                    height: yExtent[1] - yExtent[0]
                }
            });

L
lang 已提交
384
            if (animation) {
L
lang 已提交
385
                clipPath.shape[cartesian.getBaseAxis().isHorizontal() ? 'width' : 'height'] = 0;
L
lang 已提交
386 387 388 389 390
                clipPath.animateTo({
                    shape: {
                        width: xExtent[1] - xExtent[0],
                        height: yExtent[1] - yExtent[0]
                    }
L
lang 已提交
391
                }, 1500);
L
lang 已提交
392
            }
L
lang 已提交
393 394 395 396

            return clipPath;
        },

397
        _createPolarClipShape: function (polar, animation) {
L
lang 已提交
398 399 400 401 402
            // var angleAxis = polar.getAngleAxis();
            var radiusAxis = polar.getRadiusAxis();

            var radiusExtent = radiusAxis.getExtent();

L
lang 已提交
403 404
            var PI2 = Math.PI * 2;

405
            var clipPath = new graphic.Sector({
L
lang 已提交
406 407 408 409 410 411
                shape: {
                    cx: polar.cx,
                    cy: polar.cy,
                    r0: radiusExtent[0],
                    r: radiusExtent[1],
                    startAngle: 0,
L
lang 已提交
412
                    endAngle: PI2
L
lang 已提交
413 414 415
                }
            });

416 417 418 419
            if (animation) {
                clipPath.shape.endAngle = 0;
                clipPath.animateTo({
                    shape: {
L
lang 已提交
420
                        endAngle: PI2
421 422 423
                    }
                }, 1500, animation);
            }
L
lang 已提交
424 425

            return clipPath;
L
lang 已提交
426 427
        },

L
lang 已提交
428
        remove: function (ecModel) {
L
lang 已提交
429 430 431
            var group = this.group;
            group.remove(this._polyline);
            group.remove(this._polygon);
L
lang 已提交
432
            this._dataSymbol.remove(ecModel.get('animation'));
L
lang 已提交
433 434 435
        }
    });
});