LineView.js 14.1 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 SymbolDraw = require('../helper/SymbolDraw');
7
    var lineAnimationDiff = require('./lineAnimationDiff');
8
    var graphic = require('../../util/graphic');
L
lang 已提交
9 10

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

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

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

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

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

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

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

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

        type: 'line',

L
lang 已提交
84
        init: function () {
L
tweak  
lang 已提交
85 86
            var lineGroup = new graphic.Group();

L
lang 已提交
87 88
            var symbolDraw = new SymbolDraw();
            this.group.add(symbolDraw.group);
L
tweak  
lang 已提交
89 90
            this.group.add(lineGroup);

L
lang 已提交
91
            this._symbolDraw = symbolDraw;
L
tweak  
lang 已提交
92
            this._lineGroup = lineGroup;
L
lang 已提交
93
        },
L
lang 已提交
94

L
lang 已提交
95
        render: function (seriesModel, ecModel, api) {
L
lang 已提交
96
            var coordSys = seriesModel.coordinateSystem;
L
lang 已提交
97
            var group = this.group;
L
lang 已提交
98
            var data = seriesModel.getData();
99 100
            var lineStyleModel = seriesModel.getModel('lineStyle.normal');
            var areaStyleModel = seriesModel.getModel('areaStyle.normal');
L
lang 已提交
101

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

L
lang 已提交
104
            var isCoordSysPolar = coordSys.type === 'polar';
L
lang 已提交
105
            var prevCoordSys = this._coordSys;
L
lang 已提交
106

L
lang 已提交
107
            var symbolDraw = this._symbolDraw;
L
lang 已提交
108
            var polyline = this._polyline;
L
lang 已提交
109 110
            var polygon = this._polygon;

L
tweak  
lang 已提交
111 112
            var lineGroup = this._lineGroup;

L
lang 已提交
113 114 115
            var hasAnimation = ecModel.get('animation');

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

L
lang 已提交
118 119
            var isSymbolIgnore = !isCoordSysPolar && !seriesModel.get('showAllSymbol')
                && this._getSymbolIgnoreFunc(data, coordSys);
L
lang 已提交
120

L
lang 已提交
121
            // Initialization animation or coordinate system changed
L
lang 已提交
122 123
            if (
                !(polyline
L
lang 已提交
124
                && prevCoordSys.type === coordSys.type)
L
lang 已提交
125
            ) {
L
lang 已提交
126
                symbolDraw.updateData(data, api, isSymbolIgnore);
L
lang 已提交
127 128 129 130 131 132 133
                polyline = this._newPolyline(group, points, coordSys, hasAnimation);
                if (isAreaChart) {
                    polygon = this._newPolygon(
                        group, points,
                        stackedOnPoints,
                        coordSys, hasAnimation
                    );
L
lang 已提交
134
                }
L
tweak  
lang 已提交
135 136 137
                lineGroup.setClipPath(
                    this._createClipShape(coordSys, true, api)
                );
L
lang 已提交
138 139
            }
            else {
L
lang 已提交
140
                // Update clipPath
L
lang 已提交
141
                if (hasAnimation) {
L
tweak  
lang 已提交
142 143
                    lineGroup.setClipPath(
                        this._createClipShape(coordSys, false, api)
L
lang 已提交
144 145
                    );
                }
L
lang 已提交
146

L
tweak  
lang 已提交
147
                // Always update, or it is wrong in the case turning on legend because points is not changed
L
tweak  
lang 已提交
148 149
                symbolDraw.updateData(data, api, isSymbolIgnore);

L
tweak  
lang 已提交
150 151 152
                // Stop symbol animation and sync with line points
                // FIXME performance?
                data.eachItemGraphicEl(function (el) {
L
tweak  
lang 已提交
153
                    el.stopAnimation(true);
L
tweak  
lang 已提交
154 155
                });

156 157
                // 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 已提交
158 159 160
                if (!isPointsSame(this._stackedOnPoints, stackedOnPoints)
                    || !isPointsSame(this._points, points)
                ) {
L
lang 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174
                    if (hasAnimation) {
                        this._updateAnimation(
                            data, stackedOnPoints, coordSys, api
                        );
                    }
                    else {
                        polyline.setShape({
                            points: points
                        });
                        polygon && polygon.setShape({
                            points: points,
                            stackedOnPoints: stackedOnPoints
                        });
                    }
L
lang 已提交
175
                }
L
lang 已提交
176
                // Add back
L
tweak  
lang 已提交
177
                group.add(lineGroup);
L
lang 已提交
178 179
            }

180 181
            polyline.setStyle(zrUtil.defaults(
                // Use color in lineStyle first
L
lang 已提交
182
                lineStyleModel.getLineStyle(),
L
lang 已提交
183 184 185 186 187
                {
                    stroke: data.getVisual('color'),
                    lineJoin: 'bevel'
                }
            ));
L
lang 已提交
188 189 190 191 192

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

L
lang 已提交
193
            if (polygon) {
L
lang 已提交
194 195 196 197
                var polygonShape = polygon.shape;
                var stackedOn = data.stackedOn;
                var stackedOnSmooth = 0;

L
lang 已提交
198
                polygon.style.opacity = 0.7;
L
lang 已提交
199
                polygon.setStyle(zrUtil.defaults(
L
lang 已提交
200 201 202 203 204 205
                    areaStyleModel.getAreaStyle(),
                    {
                        fill: data.getVisual('color'),
                        lineJoin: 'bevel'
                    }
                ));
L
lang 已提交
206 207 208 209
                polygonShape.smooth = smooth;

                if (stackedOn) {
                    var stackedOnSeries = stackedOn.hostModel;
L
lang 已提交
210
                    stackedOnSmooth = getSmooth(stackedOnSeries.get('smooth'));
L
lang 已提交
211 212 213
                }

                polygonShape.stackedOnSmooth = stackedOnSmooth;
L
lang 已提交
214
            }
L
lang 已提交
215

L
lang 已提交
216
            this._data = data;
L
lang 已提交
217
            // Save the coordinate system for transition animation when data changed
L
lang 已提交
218
            this._coordSys = coordSys;
L
lang 已提交
219
            this._stackedOnPoints = stackedOnPoints;
L
lang 已提交
220
            this._points = points;
221 222
        },

