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

    var polyHelper = require('./poly');
12

L
lang 已提交
13 14
    var ChartView = require('../../view/Chart');

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

L
lang 已提交
29
    function getSmooth(smooth) {
30
        return typeof (smooth) === 'number' ? smooth : (smooth ? 0.3 : 0);
L
lang 已提交
31 32
    }

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

        var valueDim = valueAxis.dim;
L
lang 已提交
61 62

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

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

L
lang 已提交
83 84 85 86 87 88 89 90 91
    function queryDataIndex(data, payload) {
        if (payload.dataIndex != null) {
            return payload.dataIndex;
        }
        else if (payload.name != null) {
            return data.indexOfName(payload.name);
        }
    }

92 93 94
    function createGridClipShape(cartesian, hasAnimation, seriesModel) {
        var xExtent = getAxisExtentWithGap(cartesian.getAxis('x'));
        var yExtent = getAxisExtentWithGap(cartesian.getAxis('y'));
L
lang 已提交
95 96 97 98 99 100 101
        var isHorizontal = cartesian.getBaseAxis().isHorizontal();

        var x = xExtent[0];
        var y = yExtent[0];
        var width = xExtent[1] - x;
        var height = yExtent[1] - y;
        // Expand clip shape to avoid line value exceeds axis
L
lang 已提交
102 103 104 105 106 107 108 109 110
        if (!seriesModel.get('clipOverflow')) {
            if (isHorizontal) {
                y -= height;
                height *= 3;
            }
            else {
                x -= width;
                width *= 3;
            }
L
lang 已提交
111
        }
112 113
        var clipPath = new graphic.Rect({
            shape: {
L
lang 已提交
114 115 116 117
                x: x,
                y: y,
                width: width,
                height: height
118 119 120 121
            }
        });

        if (hasAnimation) {
L
lang 已提交
122
            clipPath.shape[isHorizontal ? 'width' : 'height'] = 0;
123 124
            graphic.initProps(clipPath, {
                shape: {
L
lang 已提交
125 126
                    width: width,
                    height: height
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                }
            }, seriesModel);
        }

        return clipPath;
    }

    function createPolarClipShape(polar, hasAnimation, seriesModel) {
        var angleAxis = polar.getAngleAxis();
        var radiusAxis = polar.getRadiusAxis();

        var radiusExtent = radiusAxis.getExtent();
        var angleExtent = angleAxis.getExtent();

        var RADIAN = Math.PI / 180;

        var clipPath = new graphic.Sector({
            shape: {
                cx: polar.cx,
                cy: polar.cy,
                r0: radiusExtent[0],
                r: radiusExtent[1],
                startAngle: -angleExtent[0] * RADIAN,
                endAngle: -angleExtent[1] * RADIAN,
                clockwise: angleAxis.inverse
            }
        });

        if (hasAnimation) {
            clipPath.shape.endAngle = -angleExtent[0] * RADIAN;
            graphic.initProps(clipPath, {
                shape: {
                    endAngle: -angleExtent[1] * RADIAN
                }
            }, seriesModel);
        }

        return clipPath;
    }

    function createClipShape(coordSys, hasAnimation, seriesModel) {
        return coordSys.type === 'polar'
            ? createPolarClipShape(coordSys, hasAnimation, seriesModel)
            : createGridClipShape(coordSys, hasAnimation, seriesModel);
    }

