LineView.ts 29.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

L
lang 已提交
20
// FIXME step not support polar
L
lang 已提交
21

S
sushuang 已提交
22
import {__DEV__} from '../../config';
S
sushuang 已提交
23
import * as zrUtil from 'zrender/src/core/util';
24
import {fromPoints} from 'zrender/src/core/bbox';
S
sushuang 已提交
25 26 27
import SymbolDraw from '../helper/SymbolDraw';
import SymbolClz from '../helper/Symbol';
import lineAnimationDiff from './lineAnimationDiff';
S
sushuang 已提交
28 29
import * as graphic from '../../util/graphic';
import * as modelUtil from '../../util/model';
P
pissang 已提交
30
import {ECPolyline, ECPolygon} from './poly';
S
sushuang 已提交
31
import ChartView from '../../view/Chart';
S
sushuang 已提交
32
import {prepareDataCoordInfo, getStackedOnPoint} from './helper';
33
import {createGridClipPath, createPolarClipPath} from '../helper/createClipPathFromCoordSys';
P
pissang 已提交
34 35 36 37 38 39 40
import LineSeriesModel, { LineSeriesOption } from './LineSeries';
import type GlobalModel from '../../model/Global';
import type ExtensionAPI from '../../ExtensionAPI';
// TODO
import Cartesian2D from '../../coord/cartesian/Cartesian2D';
import Polar from '../../coord/polar/Polar';
import type List from '../../data/List';
41
import type { Payload, Dictionary, ColorString, ECElement, DisplayState } from '../../util/types';
P
pissang 已提交
42 43
import type OrdinalScale from '../../scale/Ordinal';
import type Axis2D from '../../coord/cartesian/Axis2D';
44
import { CoordinateSystemClipArea } from '../../coord/CoordinateSystem';
45
import { setStatesStylesFromModel, setStatesFlag, enableHoverEmphasis } from '../../util/states';
P
pissang 已提交
46 47


1
100pah 已提交
48 49
type PolarArea = ReturnType<Polar['getArea']>;
type Cartesian2DArea = ReturnType<Cartesian2D['getArea']>;
P
pissang 已提交
50 51 52 53

interface SymbolExtended extends SymbolClz {
    __temp: boolean
}
S
sushuang 已提交
54

P
pissang 已提交
55
function isPointsSame(points1: number[][], points2: number[][]) {
S
sushuang 已提交
56 57 58
    if (points1.length !== points2.length) {
        return;
    }
59
    for (let i = 0; i < points1.length; i++) {
60 61
        const p1 = points1[i];
        const p2 = points2[i];
S
sushuang 已提交
62
        if (p1[0] !== p2[0] || p1[1] !== p2[1]) {
63 64 65
            return;
        }
    }
S
sushuang 已提交
66 67 68
    return true;
}

69 70 71
function getBoundingDiff(points1: number[][], points2: number[][]): number {
    const min1 = [] as number[];
    const max1 = [] as number[];
72

73 74
    const min2 = [] as number[];
    const max2 = [] as number[];
75 76 77 78 79 80 81 82 83 84 85 86 87 88

    fromPoints(points1, min1, max1);
    fromPoints(points2, min2, max2);

    // Get a max value from each corner of two boundings.
    return Math.max(
        Math.abs(min1[0] - min2[0]),
        Math.abs(min1[1] - min2[1]),

        Math.abs(max1[0] - max2[0]),
        Math.abs(max1[1] - max2[1])
    );
}

P
pissang 已提交
89 90
function getSmooth(smooth: number | boolean) {
    return typeof smooth === 'number' ? smooth : (smooth ? 0.5 : 0);
S
sushuang 已提交
91 92
}

P
pissang 已提交
93 94 95 96 97
function getStackedOnPoints(
    coordSys: Cartesian2D | Polar,
    data: List,
    dataCoordInfo: ReturnType<typeof prepareDataCoordInfo>
) {
S
sushuang 已提交
98 99
    if (!dataCoordInfo.valueDim) {
        return [];
S
sushuang 已提交
100
    }
L
lang 已提交
101

102
    const points = [];
103
    for (let idx = 0, len = data.count(); idx < len; idx++) {
S
sushuang 已提交
104 105
        points.push(getStackedOnPoint(dataCoordInfo, coordSys, data, idx));
    }
S
sushuang 已提交
106

S
sushuang 已提交
107
    return points;
S
sushuang 已提交
108 109
}