L
lang 已提交
223 224 225 226 227
        /**
         * @param {module:zrender/container/Group} group
         * @param {Array.<Array.<number>>} points
         * @private
         */
L
tweak  
lang 已提交
228
        _newPolyline: function (group, points) {
L
lang 已提交
229 230 231 232 233 234
            var polyline = this._polyline;
            // Remove previous created polyline
            if (polyline) {
                group.remove(polyline);
            }

L
lang 已提交
235
            polyline = new polyHelper.Polyline({
L
lang 已提交
236 237 238
                shape: {
                    points: points
                },
L
lang 已提交
239 240
                silent: true,
                z2: 10
L
lang 已提交
241 242
            });

L
tweak  
lang 已提交
243
            this._lineGroup.add(polyline);
L
lang 已提交
244 245 246 247 248 249 250 251 252 253 254 255

            this._polyline = polyline;

            return polyline;
        },

        /**
         * @param {module:zrender/container/Group} group
         * @param {Array.<Array.<number>>} stackedOnPoints
         * @param {Array.<Array.<number>>} points
         * @private
         */
L
tweak  
lang 已提交
256
        _newPolygon: function (group, points, stackedOnPoints) {
L
lang 已提交
257 258 259 260 261 262
            var polygon = this._polygon;
            // Remove previous created polygon
            if (polygon) {
                group.remove(polygon);
            }

L
lang 已提交
263
            polygon = new polyHelper.Polygon({
L
lang 已提交
264 265 266 267 268 269 270
                shape: {
                    points: points,
                    stackedOnPoints: stackedOnPoints
                },
                silent: true
            });

L
tweak  
lang 已提交
271
            this._lineGroup.add(polygon);
L
lang 已提交
272 273 274 275

            this._polygon = polygon;
            return polygon;
        },
L
lang 已提交
276 277 278
        /**
         * @private
         */
L
lang 已提交
279
        _getSymbolIgnoreFunc: function (data, coordSys) {
L
lang 已提交
280
            var categoryAxis = coordSys.getAxesByScale('ordinal')[0];
L
lang 已提交
281 282 283
            // `getLabelInterval` is provided by echarts/component/axis
            if (categoryAxis && categoryAxis.getLabelInterval) {
                var labelInterval = categoryAxis.getLabelInterval();
L
lang 已提交
284 285
                return function (idx) {
                    return (typeof labelInterval === 'function')
L
tweak  
lang 已提交
286
                            && !labelInterval(idx, categoryAxis.scale.getLabel(idx))
L
lang 已提交
287 288
                            || idx % (labelInterval + 1);
                };
L
lang 已提交
289 290 291 292 293 294
            }
        },

        /**
         * @private
         */
L
tweak  
lang 已提交
295
        // FIXME Two value axis