L
lang 已提交
173
    return ChartView.extend({
L
lang 已提交
174 175 176

        type: 'line',

L
lang 已提交
177
        init: function () {
L
tweak  
lang 已提交
178 179
            var lineGroup = new graphic.Group();

L
lang 已提交
180 181
            var symbolDraw = new SymbolDraw();
            this.group.add(symbolDraw.group);
L
tweak  
lang 已提交
182

L
lang 已提交
183
            this._symbolDraw = symbolDraw;
L
tweak  
lang 已提交
184
            this._lineGroup = lineGroup;
L
lang 已提交
185
        },
L
lang 已提交
186

L
lang 已提交
187
        render: function (seriesModel, ecModel, api) {
L
lang 已提交
188
            var coordSys = seriesModel.coordinateSystem;
L
lang 已提交
189
            var group = this.group;
L
lang 已提交
190
            var data = seriesModel.getData();
191 192
            var lineStyleModel = seriesModel.getModel('lineStyle.normal');
            var areaStyleModel = seriesModel.getModel('areaStyle.normal');
L
lang 已提交
193

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

L
lang 已提交
196
            var isCoordSysPolar = coordSys.type === 'polar';
L
lang 已提交
197
            var prevCoordSys = this._coordSys;
L
lang 已提交
198

L
lang 已提交
199
            var symbolDraw = this._symbolDraw;
L
lang 已提交
200
            var polyline = this._polyline;
L
lang 已提交
201 202
            var polygon = this._polygon;

L
tweak  
lang 已提交
203 204
            var lineGroup = this._lineGroup;

L
tweak  
lang 已提交
205
            var hasAnimation = seriesModel.get('animation');
L
lang 已提交
206 207

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

L
lang 已提交
210
            var showSymbol = seriesModel.get('showSymbol');
211 212

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

215 216 217 218 219 220 221 222 223
            // Remove temporary symbols
            var oldData = this._data;
            oldData && oldData.eachItemGraphicEl(function (el, idx) {
                if (el.__temp) {
                    group.remove(el);
                    oldData.setItemGraphicEl(idx, null);
                }
            });

L
lang 已提交
224
            // Remove previous created symbols if showSymbol changed to false
225 226 227 228
            if (!showSymbol) {
                symbolDraw.remove();
            }

229 230
            group.add(lineGroup);

L
lang 已提交
231
            // Initialization animation or coordinate system changed
L
lang 已提交
232
            if (
P
pah100 已提交
233
                !(polyline && prevCoordSys.type === coordSys.type)
L
lang 已提交
234
            ) {
235 236
                showSymbol && symbolDraw.updateData(data, isSymbolIgnore);

L
lang 已提交
237
                polyline = this._newPolyline(points, coordSys, hasAnimation);
L
lang 已提交
238 239
                if (isAreaChart) {
                    polygon = this._newPolygon(
L
lang 已提交
240
                        points, stackedOnPoints,
L
lang 已提交
241 242
                        coordSys, hasAnimation
                    );
L
lang 已提交
243
                }
244
                lineGroup.setClipPath(createClipShape(coordSys, true, seriesModel));
L
lang 已提交
245 246
            }
            else {
L
lang 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259
                if (isAreaChart && !polygon) {
                    // If areaStyle is added
                    polygon = this._newPolygon(
                        points, stackedOnPoints,
                        coordSys, hasAnimation
                    );
                }
                else if (polygon && !isAreaChart) {
                    // If areaStyle is removed
                    lineGroup.remove(polygon);
                    polygon = this._polygon = null;
                }

L
lang 已提交
260
                // Update clipPath
P
pah100 已提交
261
                lineGroup.setClipPath(createClipShape(coordSys, false, seriesModel));
L
lang 已提交
262

263 264 265
                // Always update, or it is wrong in the case turning on legend
                // because points are not changed
                showSymbol && symbolDraw.updateData(data, isSymbolIgnore);
L
tweak  
lang 已提交
266

L
tweak  
lang 已提交
267 268 269
                // Stop symbol animation and sync with line points
                // FIXME performance?
                data.eachItemGraphicEl(function (el) {
270
                    el.stopAnimation(true);
L
tweak  
lang 已提交
271 272
                });

273 274
                // 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 已提交
275 276 277
                if (!isPointsSame(this._stackedOnPoints, stackedOnPoints)
                    || !isPointsSame(this._points, points)
                ) {
L
lang 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291
                    if (hasAnimation) {
                        this._updateAnimation(
                            data, stackedOnPoints, coordSys, api
                        );
                    }
                    else {
                        polyline.setShape({
                            points: points
                        });
                        polygon && polygon.setShape({
                            points: points,
                            stackedOnPoints: stackedOnPoints
                        });
                    }
L
lang 已提交
292
                }
L
lang 已提交
293 294
            }

295
            polyline.useStyle(zrUtil.defaults(
296
                // Use color in lineStyle first
L
lang 已提交
297
                lineStyleModel.getLineStyle(),
L
lang 已提交
298
                {
299
                    fill: 'none',
L
lang 已提交
300 301 302 303
                    stroke: data.getVisual('color'),
                    lineJoin: 'bevel'
                }
            ));
L
lang 已提交
304 305 306

            var smooth = seriesModel.get('smooth');
            smooth = getSmooth(seriesModel.get('smooth'));
307 308
            polyline.setShape({
                smooth: smooth,
L
lang 已提交
309 310
                smoothMonotone: seriesModel.get('smoothMonotone'),
                connectNulls: seriesModel.get('connectNulls')
311
            });
L
lang 已提交
312

L
lang 已提交
313
            if (polygon) {
L
lang 已提交
314 315 316
                var stackedOn = data.stackedOn;
                var stackedOnSmooth = 0;

317
                polygon.useStyle(zrUtil.defaults(
L
lang 已提交
318 319 320
                    areaStyleModel.getAreaStyle(),
                    {
                        fill: data.getVisual('color'),
321
                        opacity: 0.7,
L
lang 已提交
322 323 324
                        lineJoin: 'bevel'
                    }
                ));
L
lang 已提交
325 326 327

                if (stackedOn) {
                    var stackedOnSeries = stackedOn.hostModel;
L
lang 已提交
328
                    stackedOnSmooth = getSmooth(stackedOnSeries.get('smooth'));
L
lang 已提交
329 330
                }

331 332 333
                polygon.setShape({
                    smooth: smooth,
                    stackedOnSmooth: stackedOnSmooth,
L
lang 已提交
334 335
                    smoothMonotone: seriesModel.get('smoothMonotone'),
                    connectNulls: seriesModel.get('connectNulls')
336
                });
L
lang 已提交
337
            }
L
lang 已提交
338

L
lang 已提交
339
            this._data = data;
L
lang 已提交
340
            // Save the coordinate system for transition animation when data changed
L
lang 已提交
341
            this._coordSys = coordSys;
L
lang 已提交
342
            this._stackedOnPoints = stackedOnPoints;
L
lang 已提交
343
            this._points = points;
344 345
        },

L
lang 已提交
346 347 348 349
        highlight: function (seriesModel, ecModel, api, payload) {
            var data = seriesModel.getData();
            var dataIndex = queryDataIndex(data, payload);

350
            if (dataIndex != null && dataIndex >= 0) {
L
lang 已提交
351 352 353
                var symbol = data.getItemGraphicEl(dataIndex);
                if (!symbol) {
                    // Create a temporary symbol if it is not exists
L
Typo  
lang 已提交
354
                    var pt = data.getItemLayout(dataIndex);
L
lang 已提交
355
                    symbol = new Symbol(data, dataIndex, api);
L
Typo  
lang 已提交
356
                    symbol.position = pt;
L
lang 已提交
357 358 359 360
                    symbol.setZ(
                        seriesModel.get('zlevel'),
                        seriesModel.get('z')
                    );
L
Typo  
lang 已提交
361
                    symbol.ignore = isNaN(pt[0]) || isNaN(pt[1]);
L
lang 已提交
362 363 364
                    symbol.__temp = true;
                    data.setItemGraphicEl(dataIndex, symbol);

L
lang 已提交
365
                    // Stop scale animation
366
                    symbol.stopSymbolAnimation(true);
367

L
lang 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
                    this.group.add(symbol);
                }
                symbol.highlight();
            }
            else {
                // Highlight whole series
                ChartView.prototype.highlight.call(
                    this, seriesModel, ecModel, api, payload
                );
            }
        },

        downplay: function (seriesModel, ecModel, api, payload) {
            var data = seriesModel.getData();
            var dataIndex = queryDataIndex(data, payload);
383
            if (dataIndex != null && dataIndex >= 0) {
L
lang 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
                var symbol = data.getItemGraphicEl(dataIndex);
                if (symbol) {
                    if (symbol.__temp) {
                        data.setItemGraphicEl(dataIndex, null);
                        this.group.remove(symbol);
                    }
                    else {
                        symbol.downplay();
                    }
                }
            }
            else {
                // Downplay whole series
                ChartView.prototype.downplay.call(
                    this, seriesModel, ecModel, api, payload
                );
            }
        },

L
lang 已提交
403 404 405 406 407
        /**
         * @param {module:zrender/container/Group} group
         * @param {Array.<Array.<number>>} points
         * @private
         */
L
lang 已提交
408
        _newPolyline: function (points) {
L
lang 已提交
409 410 411
            var polyline = this._polyline;
            // Remove previous created polyline
            if (polyline) {
L
lang 已提交
412
                this._lineGroup.remove(polyline);
L
lang 已提交
413 414
            }

L
lang 已提交
415
            polyline = new polyHelper.Polyline({
L
lang 已提交
416 417 418
                shape: {
                    points: points
                },
L
lang 已提交
419 420
                silent: true,
                z2: 10
L
lang 已提交
421 422
            });

L
tweak  
lang 已提交
423
            this._lineGroup.add(polyline);
L
lang 已提交
424 425 426 427 428 429 430 431 432 433 434 435

            this._polyline = polyline;

            return polyline;
        },

        /**
         * @param {module:zrender/container/Group} group
         * @param {Array.<Array.<number>>} stackedOnPoints
         * @param {Array.<Array.<number>>} points
         * @private
         */
L
lang 已提交
436
        _newPolygon: function (points, stackedOnPoints) {
L
lang 已提交
437 438 439
            var polygon = this._polygon;
            // Remove previous created polygon
            if (polygon) {
L
lang 已提交
440
                this._lineGroup.remove(polygon);
L
lang 已提交
441 442
            }

L
lang 已提交
443
            polygon = new polyHelper.Polygon({
L
lang 已提交
444 445 446 447 448 449 450
                shape: {
                    points: points,
                    stackedOnPoints: stackedOnPoints
                },
                silent: true
            });

L
tweak  
lang 已提交
451
            this._lineGroup.add(polygon);
L
lang 已提交
452 453 454 455

            this._polygon = polygon;
            return polygon;
        },
L
lang 已提交
456 457 458
        /**
         * @private
         */
L
lang 已提交
459
        _getSymbolIgnoreFunc: function (data, coordSys) {
L
lang 已提交
460
            var categoryAxis = coordSys.getAxesByScale('ordinal')[0];
L
lang 已提交
461
            // `getLabelInterval` is provided by echarts/component/axis
L
lang 已提交
462 463
            if (categoryAxis && categoryAxis.isLabelIgnored) {
                return zrUtil.bind(categoryAxis.isLabelIgnored, categoryAxis);
L
lang 已提交
464 465 466 467 468 469
            }
        },

        /**
         * @private
         */
L
tweak  
lang 已提交
470
        // FIXME Two value axis
L
lang 已提交
471
        _updateAnimation: function (data, stackedOnPoints, coordSys, api) {
L
lang 已提交
472
            var polyline = this._polyline;
L
lang 已提交
473
            var polygon = this._polygon;
L
lang 已提交
474
            var seriesModel = data.hostModel;
L
lang 已提交
475

L
lang 已提交
476
            var diff = lineAnimationDiff(
L
lang 已提交
477 478
                this._data, data,
                this._stackedOnPoints, stackedOnPoints,
L
lang 已提交
479
                this._coordSys, coordSys
L
lang 已提交
480
            );
L
lang 已提交
481
            polyline.shape.points = diff.current;
L
lang 已提交
482

L
lang 已提交
483
            graphic.updateProps(polyline, {
L
lang 已提交
484 485 486
                shape: {
                    points: diff.next
                }
L
lang 已提交
487
            }, seriesModel);
L
lang 已提交
488

L
lang 已提交
489
            if (polygon) {
L
lang 已提交
490 491 492 493
                polygon.setShape({
                    points: diff.current,
                    stackedOnPoints: diff.stackedOnCurrent
                });
L
lang 已提交
494
                graphic.updateProps(polygon, {
L
lang 已提交
495 496 497 498
                    shape: {
                        points: diff.next,
                        stackedOnPoints: diff.stackedOnNext
                    }
L
lang 已提交
499
                }, seriesModel);
L
lang 已提交
500 501
            }

L
lang 已提交
502
            var updatedDataInfo = [];
L
lang 已提交
503 504 505
            var diffStatus = diff.status;

            for (var i = 0; i < diffStatus.length; i++) {
L
lang 已提交
506 507
                var cmd = diffStatus[i].cmd;
                if (cmd === '=') {
L
lang 已提交
508 509 510 511 512 513 514
                    var el = data.getItemGraphicEl(diffStatus[i].idx1);
                    if (el) {
                        updatedDataInfo.push({
                            el: el,
                            ptIdx: i    // Index of points
                        });
                    }
L
lang 已提交
515
                }
L
lang 已提交
516 517
            }

L
tweak  
lang 已提交
518
            if (polyline.animators && polyline.animators.length) {
L
lang 已提交
519
                polyline.animators[0].during(function () {
L
lang 已提交
520 521
                    for (var i = 0; i < updatedDataInfo.length; i++) {
                        var el = updatedDataInfo[i].el;
L
tweak  
lang 已提交
522
                        el.attr('position', polyline.shape.points[updatedDataInfo[i].ptIdx]);
L
lang 已提交
523 524 525
                    }
                });
            }
L
lang 已提交
526 527
        },

528
        remove: function (ecModel) {
529 530
            var group = this.group;
            var oldData = this._data;
531
            this._lineGroup.removeAll();
532
            this._symbolDraw.remove(true);
533 534 535 536 537 538 539
            // Remove temporary created elements when highlighting
            oldData && oldData.eachItemGraphicEl(function (el, idx) {
                if (el.__temp) {
                    group.remove(el);
                    oldData.setItemGraphicEl(idx, null);
                }
            });
540 541 542 543 544 545 546

            this._polyline =
            this._polygon =
            this._coordSys =
            this._points =
            this._stackedOnPoints =
            this._data = null;
L
lang 已提交
547 548 549
        }
    });
});