P
pissang 已提交
110 111 112 113 114
function turnPointsIntoStep(
    points: number[][],
    coordSys: Cartesian2D | Polar,
    stepTurnAt: 'start' | 'end' | 'middle'
) {
115 116
    const baseAxis = coordSys.getBaseAxis();
    const baseIndex = baseAxis.dim === 'x' || baseAxis.dim === 'radius' ? 0 : 1;
S
sushuang 已提交
117

118
    const stepPoints = [];
119
    let i = 0;
1
100pah 已提交
120
    for (; i < points.length - 1; i++) {
121 122
        const nextPt = points[i + 1];
        const pt = points[i];
S
sushuang 已提交
123 124
        stepPoints.push(pt);

125
        const stepPt = [];
S
sushuang 已提交
126 127 128 129 130 131 132 133 134
        switch (stepTurnAt) {
            case 'end':
                stepPt[baseIndex] = nextPt[baseIndex];
                stepPt[1 - baseIndex] = pt[1 - baseIndex];
                // default is start
                stepPoints.push(stepPt);
                break;
            case 'middle':
                // default is start
135 136
                const middle = (pt[baseIndex] + nextPt[baseIndex]) / 2;
                const stepPt2 = [];
S
sushuang 已提交
137 138 139 140 141 142 143 144 145 146 147
                stepPt[baseIndex] = stepPt2[baseIndex] = middle;
                stepPt[1 - baseIndex] = pt[1 - baseIndex];
                stepPt2[1 - baseIndex] = nextPt[1 - baseIndex];
                stepPoints.push(stepPt);
                stepPoints.push(stepPt2);
                break;
            default:
                stepPt[baseIndex] = pt[baseIndex];
                stepPt[1 - baseIndex] = nextPt[1 - baseIndex];
                // default is start
                stepPoints.push(stepPt);
148 149
        }
    }
S
sushuang 已提交
150 151 152 153 154
    // Last points
    points[i] && stepPoints.push(points[i]);
    return stepPoints;
}

P
pissang 已提交
155 156 157 158
function getVisualGradient(
    data: List,
    coordSys: Cartesian2D | Polar
) {
159
    const visualMetaList = data.getVisual('visualMeta');
S
sushuang 已提交
160 161 162
    if (!visualMetaList || !visualMetaList.length || !data.count()) {
        // When data.count() is 0, gradient range can not be calculated.
        return;
163 164
    }

S
sushuang 已提交
165 166 167 168 169 170 171
    if (coordSys.type !== 'cartesian2d') {
        if (__DEV__) {
            console.warn('Visual map on line style is only supported on cartesian2d.');
        }
        return;
    }

172 173
    let coordDim: 'x' | 'y';
    let visualMeta;
S
sushuang 已提交
174

175
    for (let i = visualMetaList.length - 1; i >= 0; i--) {
176 177 178
        const dimIndex = visualMetaList[i].dimension;
        const dimName = data.dimensions[dimIndex];
        const dimInfo = data.getDimensionInfo(dimName);
P
pissang 已提交
179
        coordDim = (dimInfo && dimInfo.coordDim) as 'x' | 'y';
S
sushuang 已提交
180
        // Can only be x or y
S
sushuang 已提交
181
        if (coordDim === 'x' || coordDim === 'y') {
S
sushuang 已提交
182 183
            visualMeta = visualMetaList[i];
            break;
L
lang 已提交
184 185
        }
    }
S
sushuang 已提交
186 187

    if (!visualMeta) {
S
sushuang 已提交
188 189
        if (__DEV__) {
            console.warn('Visual map on line style only support x or y dimension.');
190
        }
S
sushuang 已提交
191 192
        return;
    }
193

S
sushuang 已提交
194 195 196 197 198 199 200
    // If the area to be rendered is bigger than area defined by LinearGradient,
    // the canvas spec prescribes that the color of the first stop and the last
    // stop should be used. But if two stops are added at offset 0, in effect
    // browsers use the color of the second stop to render area outside
    // LinearGradient. So we can only infinitesimally extend area defined in
    // LinearGradient to render `outerColors`.

201
    const axis = coordSys.getAxis(coordDim);
S
sushuang 已提交
202

P
pissang 已提交
203 204 205 206 207
    interface ColorStop {
        offset: number
        coord?: number
        color: ColorString
    }
S
sushuang 已提交
208
    // dataToCoor mapping may not be linear, but must be monotonic.
209
    const colorStops: ColorStop[] = zrUtil.map(visualMeta.stops, function (stop) {
S
sushuang 已提交
210
        return {
P
pissang 已提交
211
            offset: 0,
S
sushuang 已提交
212 213 214 215
            coord: axis.toGlobalCoord(axis.dataToCoord(stop.value)),
            color: stop.color
        };
    });
216 217
    const stopLen = colorStops.length;
    const outerColors = visualMeta.outerColors.slice();
218

S
sushuang 已提交
219 220 221 222
    if (stopLen && colorStops[0].coord > colorStops[stopLen - 1].coord) {
        colorStops.reverse();
        outerColors.reverse();
    }
223

224 225 226 227
    const tinyExtent = 10; // Arbitrary value: 10px
    const minCoord = colorStops[0].coord - tinyExtent;
    const maxCoord = colorStops[stopLen - 1].coord + tinyExtent;
    const coordSpan = maxCoord - minCoord;
228

S
sushuang 已提交
229 230 231
    if (coordSpan < 1e-3) {
        return 'transparent';
    }
232

S
sushuang 已提交
233 234 235 236 237 238 239 240 241 242 243
    zrUtil.each(colorStops, function (stop) {
        stop.offset = (stop.coord - minCoord) / coordSpan;
    });
    colorStops.push({
        offset: stopLen ? colorStops[stopLen - 1].offset : 0.5,
        color: outerColors[1] || 'transparent'
    });
    colorStops.unshift({ // notice colorStops.length have been changed.
        offset: stopLen ? colorStops[0].offset : 0.5,
        color: outerColors[0] || 'transparent'
    });
244

S
sushuang 已提交
245 246 247 248
    // zrUtil.each(colorStops, function (colorStop) {
    //     // Make sure each offset has rounded px to avoid not sharp edge
    //     colorStop.offset = (Math.round(colorStop.offset * (end - start) + start) - start) / (end - start);
    // });
249

250
    const gradient = new graphic.LinearGradient(0, 0, 0, 0, colorStops, true);
S
sushuang 已提交
251
    gradient[coordDim] = minCoord;
P
pissang 已提交
252
    gradient[coordDim + '2' as 'x2' | 'y2'] = maxCoord;
L
Tweak  
lang 已提交
253

S
sushuang 已提交
254 255
    return gradient;
}
256