L
lang 已提交
296
        _updateAnimation: function (data, stackedOnPoints, coordSys, api) {
L
lang 已提交
297
            var polyline = this._polyline;
L
lang 已提交
298 299
            var polygon = this._polygon;

L
lang 已提交
300
            var diff = lineAnimationDiff(
L
lang 已提交
301 302
                this._data, data,
                this._stackedOnPoints, stackedOnPoints,
L
lang 已提交
303
                this._coordSys, coordSys
L
lang 已提交
304
            );
L
lang 已提交
305
            polyline.shape.points = diff.current;
L
lang 已提交
306 307

            api.updateGraphicEl(polyline, {
L
lang 已提交
308 309 310
                shape: {
                    points: diff.next
                }
L
lang 已提交
311
            });
L
lang 已提交
312

L
lang 已提交
313
            if (polygon) {
L
lang 已提交
314 315 316 317 318
                polygon.setShape({
                    points: diff.current,
                    stackedOnPoints: diff.stackedOnCurrent
                });
                api.updateGraphicEl(polygon, {
L
lang 已提交
319 320 321 322
                    shape: {
                        points: diff.next,
                        stackedOnPoints: diff.stackedOnNext
                    }
L
lang 已提交
323
                });
L
lang 已提交
324 325
            }

L
lang 已提交
326
            var updatedDataInfo = [];
L
lang 已提交
327 328 329
            var diffStatus = diff.status;

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

L
tweak  
lang 已提交
342
            if (polyline.animators && polyline.animators.length) {
L
lang 已提交
343
                polyline.animators[0].during(function () {
L
lang 已提交
344 345
                    for (var i = 0; i < updatedDataInfo.length; i++) {
                        var el = updatedDataInfo[i].el;
L
tweak  
lang 已提交
346
                        el.attr('position', polyline.shape.points[updatedDataInfo[i].ptIdx]);
L
lang 已提交
347 348 349
                    }
                });
            }
L
lang 已提交
350 351
        },

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

L
tweak  
lang 已提交
358
        _createGridClipShape: function (cartesian, hasAnimation, api) {
L
lang 已提交
359 360
            var xExtent = getAxisExtentWithGap(cartesian.getAxis('x'));
            var yExtent = getAxisExtentWithGap(cartesian.getAxis('y'));
L
lang 已提交
361

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

L
tweak  
lang 已提交
371
            if (hasAnimation) {
L
lang 已提交
372
                clipPath.shape[cartesian.getBaseAxis().isHorizontal() ? 'width' : 'height'] = 0;
L
tweak  
lang 已提交
373
                api.initGraphicEl(clipPath, {
L
lang 已提交
374 375 376 377
                    shape: {
                        width: xExtent[1] - xExtent[0],
                        height: yExtent[1] - yExtent[0]
                    }
L
tweak  
lang 已提交
378
                });
L
lang 已提交
379
            }
L
lang 已提交
380 381 382 383

            return clipPath;
        },

L
tweak  
lang 已提交
384
        _createPolarClipShape: function (polar, hasAnimation, api) {
385
            var angleAxis = polar.getAngleAxis();
L
lang 已提交
386 387 388
            var radiusAxis = polar.getRadiusAxis();

            var radiusExtent = radiusAxis.getExtent();
389
            var angleExtent = angleAxis.getExtent();
L
lang 已提交
390

391
            var RADIAN = Math.PI / 180;
L
lang 已提交
392

393
            var clipPath = new graphic.Sector({
L
lang 已提交
394 395 396 397 398
                shape: {
                    cx: polar.cx,
                    cy: polar.cy,
                    r0: radiusExtent[0],
                    r: radiusExtent[1],
399 400 401
                    startAngle: -angleExtent[0] * RADIAN,
                    endAngle: -angleExtent[1] * RADIAN,
                    clockwise: angleAxis.inverse
L
lang 已提交
402 403 404
                }
            });

L
tweak  
lang 已提交
405
            if (hasAnimation) {
406
                clipPath.shape.endAngle = -angleExtent[0] * RADIAN;
L
tweak  
lang 已提交
407
                api.initGraphicEl(clipPath, {
408
                    shape: {
409
                        endAngle: -angleExtent[1] * RADIAN
410
                    }
L
tweak  
lang 已提交
411
                });
412
            }
L
lang 已提交
413 414

            return clipPath;
L
lang 已提交
415 416
        },

L
lang 已提交
417
        remove: function (ecModel, api) {
L
lang 已提交
418
            var group = this.group;
L
tweak  
lang 已提交
419
            group.remove(this._lineGroup);
L
lang 已提交
420
            this._symbolDraw.remove(api, true);
L
lang 已提交
421 422 423
        }
    });
});