LineView.js 14.9 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 SymbolDraw = require('../helper/SymbolDraw');
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
    function getSmooth(smooth) {
28
        return typeof (smooth) === 'number' ? smooth : (smooth ? 0.3 : 0);
L
lang 已提交
29 30
    }

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 symbolDraw = new SymbolDraw();
            this.group.add(symbolDraw.group);
            this._symbolDraw = symbolDraw;
L
lang 已提交
89
        },
L
lang 已提交
90

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

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 symbolDraw = this._symbolDraw;
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
                symbolDraw.updateData(
L
lang 已提交
123
                    data, seriesModel, api, hasAnimation, symbolIgnoreMap
L
lang 已提交
124
                );
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
                symbolDraw.updateData(
L
lang 已提交
138
                    data, seriesModel, api, false, symbolIgnoreMap
L
lang 已提交
139
                );
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 156 157 158 159 160 161 162 163 164 165 166 167 168
                    if (hasAnimation) {
                        this._updateAnimation(
                            data, stackedOnPoints, coordSys, api
                        );
                    }
                    else {
                        polyline.setShape({
                            points: points
                        });
                        polygon && polygon.setShape({
                            points: points,
                            stackedOnPoints: stackedOnPoints
                        });
                    }
L
lang 已提交
169
                }
L
lang 已提交
170
                // Add back
L
lang 已提交
171
                group.add(polyline);
L
lang 已提交
172
                group.add(polygon);
L
lang 已提交
173 174
            }

L
lang 已提交
175
            polyline.setStyle(zrUtil.extend(
L
lang 已提交
176
                lineStyleModel.getLineStyle(),
L
lang 已提交
177 178 179 180 181
                {
                    stroke: data.getVisual('color'),
                    lineJoin: 'bevel'
                }
            ));
L
lang 已提交
182 183 184 185 186

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

L
lang 已提交
187
            if (polygon) {
L
lang 已提交
188 189 190 191
                var polygonShape = polygon.shape;
                var stackedOn = data.stackedOn;
                var stackedOnSmooth = 0;

L
lang 已提交
192
                polygon.style.opacity = 0.7;
L
lang 已提交
193
                polygon.setStyle(zrUtil.defaults(
L
lang 已提交
194 195 196 197 198 199
                    areaStyleModel.getAreaStyle(),
                    {
                        fill: data.getVisual('color'),
                        lineJoin: 'bevel'
                    }
                ));
L
lang 已提交
200 201 202 203
                polygonShape.smooth = smooth;

                if (stackedOn) {
                    var stackedOnSeries = stackedOn.hostModel;
L
lang 已提交
204
                    stackedOnSmooth = getSmooth(stackedOnSeries.get('smooth'));
L
lang 已提交
205 206 207
                }

                polygonShape.stackedOnSmooth = stackedOnSmooth;
L
lang 已提交
208
            }
L
lang 已提交
209

L
lang 已提交
210
            this._data = data;
211

L
lang 已提交
212
            // Save the coordinate system for transition animation when data changed
L
lang 已提交
213
            this._coordSys = coordSys;
L
lang 已提交
214
            this._stackedOnPoints = stackedOnPoints;
L
lang 已提交
215
            this._points = points;
216 217
        },

L
lang 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231
        /**
         * @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 已提交
232
            polyline = new polyHelper.Polyline({
L
lang 已提交
233 234 235
                shape: {
                    points: points
                },
L
lang 已提交
236 237
                silent: true,
                z2: 10
L
lang 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
            });

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

        /**
         * @private
         */