P
pissang 已提交
257 258 259 260 261
function getIsIgnoreFunc(
    seriesModel: LineSeriesModel,
    data: List,
    coordSys: Cartesian2D
) {
262 263
    const showAllSymbol = seriesModel.get('showAllSymbol');
    const isAuto = showAllSymbol === 'auto';
264 265 266 267 268

    if (showAllSymbol && !isAuto) {
        return;
    }

269
    const categoryAxis = coordSys.getAxesByScale('ordinal')[0];
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    if (!categoryAxis) {
        return;
    }

    // Note that category label interval strategy might bring some weird effect
    // in some scenario: users may wonder why some of the symbols are not
    // displayed. So we show all symbols as possible as we can.
    if (isAuto
        // Simplify the logic, do not determine label overlap here.
        && canShowAllSymbolForCategory(categoryAxis, data)
    ) {
        return;
    }

    // Otherwise follow the label interval strategy on category axis.
285 286
    const categoryDataDim = data.mapDimension(categoryAxis.dim);
    const labelMap: Dictionary<1> = {};
287 288 289 290 291

    zrUtil.each(categoryAxis.getViewLabels(), function (labelItem) {
        labelMap[labelItem.tickValue] = 1;
    });

P
pissang 已提交
292
    return function (dataIndex: number) {
293 294 295 296
        return !labelMap.hasOwnProperty(data.get(categoryDataDim, dataIndex));
    };
}

P
pissang 已提交
297 298 299 300
function canShowAllSymbolForCategory(
    categoryAxis: Axis2D,
    data: List
) {
301 302 303 304
    // In mose cases, line is monotonous on category axis, and the label size
    // is close with each other. So we check the symbol size and some of the
    // label size alone with the category axis to estimate whether all symbol
    // can be shown without overlap.
305
    const axisExtent = categoryAxis.getExtent();
306
    let availSize = Math.abs(axisExtent[1] - axisExtent[0]) / (categoryAxis.scale as OrdinalScale).count();
307 308 309
    isNaN(availSize) && (availSize = 0); // 0/0 is NaN.

    // Sampling some points, max 5.
310 311
    const dataLen = data.count();
    const step = Math.max(1, Math.round(dataLen / 5));
312
    for (let dataIndex = 0; dataIndex < dataLen; dataIndex += step) {
313 314 315 316 317 318 319 320 321 322 323 324 325 326
        if (SymbolClz.getSymbolSize(
                data, dataIndex
            // Only for cartesian, where `isHorizontal` exists.
            )[categoryAxis.isHorizontal() ? 1 : 0]
            // Empirical number
            * 1.5 > availSize
        ) {
            return false;
        }
    }

    return true;
}

P
pissang 已提交
327 328 329 330 331
function createLineClipPath(
    coordSys: Cartesian2D | Polar,
    hasAnimation: boolean,
    seriesModel: LineSeriesModel
) {
332
    if (coordSys.type === 'cartesian2d') {
333 334
        const isHorizontal = coordSys.getBaseAxis().isHorizontal();
        const clipPath = createGridClipPath(coordSys, hasAnimation, seriesModel);
335
        // Expand clip shape to avoid clipping when line value exceeds axis
336
        if (!seriesModel.get('clip', true)) {
337 338
            const rectShape = clipPath.shape;
            const expandSize = Math.max(rectShape.width, rectShape.height);
339 340 341 342 343 344 345 346 347
            if (isHorizontal) {
                rectShape.y -= expandSize;
                rectShape.height += expandSize * 2;
            }
            else {
                rectShape.x -= expandSize;
                rectShape.width += expandSize * 2;
            }
        }
348
        return clipPath;
349 350 351 352 353 354 355
    }
    else {
        return createPolarClipPath(coordSys, hasAnimation, seriesModel);
    }

}