L
lang 已提交
303
        _updateAnimation: function (data, stackedOnPoints, coordSys, api) {
L
lang 已提交
304
            var polyline = this._polyline;
L
lang 已提交
305 306
            var polygon = this._polygon;

L
lang 已提交
307
            var diff = lineAnimationDiff(
L
lang 已提交
308 309
                this._data, data,
                this._stackedOnPoints, stackedOnPoints,
L
lang 已提交
310
                this._coordSys, coordSys
L
lang 已提交
311
            );
L
lang 已提交
312
            polyline.shape.points = diff.current;
L
lang 已提交
313 314

            api.updateGraphicEl(polyline, {
L
lang 已提交
315 316 317
                shape: {
                    points: diff.next
                }
L
lang 已提交
318
            });
L
lang 已提交
319

L
lang 已提交
320
            if (polygon) {
L
lang 已提交
321 322 323 324 325
                polygon.setShape({
                    points: diff.current,
                    stackedOnPoints: diff.stackedOnCurrent
                });
                api.updateGraphicEl(polygon, {
L
lang 已提交
326 327 328 329
                    shape: {
                        points: diff.next,
                        stackedOnPoints: diff.stackedOnNext
                    }
L
lang 已提交
330
                });
L
lang 已提交
331 332
            }

L
lang 已提交
333 334
            var updatedDataInfo = [];
            var addedDataIndices = [];
L
lang 已提交
335 336 337
            var diffStatus = diff.status;

            for (var i = 0; i < diffStatus.length; i++) {
L
lang 已提交
338 339
                var cmd = diffStatus[i].cmd;
                if (cmd === '=') {
L
lang 已提交
340 341 342 343 344 345 346
                    var el = data.getItemGraphicEl(diffStatus[i].idx1);
                    if (el) {
                        updatedDataInfo.push({
                            el: el,
                            ptIdx: i    // Index of points
                        });
                    }
L
lang 已提交
347 348 349
                }
                else if (cmd === '+') {
                    addedDataIndices.push(diffStatus[i].idx);
L
lang 已提交
350 351 352 353
                }
            }

            if (polyline.animators) {
L
lang 已提交
354 355
                for (var i = 0; i < addedDataIndices.length; i++) {
                    var el = data.getItemGraphicEl(addedDataIndices[i]);
L
lang 已提交
356
                    if (el) {
L
lang 已提交
357
                        el.scale = [0, 0];
L
lang 已提交
358
                        el.animateTo({
L
lang 已提交
359
                            scale: [1, 1]
L
lang 已提交
360 361
                        }, 300, 300, 'cubicOut');
                    }
L
lang 已提交
362
                }
L
lang 已提交
363
                polyline.animators[0].during(function () {
L
lang 已提交
364 365
                    for (var i = 0; i < updatedDataInfo.length; i++) {
                        var el = updatedDataInfo[i].el;
L
lang 已提交
366
                        vector.copy(
L
lang 已提交
367 368
                            el.position,
                            // synchronizing with the point on line
L
lang 已提交
369
                            polyline.shape.points[updatedDataInfo[i].ptIdx]
L
lang 已提交
370
                        );
L
lang 已提交
371
                        el.dirty();
L
lang 已提交
372 373 374
                    }
                });
            }
L
lang 已提交
375 376
        },

L
lang 已提交
377 378 379 380 381 382 383
        _createClipShape: function (coordSys, hasAnimation) {
            return coordSys.type === 'polar'
                ? this._createPolarClipShape(coordSys, hasAnimation)
                : this._createGridClipShape(coordSys, hasAnimation);
        },

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

387
            var clipPath = new graphic.Rect({
L
lang 已提交
388 389 390
                shape: {
                    x: xExtent[0],
                    y: yExtent[0],
L
lang 已提交
391
                    width: xExtent[1] - xExtent[0],
L
lang 已提交
392 393 394 395
                    height: yExtent[1] - yExtent[0]
                }
            });

L
lang 已提交
396
            if (animation) {
L
lang 已提交
397
                clipPath.shape[cartesian.getBaseAxis().isHorizontal() ? 'width' : 'height'] = 0;
L
lang 已提交
398 399 400 401 402
                clipPath.animateTo({
                    shape: {
                        width: xExtent[1] - xExtent[0],
                        height: yExtent[1] - yExtent[0]
                    }
L
lang 已提交
403
                }, 1500);
L
lang 已提交
404
            }
L
lang 已提交
405 406 407 408

            return clipPath;
        },

409
        _createPolarClipShape: function (polar, animation) {
L
lang 已提交
410 411 412 413 414
            // var angleAxis = polar.getAngleAxis();
            var radiusAxis = polar.getRadiusAxis();

            var radiusExtent = radiusAxis.getExtent();

L
lang 已提交
415 416
            var PI2 = Math.PI * 2;

417
            var clipPath = new graphic.Sector({
L
lang 已提交
418 419 420 421 422 423
                shape: {
                    cx: polar.cx,
                    cy: polar.cy,
                    r0: radiusExtent[0],
                    r: radiusExtent[1],
                    startAngle: 0,
L
lang 已提交
424
                    endAngle: PI2
L
lang 已提交
425 426 427
                }
            });

428 429 430 431
            if (animation) {
                clipPath.shape.endAngle = 0;
                clipPath.animateTo({
                    shape: {
L
lang 已提交
432
                        endAngle: PI2
433 434 435
                    }
                }, 1500, animation);
            }
L
lang 已提交
436 437

            return clipPath;
L
lang 已提交
438 439
        },

L
lang 已提交
440
        remove: function (ecModel) {
L
lang 已提交
441 442 443
            var group = this.group;
            group.remove(this._polyline);
            group.remove(this._polygon);
L
lang 已提交
444
            this._symbolDraw.remove(ecModel.get('animation'));
L
lang 已提交
445 446 447
        }
    });
});