P
pissang 已提交
356 357
class LineView extends ChartView {

1
100pah 已提交
358
    static readonly type = 'line';
P
pissang 已提交
359

1
100pah 已提交
360
    _symbolDraw: SymbolDraw;
P
pissang 已提交
361

1
100pah 已提交
362 363
    _lineGroup: graphic.Group;
    _coordSys: Cartesian2D | Polar;
P
pissang 已提交
364

1
100pah 已提交
365 366
    _polyline: ECPolyline;
    _polygon: ECPolygon;
P
pissang 已提交
367

1
100pah 已提交
368 369
    _stackedOnPoints: number[][];
    _points: number[][];
370

1
100pah 已提交
371 372
    _step: LineSeriesOption['step'];
    _valueOrigin: LineSeriesOption['areaStyle']['origin'];
L
lang 已提交
373

1
100pah 已提交
374
    _clipShapeForSymbol: CoordinateSystemClipArea;
P
pissang 已提交
375

1
100pah 已提交
376
    _data: List;
P
pissang 已提交
377 378

    init() {
379
        const lineGroup = new graphic.Group();
L
lang 已提交
380

381
        const symbolDraw = new SymbolDraw();
S
sushuang 已提交
382
        this.group.add(symbolDraw.group);
L
tweak  
lang 已提交
383

S
sushuang 已提交
384 385
        this._symbolDraw = symbolDraw;
        this._lineGroup = lineGroup;
P
pissang 已提交
386
    }
L
tweak  
lang 已提交
387

P
pissang 已提交
388
    render(seriesModel: LineSeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
389 390 391 392 393
        const coordSys = seriesModel.coordinateSystem;
        const group = this.group;
        const data = seriesModel.getData();
        const lineStyleModel = seriesModel.getModel('lineStyle');
        const areaStyleModel = seriesModel.getModel('areaStyle');
L
lang 已提交
394

395
        let points = data.mapArray(data.getItemLayout);
L
lang 已提交
396

397 398
        const isCoordSysPolar = coordSys.type === 'polar';
        const prevCoordSys = this._coordSys;
399

400
        const symbolDraw = this._symbolDraw;
401 402
        let polyline = this._polyline;
        let polygon = this._polygon;
L
lang 已提交
403

404
        const lineGroup = this._lineGroup;
L
lang 已提交
405

406
        const hasAnimation = seriesModel.get('animation');
L
tweak  
lang 已提交
407

408
        const isAreaChart = !areaStyleModel.isEmpty();
S
sushuang 已提交
409

410 411
        const valueOrigin = areaStyleModel.get('origin');
        const dataCoordInfo = prepareDataCoordInfo(coordSys, data, valueOrigin);
S
sushuang 已提交
412

413
        let stackedOnPoints = getStackedOnPoints(coordSys, data, dataCoordInfo);
L
lang 已提交
414

415
        const showSymbol = seriesModel.get('showSymbol');
L
lang 已提交
416

417
        const isIgnoreFunc = showSymbol && !isCoordSysPolar
P
pissang 已提交
418
            && getIsIgnoreFunc(seriesModel, data, coordSys as Cartesian2D);
419

S
sushuang 已提交
420
        // Remove temporary symbols
421
        const oldData = this._data;
P
pissang 已提交
422
        oldData && oldData.eachItemGraphicEl(function (el: SymbolExtended, idx) {
S
sushuang 已提交
423 424 425 426 427
            if (el.__temp) {
                group.remove(el);
                oldData.setItemGraphicEl(idx, null);
            }
        });
L
lang 已提交
428

S
sushuang 已提交
429 430 431 432 433 434
        // Remove previous created symbols if showSymbol changed to false
        if (!showSymbol) {
            symbolDraw.remove();
        }

        group.add(lineGroup);
435

S
sushuang 已提交
436
        // FIXME step not support polar
437
        const step = !isCoordSysPolar ? seriesModel.get('step') : false;
438
        let clipShapeForSymbol: PolarArea | Cartesian2DArea;
H
hantianjiao 已提交
439
        if (coordSys && coordSys.getArea && seriesModel.get('clip', true)) {
440 441 442
            clipShapeForSymbol = coordSys.getArea();
            // Avoid float number rounding error for symbol on the edge of axis extent.
            // See #7913 and `test/dataZoom-clip.html`.
P
pissang 已提交
443 444 445 446 447
            if ((clipShapeForSymbol as Cartesian2DArea).width != null) {
                (clipShapeForSymbol as Cartesian2DArea).x -= 0.1;
                (clipShapeForSymbol as Cartesian2DArea).y -= 0.1;
                (clipShapeForSymbol as Cartesian2DArea).width += 0.2;
                (clipShapeForSymbol as Cartesian2DArea).height += 0.2;
448
            }
P
pissang 已提交
449 450 451
            else if ((clipShapeForSymbol as PolarArea).r0) {
                (clipShapeForSymbol as PolarArea).r0 -= 0.5;
                (clipShapeForSymbol as PolarArea).r += 0.5;
452 453
            }
        }
454
        this._clipShapeForSymbol = clipShapeForSymbol;
S
sushuang 已提交
455 456 457 458
        // Initialization animation or coordinate system changed
        if (
            !(polyline && prevCoordSys.type === coordSys.type && step === this._step)
        ) {
S
sushuang 已提交
459
            showSymbol && symbolDraw.updateData(data, {
460
                isIgnore: isIgnoreFunc,
461
                clipShape: clipShapeForSymbol
S
sushuang 已提交
462
            });
S
sushuang 已提交
463 464 465 466 467

            if (step) {
                // TODO If stacked series is not step
                points = turnPointsIntoStep(points, coordSys, step);
                stackedOnPoints = turnPointsIntoStep(stackedOnPoints, coordSys, step);
468 469
            }

P
pissang 已提交
470
            polyline = this._newPolyline(points);
S
sushuang 已提交
471 472
            if (isAreaChart) {
                polygon = this._newPolygon(
P
pissang 已提交
473
                    points, stackedOnPoints
S
sushuang 已提交
474 475
                );
            }
476
            lineGroup.setClipPath(createLineClipPath(coordSys, true, seriesModel));
S
sushuang 已提交
477 478 479 480 481
        }
        else {
            if (isAreaChart && !polygon) {
                // If areaStyle is added
                polygon = this._newPolygon(
P
pissang 已提交
482
                    points, stackedOnPoints
S
sushuang 已提交
483 484 485 486 487 488 489
                );
            }
            else if (polygon && !isAreaChart) {
                // If areaStyle is removed
                lineGroup.remove(polygon);
                polygon = this._polygon = null;
            }
490

S
sushuang 已提交
491
            // Update clipPath
492
            lineGroup.setClipPath(createLineClipPath(coordSys, false, seriesModel));
493

S
sushuang 已提交
494 495
            // Always update, or it is wrong in the case turning on legend
            // because points are not changed
S
sushuang 已提交
496
            showSymbol && symbolDraw.updateData(data, {
497
                isIgnore: isIgnoreFunc,
498
                clipShape: clipShapeForSymbol
S
sushuang 已提交
499
            });
L
lang 已提交
500

S
sushuang 已提交
501 502 503
            // Stop symbol animation and sync with line points
            // FIXME performance?
            data.eachItemGraphicEl(function (el) {
504
                el && el.stopAnimation(null, true);
S
sushuang 已提交
505 506 507 508 509 510 511 512 513
            });

            // In the case data zoom triggerred refreshing frequently
            // Data may not change if line has a category axis. So it should animate nothing
            if (!isPointsSame(this._stackedOnPoints, stackedOnPoints)
                || !isPointsSame(this._points, points)
            ) {
                if (hasAnimation) {
                    this._updateAnimation(
S
sushuang 已提交
514
                        data, stackedOnPoints, coordSys, api, step, valueOrigin
L
lang 已提交
515 516
                    );
                }
S
sushuang 已提交
517 518 519 520 521 522
                else {
                    // Not do it in update with animation
                    if (step) {
                        // TODO If stacked series is not step
                        points = turnPointsIntoStep(points, coordSys, step);
                        stackedOnPoints = turnPointsIntoStep(stackedOnPoints, coordSys, step);
L
lang 已提交
523
                    }
S
sushuang 已提交
524 525 526 527 528 529 530 531

                    polyline.setShape({
                        points: points
                    });
                    polygon && polygon.setShape({
                        points: points,
                        stackedOnPoints: stackedOnPoints
                    });
L
lang 已提交
532
                }
L
lang 已提交
533
            }
S
sushuang 已提交
534 535
        }

536
        const visualColor = getVisualGradient(data, coordSys)
537
            || data.getVisual('style')[data.getVisual('drawType')];
538 539
        const focus = seriesModel.get(['emphasis', 'focus']);
        const blurScope = seriesModel.get(['emphasis', 'blurScope']);
S
sushuang 已提交
540 541 542 543 544 545 546

        polyline.useStyle(zrUtil.defaults(
            // Use color in lineStyle first
            lineStyleModel.getLineStyle(),
            {
                fill: 'none',
                stroke: visualColor,
547
                lineJoin: 'bevel' as CanvasLineJoin
S
sushuang 已提交
548 549 550
            }
        ));

551
        setStatesStylesFromModel(polyline, seriesModel, 'lineStyle');
552

553 554
        const shouldBolderOnEmphasis = seriesModel.get(['emphasis', 'lineStyle', 'width']) === 'bolder';
        if (shouldBolderOnEmphasis) {
555
            const emphasisLineStyle = polyline.getState('emphasis').style;
556
            emphasisLineStyle.lineWidth = polyline.style.lineWidth + 1;
557 558
        }

559 560 561
        // Needs seriesIndex for focus
        graphic.getECData(polyline).seriesIndex = seriesModel.seriesIndex;
        enableHoverEmphasis(polyline, focus, blurScope);
562

563
        const smooth = getSmooth(seriesModel.get('smooth'));
S
sushuang 已提交
564 565 566 567 568
        polyline.setShape({
            smooth: smooth,
            smoothMonotone: seriesModel.get('smoothMonotone'),
            connectNulls: seriesModel.get('connectNulls')
        });
L
lang 已提交
569

S
sushuang 已提交
570
        if (polygon) {
571
            const stackedOnSeries = data.getCalculationInfo('stackedOnSeries');
572
            let stackedOnSmooth = 0;
573

S
sushuang 已提交
574 575
            polygon.useStyle(zrUtil.defaults(
                areaStyleModel.getAreaStyle(),
L
lang 已提交
576
                {
S
sushuang 已提交
577 578
                    fill: visualColor,
                    opacity: 0.7,
579
                    lineJoin: 'bevel' as CanvasLineJoin
L
lang 已提交
580 581
                }
            ));
L
lang 已提交
582

S
sushuang 已提交
583
            if (stackedOnSeries) {
S
sushuang 已提交
584 585 586 587
                stackedOnSmooth = getSmooth(stackedOnSeries.get('smooth'));
            }

            polygon.setShape({
588
                smooth: smooth,
S
sushuang 已提交
589
                stackedOnSmooth: stackedOnSmooth,
L
lang 已提交
590 591
                smoothMonotone: seriesModel.get('smoothMonotone'),
                connectNulls: seriesModel.get('connectNulls')
592
            });
593 594

            setStatesStylesFromModel(polygon, seriesModel, 'areaStyle');
595 596 597
            // Needs seriesIndex for focus
            graphic.getECData(polygon).seriesIndex = seriesModel.seriesIndex;
            enableHoverEmphasis(polygon, focus, blurScope);
S
sushuang 已提交
598
        }
L
lang 已提交
599

600 601
        const changePolyState = (toState: DisplayState) => {
            this._changePolyState(toState);
602 603 604 605
        };

        data.eachItemGraphicEl(function (el) {
            // Switch polyline / polygon state if element changed its state.
606
            el && ((el as ECElement).onHoverStateChange = changePolyState);
607 608
        });

S
sushuang 已提交
609 610 611 612 613 614
        this._data = data;
        // Save the coordinate system for transition animation when data changed
        this._coordSys = coordSys;
        this._stackedOnPoints = stackedOnPoints;
        this._points = points;
        this._step = step;
S
sushuang 已提交
615
        this._valueOrigin = valueOrigin;
P
pissang 已提交
616
    }
S
sushuang 已提交
617

P
pissang 已提交
618
    dispose() {}
S
sushuang 已提交
619

P
pissang 已提交
620 621 622 623 624 625
    highlight(
        seriesModel: LineSeriesModel,
        ecModel: GlobalModel,
        api: ExtensionAPI,
        payload: Payload
    ) {
626 627
        const data = seriesModel.getData();
        const dataIndex = modelUtil.queryDataIndex(data, payload);
S
sushuang 已提交
628

629 630
        this._changePolyState('emphasis');

S
sushuang 已提交
631
        if (!(dataIndex instanceof Array) && dataIndex != null && dataIndex >= 0) {
632
            let symbol = data.getItemGraphicEl(dataIndex) as SymbolClz;
S
sushuang 已提交
633 634
            if (!symbol) {
                // Create a temporary symbol if it is not exists
635
                const pt = data.getItemLayout(dataIndex) as number[];
S
sushuang 已提交
636 637 638 639
                if (!pt) {
                    // Null data
                    return;
                }
640 641 642 643
                // fix #11360: should't draw symbol outside clipShapeForSymbol
                if (this._clipShapeForSymbol && !this._clipShapeForSymbol.contain(pt[0], pt[1])) {
                    return;
                }
S
sushuang 已提交
644
                symbol = new SymbolClz(data, dataIndex);
645
                symbol.setPosition(pt);
S
sushuang 已提交
646 647 648 649 650
                symbol.setZ(
                    seriesModel.get('zlevel'),
                    seriesModel.get('z')
                );
                symbol.ignore = isNaN(pt[0]) || isNaN(pt[1]);
P
pissang 已提交
651
                (symbol as SymbolExtended).__temp = true;
S
sushuang 已提交
652
                data.setItemGraphicEl(dataIndex, symbol);
L
lang 已提交
653

S
sushuang 已提交
654 655
                // Stop scale animation
                symbol.stopSymbolAnimation(true);
L
lang 已提交
656

S
sushuang 已提交
657 658 659 660 661 662 663 664 665 666
                this.group.add(symbol);
            }
            symbol.highlight();
        }
        else {
            // Highlight whole series
            ChartView.prototype.highlight.call(
                this, seriesModel, ecModel, api, payload
            );
        }
P
pissang 已提交
667
    }
S
sushuang 已提交
668

P
pissang 已提交
669 670 671 672 673 674
    downplay(
        seriesModel: LineSeriesModel,
        ecModel: GlobalModel,
        api: ExtensionAPI,
        payload: Payload
    ) {
675 676
        const data = seriesModel.getData();
        const dataIndex = modelUtil.queryDataIndex(data, payload) as number;
677 678 679

        this._changePolyState('normal');

S
sushuang 已提交
680
        if (dataIndex != null && dataIndex >= 0) {
681
            const symbol = data.getItemGraphicEl(dataIndex) as SymbolExtended;
S
sushuang 已提交
682 683 684 685 686 687 688
            if (symbol) {
                if (symbol.__temp) {
                    data.setItemGraphicEl(dataIndex, null);
                    this.group.remove(symbol);
                }
                else {
                    symbol.downplay();
L
lang 已提交
689
                }
L
lang 已提交
690
            }
S
sushuang 已提交
691 692 693 694 695 696 697 698 699
        }
        else {
            // FIXME
            // can not downplay completely.
            // Downplay whole series
            ChartView.prototype.downplay.call(
                this, seriesModel, ecModel, api, payload
            );
        }
P
pissang 已提交
700
    }
L
lang 已提交
701

702 703 704 705 706 707
    _changePolyState(toState: DisplayState) {
        const polygon = this._polygon;
        setStatesFlag(this._polyline, toState);
        polygon && setStatesFlag(polygon, toState);
    }

P
pissang 已提交
708
    _newPolyline(points: number[][]) {
709
        let polyline = this._polyline;
S
sushuang 已提交
710 711 712 713
        // Remove previous created polyline
        if (polyline) {
            this._lineGroup.remove(polyline);
        }
L
lang 已提交
714

P
pissang 已提交
715
        polyline = new ECPolyline({
S
sushuang 已提交
716 717 718
            shape: {
                points: points
            },
719
            segmentIgnoreThreshold: 2,
S
sushuang 已提交
720 721
            z2: 10
        });
722

S
sushuang 已提交
723
        this._lineGroup.add(polyline);
L
lang 已提交
724

S
sushuang 已提交
725
        this._polyline = polyline;
L
lang 已提交
726

S
sushuang 已提交
727
        return polyline;
P
pissang 已提交
728
    }
L
lang 已提交
729

P
pissang 已提交
730
    _newPolygon(points: number[][], stackedOnPoints: number[][]) {
731
        let polygon = this._polygon;
S
sushuang 已提交
732 733 734 735
        // Remove previous created polygon
        if (polygon) {
            this._lineGroup.remove(polygon);
        }
L
lang 已提交
736

P
pissang 已提交
737
        polygon = new ECPolygon({
S
sushuang 已提交
738 739 740 741
            shape: {
                points: points,
                stackedOnPoints: stackedOnPoints
            },
742
            segmentIgnoreThreshold: 2
S
sushuang 已提交
743
        });
L
lang 已提交
744

S
sushuang 已提交
745
        this._lineGroup.add(polygon);
L
lang 已提交
746

S
sushuang 已提交
747 748
        this._polygon = polygon;
        return polygon;
P
pissang 已提交
749
    }
750

S
sushuang 已提交
751 752 753 754
    /**
     * @private
     */
    // FIXME Two value axis
P
pissang 已提交
755 756 757 758 759 760 761 762
    _updateAnimation(
        data: List,
        stackedOnPoints: number[][],
        coordSys: Cartesian2D | Polar,
        api: ExtensionAPI,
        step: LineSeriesOption['step'],
        valueOrigin: LineSeriesOption['areaStyle']['origin']
    ) {
763 764 765
        const polyline = this._polyline;
        const polygon = this._polygon;
        const seriesModel = data.hostModel;
S
sushuang 已提交
766

767
        const diff = lineAnimationDiff(
S
sushuang 已提交
768 769
            this._data, data,
            this._stackedOnPoints, stackedOnPoints,
S
sushuang 已提交
770 771
            this._coordSys, coordSys,
            this._valueOrigin, valueOrigin
S
sushuang 已提交
772 773
        );

774 775 776 777
        let current = diff.current;
        let stackedOnCurrent = diff.stackedOnCurrent;
        let next = diff.next;
        let stackedOnNext = diff.stackedOnNext;
S
sushuang 已提交
778 779 780 781 782 783 784
        if (step) {
            // TODO If stacked series is not step
            current = turnPointsIntoStep(diff.current, coordSys, step);
            stackedOnCurrent = turnPointsIntoStep(diff.stackedOnCurrent, coordSys, step);
            next = turnPointsIntoStep(diff.next, coordSys, step);
            stackedOnNext = turnPointsIntoStep(diff.stackedOnNext, coordSys, step);
        }
785

786 787 788
        // Don't apply animation if diff is large.
        // For better result and avoid memory explosion problems like
        // https://github.com/apache/incubator-echarts/issues/12229
P
pissang 已提交
789 790
        if (getBoundingDiff(current, next) > 3000
            || (polygon && getBoundingDiff(stackedOnCurrent, stackedOnNext) > 3000)
791 792 793 794 795 796 797 798 799 800 801 802
        ) {
            polyline.setShape({
                points: next
            });
            if (polygon) {
                polygon.setShape({
                    points: next,
                    stackedOnPoints: stackedOnNext
                });
            }
            return;
        }
803

S
sushuang 已提交
804 805 806
        // `diff.current` is subset of `current` (which should be ensured by
        // turnPointsIntoStep), so points in `__points` can be updated when
        // points in `current` are update during animation.
P
pissang 已提交
807
        (polyline.shape as any).__points = diff.current;
S
sushuang 已提交
808
        polyline.shape.points = current;
L
lang 已提交
809

810 811
        // Stop previous animation.
        polyline.stopAnimation();
S
sushuang 已提交
812 813 814
        graphic.updateProps(polyline, {
            shape: {
                points: next
L
lang 已提交
815
            }
S
sushuang 已提交
816
        }, seriesModel);
L
lang 已提交
817

S
sushuang 已提交
818 819
        if (polygon) {
            polygon.setShape({
820
                // Reuse the points with polyline.
S
sushuang 已提交
821 822 823
                points: current,
                stackedOnPoints: stackedOnCurrent
            });
824
            polygon.stopAnimation();
S
sushuang 已提交
825
            graphic.updateProps(polygon, {
L
lang 已提交
826
                shape: {
S
sushuang 已提交
827
                    stackedOnPoints: stackedOnNext
L
lang 已提交
828
                }
L
lang 已提交
829
            }, seriesModel);
830 831 832 833
            // If use attr directly in updateProps.
            if (polyline.shape.points !== polygon.shape.points) {
                polygon.shape.points = polyline.shape.points;
            }
S
sushuang 已提交
834
        }
L
lang 已提交
835

836

837
        const updatedDataInfo: {
P
pissang 已提交
838 839 840
            el: SymbolExtended,
            ptIdx: number
        }[] = [];
841
        const diffStatus = diff.status;
S
sushuang 已提交
842

843
        for (let i = 0; i < diffStatus.length; i++) {
844
            const cmd = diffStatus[i].cmd;
S
sushuang 已提交
845
            if (cmd === '=') {
846
                const el = data.getItemGraphicEl(diffStatus[i].idx1) as SymbolExtended;
S
sushuang 已提交
847 848 849 850 851
                if (el) {
                    updatedDataInfo.push({
                        el: el,
                        ptIdx: i    // Index of points
                    });
L
lang 已提交
852
                }
L
lang 已提交
853
            }
S
sushuang 已提交
854
        }
L
lang 已提交
855

S
sushuang 已提交
856 857
        if (polyline.animators && polyline.animators.length) {
            polyline.animators[0].during(function () {
858
                for (let i = 0; i < updatedDataInfo.length; i++) {
859
                    const el = updatedDataInfo[i].el;
860 861
                    el.setPosition((polyline.shape as any).__points[updatedDataInfo[i].ptIdx]);
                    el.markRedraw();
862 863
                }
            });
L
lang 已提交
864
        }
P
pissang 已提交
865
    }
S
sushuang 已提交
866

P
pissang 已提交
867
    remove(ecModel: GlobalModel) {
868 869
        const group = this.group;
        const oldData = this._data;
S
sushuang 已提交
870 871 872
        this._lineGroup.removeAll();
        this._symbolDraw.remove(true);
        // Remove temporary created elements when highlighting
P
pissang 已提交
873
        oldData && oldData.eachItemGraphicEl(function (el: SymbolExtended, idx) {
S
sushuang 已提交
874 875 876 877 878 879
            if (el.__temp) {
                group.remove(el);
                oldData.setItemGraphicEl(idx, null);
            }
        });

S
sushuang 已提交
880 881 882 883 884 885
        this._polyline =
            this._polygon =
            this._coordSys =
            this._points =
            this._stackedOnPoints =
            this._data = null;
S
sushuang 已提交
886
    }
P
pissang 已提交
887 888 889 890
}

ChartView.registerClass(LineView);

891
export default ChartView;