LineView.ts 30.6 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 * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
23 24 25
import SymbolDraw from '../helper/SymbolDraw';
import SymbolClz from '../helper/Symbol';
import lineAnimationDiff from './lineAnimationDiff';
S
sushuang 已提交
26 27
import * as graphic from '../../util/graphic';
import * as modelUtil from '../../util/model';
P
pissang 已提交
28
import {ECPolyline, ECPolygon} from './poly';
S
sushuang 已提交
29
import ChartView from '../../view/Chart';
S
sushuang 已提交
30
import {prepareDataCoordInfo, getStackedOnPoint} from './helper';
31
import {createGridClipPath, createPolarClipPath} from '../helper/createClipPathFromCoordSys';
P
pissang 已提交
32 33 34 35 36 37 38
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';
39
import type { Payload, Dictionary, ColorString, ECElement, DisplayState } from '../../util/types';
P
pissang 已提交
40 41
import type OrdinalScale from '../../scale/Ordinal';
import type Axis2D from '../../coord/cartesian/Axis2D';
42
import { CoordinateSystemClipArea } from '../../coord/CoordinateSystem';
43
import { setStatesStylesFromModel, setStatesFlag, enableHoverEmphasis } from '../../util/states';
44
import { getECData } from '../../util/ecData';
45 46
import { createFloat32Array } from '../../util/vendor';
import { createSymbol } from '../../util/symbol';
P
pissang 已提交
47 48


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

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

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

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
function bboxFromPoints(points: ArrayLike<number>) {
    let minX = Infinity;
    let minY = Infinity;
    let maxX = -Infinity;
    let maxY = -Infinity;

    for (let i = 0; i < points.length;) {
        const x = points[i++];
        const y = points[i++];
        if (!isNaN(x)) {
            minX = Math.min(x, minX);
            maxX = Math.max(x, maxX);
        }
        if (!isNaN(y)) {
            minY = Math.min(y, minY);
            maxY = Math.max(y, maxY);
        }
    }
    return [
        [minX, minY],
        [maxX, maxY]
    ];
}
91

92
function getBoundingDiff(points1: ArrayLike<number>, points2: ArrayLike<number>): number {
93

94 95
    const [min1, max1] = bboxFromPoints(points1);
    const [min2, max2] = bboxFromPoints(points2);
96 97 98 99 100 101 102 103 104 105 106

    // 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 已提交
107 108
function getSmooth(smooth: number | boolean) {
    return typeof smooth === 'number' ? smooth : (smooth ? 0.5 : 0);
S
sushuang 已提交
109 110
}

P
pissang 已提交
111 112 113 114 115
function getStackedOnPoints(
    coordSys: Cartesian2D | Polar,
    data: List,
    dataCoordInfo: ReturnType<typeof prepareDataCoordInfo>
) {
S
sushuang 已提交
116 117
    if (!dataCoordInfo.valueDim) {
        return [];
S
sushuang 已提交
118
    }
L
lang 已提交
119

120 121 122 123 124 125
    const len = data.count();
    const points = createFloat32Array(len * 2);
    for (let idx = 0; idx < len; idx++) {
        const pt = getStackedOnPoint(dataCoordInfo, coordSys, data, idx);
        points[idx * 2] = pt[0];
        points[idx * 2 + 1] = pt[1];
S
sushuang 已提交
126
    }
S
sushuang 已提交
127

S
sushuang 已提交
128
    return points;
S
sushuang 已提交
129 130
}

P
pissang 已提交
131
function turnPointsIntoStep(
132
    points: ArrayLike<number>,
P
pissang 已提交
133 134
    coordSys: Cartesian2D | Polar,
    stepTurnAt: 'start' | 'end' | 'middle'
135
): number[] {
136 137
    const baseAxis = coordSys.getBaseAxis();
    const baseIndex = baseAxis.dim === 'x' || baseAxis.dim === 'radius' ? 0 : 1;
S
sushuang 已提交
138

139
    const stepPoints: number[] = [];
140
    let i = 0;
141 142 143 144 145 146 147 148 149
    const stepPt: number[] = [];
    const pt: number[] = [];
    const nextPt: number[] = [];
    for (; i < points.length - 2; i += 2) {
        nextPt[0] = points[i + 2];
        nextPt[1] = points[i + 3];
        pt[0] = points[i];
        pt[1] = points[i + 1];
        stepPoints.push(pt[0], pt[1]);
S
sushuang 已提交
150 151 152 153 154

        switch (stepTurnAt) {
            case 'end':
                stepPt[baseIndex] = nextPt[baseIndex];
                stepPt[1 - baseIndex] = pt[1 - baseIndex];
155
                stepPoints.push(stepPt[0], stepPt[1]);
S
sushuang 已提交
156 157
                break;
            case 'middle':
158 159
                const middle = (pt[baseIndex] + nextPt[baseIndex]) / 2;
                const stepPt2 = [];
S
sushuang 已提交
160 161 162
                stepPt[baseIndex] = stepPt2[baseIndex] = middle;
                stepPt[1 - baseIndex] = pt[1 - baseIndex];
                stepPt2[1 - baseIndex] = nextPt[1 - baseIndex];
163 164
                stepPoints.push(stepPt[0], stepPt[1]);
                stepPoints.push(stepPt2[0], stepPt[1]);
S
sushuang 已提交
165 166
                break;
            default:
167
                // default is start
S
sushuang 已提交
168 169
                stepPt[baseIndex] = pt[baseIndex];
                stepPt[1 - baseIndex] = nextPt[1 - baseIndex];
170
                stepPoints.push(stepPt[0], stepPt[1]);
171 172
        }
    }
S
sushuang 已提交
173
    // Last points
174
    stepPoints.push(points[i++], points[i++]);
S
sushuang 已提交
175 176 177
    return stepPoints;
}

P
pissang 已提交
178 179 180 181
function getVisualGradient(
    data: List,
    coordSys: Cartesian2D | Polar
) {
182
    const visualMetaList = data.getVisual('visualMeta');
S
sushuang 已提交
183 184 185
    if (!visualMetaList || !visualMetaList.length || !data.count()) {
        // When data.count() is 0, gradient range can not be calculated.
        return;
186 187
    }

S
sushuang 已提交
188 189 190 191 192 193 194
    if (coordSys.type !== 'cartesian2d') {
        if (__DEV__) {
            console.warn('Visual map on line style is only supported on cartesian2d.');
        }
        return;
    }

195 196
    let coordDim: 'x' | 'y';
    let visualMeta;
S
sushuang 已提交
197

198
    for (let i = visualMetaList.length - 1; i >= 0; i--) {
199 200 201
        const dimIndex = visualMetaList[i].dimension;
        const dimName = data.dimensions[dimIndex];
        const dimInfo = data.getDimensionInfo(dimName);
P
pissang 已提交
202
        coordDim = (dimInfo && dimInfo.coordDim) as 'x' | 'y';
S
sushuang 已提交
203
        // Can only be x or y
S
sushuang 已提交
204
        if (coordDim === 'x' || coordDim === 'y') {
S
sushuang 已提交
205 206
            visualMeta = visualMetaList[i];
            break;
L
lang 已提交
207 208
        }
    }
S
sushuang 已提交
209 210

    if (!visualMeta) {
S
sushuang 已提交
211 212
        if (__DEV__) {
            console.warn('Visual map on line style only support x or y dimension.');
213
        }
S
sushuang 已提交
214 215
        return;
    }
216

S
sushuang 已提交
217 218 219 220 221 222 223
    // 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`.

224
    const axis = coordSys.getAxis(coordDim);
S
sushuang 已提交
225

P
pissang 已提交
226 227 228 229 230
    interface ColorStop {
        offset: number
        coord?: number
        color: ColorString
    }
S
sushuang 已提交
231
    // dataToCoor mapping may not be linear, but must be monotonic.
232
    const colorStops: ColorStop[] = zrUtil.map(visualMeta.stops, function (stop) {
S
sushuang 已提交
233
        return {
P
pissang 已提交
234
            offset: 0,
S
sushuang 已提交
235 236 237 238
            coord: axis.toGlobalCoord(axis.dataToCoord(stop.value)),
            color: stop.color
        };
    });
239 240
    const stopLen = colorStops.length;
    const outerColors = visualMeta.outerColors.slice();
241

S
sushuang 已提交
242 243 244 245
    if (stopLen && colorStops[0].coord > colorStops[stopLen - 1].coord) {
        colorStops.reverse();
        outerColors.reverse();
    }
246

247 248 249 250
    const tinyExtent = 10; // Arbitrary value: 10px
    const minCoord = colorStops[0].coord - tinyExtent;
    const maxCoord = colorStops[stopLen - 1].coord + tinyExtent;
    const coordSpan = maxCoord - minCoord;
251

S
sushuang 已提交
252 253 254
    if (coordSpan < 1e-3) {
        return 'transparent';
    }
255

S
sushuang 已提交
256 257 258 259 260 261 262 263 264 265 266
    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'
    });
267

S
sushuang 已提交
268 269 270 271
    // 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);
    // });
272

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

S
sushuang 已提交
277 278
    return gradient;
}
279

P
pissang 已提交
280 281 282 283 284
function getIsIgnoreFunc(
    seriesModel: LineSeriesModel,
    data: List,
    coordSys: Cartesian2D
) {
285 286
    const showAllSymbol = seriesModel.get('showAllSymbol');
    const isAuto = showAllSymbol === 'auto';
287 288 289 290 291

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

292
    const categoryAxis = coordSys.getAxesByScale('ordinal')[0];
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    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.
308 309
    const categoryDataDim = data.mapDimension(categoryAxis.dim);
    const labelMap: Dictionary<1> = {};
310 311 312 313 314

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

P
pissang 已提交
315
    return function (dataIndex: number) {
316 317 318 319
        return !labelMap.hasOwnProperty(data.get(categoryDataDim, dataIndex));
    };
}

P
pissang 已提交
320 321 322 323
function canShowAllSymbolForCategory(
    categoryAxis: Axis2D,
    data: List
) {
324 325 326 327
    // 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.
328
    const axisExtent = categoryAxis.getExtent();
329
    let availSize = Math.abs(axisExtent[1] - axisExtent[0]) / (categoryAxis.scale as OrdinalScale).count();
330 331 332
    isNaN(availSize) && (availSize = 0); // 0/0 is NaN.

    // Sampling some points, max 5.
333 334
    const dataLen = data.count();
    const step = Math.max(1, Math.round(dataLen / 5));
335
    for (let dataIndex = 0; dataIndex < dataLen; dataIndex += step) {
336 337 338 339 340 341 342 343 344 345 346 347 348 349
        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 已提交
350 351 352 353 354
function createLineClipPath(
    coordSys: Cartesian2D | Polar,
    hasAnimation: boolean,
    seriesModel: LineSeriesModel
) {
355
    if (coordSys.type === 'cartesian2d') {
356 357
        const isHorizontal = coordSys.getBaseAxis().isHorizontal();
        const clipPath = createGridClipPath(coordSys, hasAnimation, seriesModel);
358
        // Expand clip shape to avoid clipping when line value exceeds axis
359
        if (!seriesModel.get('clip', true)) {
360 361
            const rectShape = clipPath.shape;
            const expandSize = Math.max(rectShape.width, rectShape.height);
362 363 364 365 366 367 368 369 370
            if (isHorizontal) {
                rectShape.y -= expandSize;
                rectShape.height += expandSize * 2;
            }
            else {
                rectShape.x -= expandSize;
                rectShape.width += expandSize * 2;
            }
        }
371
        return clipPath;
372 373 374 375 376 377 378
    }
    else {
        return createPolarClipPath(coordSys, hasAnimation, seriesModel);
    }

}

P
pissang 已提交
379 380
class LineView extends ChartView {

1
100pah 已提交
381
    static readonly type = 'line';
P
pissang 已提交
382

1
100pah 已提交
383
    _symbolDraw: SymbolDraw;
P
pissang 已提交
384

1
100pah 已提交
385 386
    _lineGroup: graphic.Group;
    _coordSys: Cartesian2D | Polar;
P
pissang 已提交
387

1
100pah 已提交
388 389
    _polyline: ECPolyline;
    _polygon: ECPolygon;
P
pissang 已提交
390

391 392
    _stackedOnPoints: ArrayLike<number>;
    _points: ArrayLike<number>;
393

1
100pah 已提交
394 395
    _step: LineSeriesOption['step'];
    _valueOrigin: LineSeriesOption['areaStyle']['origin'];
L
lang 已提交
396

1
100pah 已提交
397
    _clipShapeForSymbol: CoordinateSystemClipArea;
P
pissang 已提交
398

1
100pah 已提交
399
    _data: List;
P
pissang 已提交
400 401

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

404
        const symbolDraw = new SymbolDraw();
S
sushuang 已提交
405
        this.group.add(symbolDraw.group);
L
tweak  
lang 已提交
406

S
sushuang 已提交
407 408
        this._symbolDraw = symbolDraw;
        this._lineGroup = lineGroup;
P
pissang 已提交
409
    }
L
tweak  
lang 已提交
410

P
pissang 已提交
411
    render(seriesModel: LineSeriesModel, ecModel: GlobalModel, api: ExtensionAPI) {
412 413 414 415 416
        const coordSys = seriesModel.coordinateSystem;
        const group = this.group;
        const data = seriesModel.getData();
        const lineStyleModel = seriesModel.getModel('lineStyle');
        const areaStyleModel = seriesModel.getModel('areaStyle');
L
lang 已提交
417

418
        let points = data.getLayout('points') as number[] || [];
L
lang 已提交
419

420 421
        const isCoordSysPolar = coordSys.type === 'polar';
        const prevCoordSys = this._coordSys;
422

423
        const symbolDraw = this._symbolDraw;
424 425
        let polyline = this._polyline;
        let polygon = this._polygon;
L
lang 已提交
426

427
        const lineGroup = this._lineGroup;
L
lang 已提交
428

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

431
        const isAreaChart = !areaStyleModel.isEmpty();
S
sushuang 已提交
432

433 434
        const valueOrigin = areaStyleModel.get('origin');
        const dataCoordInfo = prepareDataCoordInfo(coordSys, data, valueOrigin);
S
sushuang 已提交
435

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

438
        const showSymbol = seriesModel.get('showSymbol');
L
lang 已提交
439

440
        const isIgnoreFunc = showSymbol && !isCoordSysPolar
P
pissang 已提交
441
            && getIsIgnoreFunc(seriesModel, data, coordSys as Cartesian2D);
442

S
sushuang 已提交
443
        // Remove temporary symbols
444
        const oldData = this._data;
P
pissang 已提交
445
        oldData && oldData.eachItemGraphicEl(function (el: SymbolExtended, idx) {
S
sushuang 已提交
446 447 448 449 450
            if (el.__temp) {
                group.remove(el);
                oldData.setItemGraphicEl(idx, null);
            }
        });
L
lang 已提交
451

S
sushuang 已提交
452 453 454 455 456 457
        // Remove previous created symbols if showSymbol changed to false
        if (!showSymbol) {
            symbolDraw.remove();
        }

        group.add(lineGroup);
458

S
sushuang 已提交
459
        // FIXME step not support polar
460
        const step = !isCoordSysPolar ? seriesModel.get('step') : false;
461
        let clipShapeForSymbol: PolarArea | Cartesian2DArea;
H
hantianjiao 已提交
462
        if (coordSys && coordSys.getArea && seriesModel.get('clip', true)) {
463 464 465
            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 已提交
466 467 468 469 470
            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;
471
            }
P
pissang 已提交
472 473 474
            else if ((clipShapeForSymbol as PolarArea).r0) {
                (clipShapeForSymbol as PolarArea).r0 -= 0.5;
                (clipShapeForSymbol as PolarArea).r += 0.5;
475 476
            }
        }
477
        this._clipShapeForSymbol = clipShapeForSymbol;
S
sushuang 已提交
478 479 480 481
        // Initialization animation or coordinate system changed
        if (
            !(polyline && prevCoordSys.type === coordSys.type && step === this._step)
        ) {
S
sushuang 已提交
482
            showSymbol && symbolDraw.updateData(data, {
483
                isIgnore: isIgnoreFunc,
484 485 486 487
                clipShape: clipShapeForSymbol,
                getSymbolPoint(idx) {
                    return [points[idx * 2], points[idx * 2 + 1]];
                }
S
sushuang 已提交
488
            });
S
sushuang 已提交
489 490 491 492 493

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

P
pissang 已提交
496
            polyline = this._newPolyline(points);
S
sushuang 已提交
497 498
            if (isAreaChart) {
                polygon = this._newPolygon(
P
pissang 已提交
499
                    points, stackedOnPoints
S
sushuang 已提交
500 501
                );
            }
502
            lineGroup.setClipPath(createLineClipPath(coordSys, true, seriesModel));
S
sushuang 已提交
503 504 505 506 507
        }
        else {
            if (isAreaChart && !polygon) {
                // If areaStyle is added
                polygon = this._newPolygon(
P
pissang 已提交
508
                    points, stackedOnPoints
S
sushuang 已提交
509 510 511 512 513 514 515
                );
            }
            else if (polygon && !isAreaChart) {
                // If areaStyle is removed
                lineGroup.remove(polygon);
                polygon = this._polygon = null;
            }
516

S
sushuang 已提交
517
            // Update clipPath
518
            lineGroup.setClipPath(createLineClipPath(coordSys, false, seriesModel));
519

S
sushuang 已提交
520 521
            // Always update, or it is wrong in the case turning on legend
            // because points are not changed
S
sushuang 已提交
522
            showSymbol && symbolDraw.updateData(data, {
523
                isIgnore: isIgnoreFunc,
524 525 526 527
                clipShape: clipShapeForSymbol,
                getSymbolPoint(idx) {
                    return [points[idx * 2], points[idx * 2 + 1]];
                }
S
sushuang 已提交
528
            });
L
lang 已提交
529

S
sushuang 已提交
530 531 532
            // Stop symbol animation and sync with line points
            // FIXME performance?
            data.eachItemGraphicEl(function (el) {
533
                el && el.stopAnimation(null, true);
S
sushuang 已提交
534 535 536 537 538 539 540 541 542
            });

            // 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 已提交
543
                        data, stackedOnPoints, coordSys, api, step, valueOrigin
L
lang 已提交
544 545
                    );
                }
S
sushuang 已提交
546 547 548 549 550 551
                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 已提交
552
                    }
S
sushuang 已提交
553 554 555 556 557 558 559 560

                    polyline.setShape({
                        points: points
                    });
                    polygon && polygon.setShape({
                        points: points,
                        stackedOnPoints: stackedOnPoints
                    });
L
lang 已提交
561
                }
L
lang 已提交
562
            }
S
sushuang 已提交
563 564
        }

565
        const visualColor = getVisualGradient(data, coordSys)
566
            || data.getVisual('style')[data.getVisual('drawType')];
567 568
        const focus = seriesModel.get(['emphasis', 'focus']);
        const blurScope = seriesModel.get(['emphasis', 'blurScope']);
S
sushuang 已提交
569 570 571 572 573 574 575

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

580
        setStatesStylesFromModel(polyline, seriesModel, 'lineStyle');
581

582 583
        const shouldBolderOnEmphasis = seriesModel.get(['emphasis', 'lineStyle', 'width']) === 'bolder';
        if (shouldBolderOnEmphasis) {
584
            const emphasisLineStyle = polyline.getState('emphasis').style;
585
            emphasisLineStyle.lineWidth = polyline.style.lineWidth + 1;
586 587
        }

588
        // Needs seriesIndex for focus
589
        getECData(polyline).seriesIndex = seriesModel.seriesIndex;
590
        enableHoverEmphasis(polyline, focus, blurScope);
591

592
        const smooth = getSmooth(seriesModel.get('smooth'));
S
sushuang 已提交
593 594 595 596 597
        polyline.setShape({
            smooth: smooth,
            smoothMonotone: seriesModel.get('smoothMonotone'),
            connectNulls: seriesModel.get('connectNulls')
        });
L
lang 已提交
598

S
sushuang 已提交
599
        if (polygon) {
600
            const stackedOnSeries = data.getCalculationInfo('stackedOnSeries');
601
            let stackedOnSmooth = 0;
602

S
sushuang 已提交
603 604
            polygon.useStyle(zrUtil.defaults(
                areaStyleModel.getAreaStyle(),
L
lang 已提交
605
                {
S
sushuang 已提交
606 607
                    fill: visualColor,
                    opacity: 0.7,
608
                    lineJoin: 'bevel' as CanvasLineJoin
L
lang 已提交
609 610
                }
            ));
L
lang 已提交
611

S
sushuang 已提交
612
            if (stackedOnSeries) {
S
sushuang 已提交
613 614 615 616
                stackedOnSmooth = getSmooth(stackedOnSeries.get('smooth'));
            }

            polygon.setShape({
617
                smooth: smooth,
S
sushuang 已提交
618
                stackedOnSmooth: stackedOnSmooth,
L
lang 已提交
619 620
                smoothMonotone: seriesModel.get('smoothMonotone'),
                connectNulls: seriesModel.get('connectNulls')
621
            });
622 623

            setStatesStylesFromModel(polygon, seriesModel, 'areaStyle');
624
            // Needs seriesIndex for focus
625
            getECData(polygon).seriesIndex = seriesModel.seriesIndex;
626
            enableHoverEmphasis(polygon, focus, blurScope);
S
sushuang 已提交
627
        }
L
lang 已提交
628

629 630
        const changePolyState = (toState: DisplayState) => {
            this._changePolyState(toState);
631 632 633 634
        };

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

S
sushuang 已提交
638 639 640 641 642 643
        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 已提交
644
        this._valueOrigin = valueOrigin;
P
pissang 已提交
645
    }
S
sushuang 已提交
646

P
pissang 已提交
647
    dispose() {}
S
sushuang 已提交
648

P
pissang 已提交
649 650 651 652 653 654
    highlight(
        seriesModel: LineSeriesModel,
        ecModel: GlobalModel,
        api: ExtensionAPI,
        payload: Payload
    ) {
655 656
        const data = seriesModel.getData();
        const dataIndex = modelUtil.queryDataIndex(data, payload);
S
sushuang 已提交
657

658 659
        this._changePolyState('emphasis');

S
sushuang 已提交
660
        if (!(dataIndex instanceof Array) && dataIndex != null && dataIndex >= 0) {
661
            const points = data.getLayout('points');
662
            let symbol = data.getItemGraphicEl(dataIndex) as SymbolClz;
S
sushuang 已提交
663 664
            if (!symbol) {
                // Create a temporary symbol if it is not exists
665 666 667
                const x = points[dataIndex * 2];
                const y = points[dataIndex * 2 + 1];
                if (isNaN(x) || isNaN(y)) {
S
sushuang 已提交
668 669 670
                    // Null data
                    return;
                }
671
                // fix #11360: should't draw symbol outside clipShapeForSymbol
672
                if (this._clipShapeForSymbol && !this._clipShapeForSymbol.contain(x, y)) {
673 674
                    return;
                }
S
sushuang 已提交
675
                symbol = new SymbolClz(data, dataIndex);
676 677
                symbol.x = x;
                symbol.y = y;
S
sushuang 已提交
678 679 680 681
                symbol.setZ(
                    seriesModel.get('zlevel'),
                    seriesModel.get('z')
                );
P
pissang 已提交
682
                (symbol as SymbolExtended).__temp = true;
S
sushuang 已提交
683
                data.setItemGraphicEl(dataIndex, symbol);
L
lang 已提交
684

S
sushuang 已提交
685 686
                // Stop scale animation
                symbol.stopSymbolAnimation(true);
L
lang 已提交
687

S
sushuang 已提交
688 689 690 691 692 693 694 695 696 697
                this.group.add(symbol);
            }
            symbol.highlight();
        }
        else {
            // Highlight whole series
            ChartView.prototype.highlight.call(
                this, seriesModel, ecModel, api, payload
            );
        }
P
pissang 已提交
698
    }
S
sushuang 已提交
699

P
pissang 已提交
700 701 702 703 704 705
    downplay(
        seriesModel: LineSeriesModel,
        ecModel: GlobalModel,
        api: ExtensionAPI,
        payload: Payload
    ) {
706 707
        const data = seriesModel.getData();
        const dataIndex = modelUtil.queryDataIndex(data, payload) as number;
708 709 710

        this._changePolyState('normal');

S
sushuang 已提交
711
        if (dataIndex != null && dataIndex >= 0) {
712
            const symbol = data.getItemGraphicEl(dataIndex) as SymbolExtended;
S
sushuang 已提交
713 714 715 716 717 718 719
            if (symbol) {
                if (symbol.__temp) {
                    data.setItemGraphicEl(dataIndex, null);
                    this.group.remove(symbol);
                }
                else {
                    symbol.downplay();
L
lang 已提交
720
                }
L
lang 已提交
721
            }
S
sushuang 已提交
722 723 724 725 726 727 728 729 730
        }
        else {
            // FIXME
            // can not downplay completely.
            // Downplay whole series
            ChartView.prototype.downplay.call(
                this, seriesModel, ecModel, api, payload
            );
        }
P
pissang 已提交
731
    }
L
lang 已提交
732

733 734 735 736 737 738
    _changePolyState(toState: DisplayState) {
        const polygon = this._polygon;
        setStatesFlag(this._polyline, toState);
        polygon && setStatesFlag(polygon, toState);
    }

739
    _newPolyline(points: ArrayLike<number>) {
740
        let polyline = this._polyline;
S
sushuang 已提交
741 742 743 744
        // Remove previous created polyline
        if (polyline) {
            this._lineGroup.remove(polyline);
        }
L
lang 已提交
745

P
pissang 已提交
746
        polyline = new ECPolyline({
S
sushuang 已提交
747
            shape: {
748
                points
S
sushuang 已提交
749
            },
750
            segmentIgnoreThreshold: 2,
S
sushuang 已提交
751 752
            z2: 10
        });
753

S
sushuang 已提交
754
        this._lineGroup.add(polyline);
L
lang 已提交
755

S
sushuang 已提交
756
        this._polyline = polyline;
L
lang 已提交
757

S
sushuang 已提交
758
        return polyline;
P
pissang 已提交
759
    }
L
lang 已提交
760

761
    _newPolygon(points: ArrayLike<number>, stackedOnPoints: ArrayLike<number>) {
762
        let polygon = this._polygon;
S
sushuang 已提交
763 764 765 766
        // Remove previous created polygon
        if (polygon) {
            this._lineGroup.remove(polygon);
        }
L
lang 已提交
767

P
pissang 已提交
768
        polygon = new ECPolygon({
S
sushuang 已提交
769
            shape: {
770
                points,
S
sushuang 已提交
771 772
                stackedOnPoints: stackedOnPoints
            },
773
            segmentIgnoreThreshold: 2
S
sushuang 已提交
774
        });
L
lang 已提交
775

S
sushuang 已提交
776
        this._lineGroup.add(polygon);
L
lang 已提交
777

S
sushuang 已提交
778 779
        this._polygon = polygon;
        return polygon;
P
pissang 已提交
780
    }
781

S
sushuang 已提交
782 783 784 785
    /**
     * @private
     */
    // FIXME Two value axis
P
pissang 已提交
786 787
    _updateAnimation(
        data: List,
788
        stackedOnPoints: ArrayLike<number>,
P
pissang 已提交
789 790 791 792 793
        coordSys: Cartesian2D | Polar,
        api: ExtensionAPI,
        step: LineSeriesOption['step'],
        valueOrigin: LineSeriesOption['areaStyle']['origin']
    ) {
794 795 796
        const polyline = this._polyline;
        const polygon = this._polygon;
        const seriesModel = data.hostModel;
S
sushuang 已提交
797

798
        const diff = lineAnimationDiff(
S
sushuang 已提交
799 800
            this._data, data,
            this._stackedOnPoints, stackedOnPoints,
S
sushuang 已提交
801 802
            this._coordSys, coordSys,
            this._valueOrigin, valueOrigin
S
sushuang 已提交
803 804
        );

805 806 807 808
        let current = diff.current;
        let stackedOnCurrent = diff.stackedOnCurrent;
        let next = diff.next;
        let stackedOnNext = diff.stackedOnNext;
S
sushuang 已提交
809 810 811 812 813 814 815
        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);
        }
816

817 818 819
        // 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 已提交
820 821
        if (getBoundingDiff(current, next) > 3000
            || (polygon && getBoundingDiff(stackedOnCurrent, stackedOnNext) > 3000)
822 823 824 825 826 827 828 829 830 831 832 833
        ) {
            polyline.setShape({
                points: next
            });
            if (polygon) {
                polygon.setShape({
                    points: next,
                    stackedOnPoints: stackedOnNext
                });
            }
            return;
        }
834

S
sushuang 已提交
835 836 837
        // `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 已提交
838
        (polyline.shape as any).__points = diff.current;
S
sushuang 已提交
839
        polyline.shape.points = current;
L
lang 已提交
840

841 842
        // Stop previous animation.
        polyline.stopAnimation();
S
sushuang 已提交
843 844 845
        graphic.updateProps(polyline, {
            shape: {
                points: next
L
lang 已提交
846
            }
S
sushuang 已提交
847
        }, seriesModel);
L
lang 已提交
848

S
sushuang 已提交
849 850
        if (polygon) {
            polygon.setShape({
851
                // Reuse the points with polyline.
S
sushuang 已提交
852 853 854
                points: current,
                stackedOnPoints: stackedOnCurrent
            });
855
            polygon.stopAnimation();
S
sushuang 已提交
856
            graphic.updateProps(polygon, {
L
lang 已提交
857
                shape: {
S
sushuang 已提交
858
                    stackedOnPoints: stackedOnNext
L
lang 已提交
859
                }
L
lang 已提交
860
            }, seriesModel);
861 862 863 864
            // If use attr directly in updateProps.
            if (polyline.shape.points !== polygon.shape.points) {
                polygon.shape.points = polyline.shape.points;
            }
S
sushuang 已提交
865
        }
L
lang 已提交
866

867

868
        const updatedDataInfo: {
P
pissang 已提交
869 870 871
            el: SymbolExtended,
            ptIdx: number
        }[] = [];
872
        const diffStatus = diff.status;
S
sushuang 已提交
873

874
        for (let i = 0; i < diffStatus.length; i++) {
875
            const cmd = diffStatus[i].cmd;
S
sushuang 已提交
876
            if (cmd === '=') {
877
                const el = data.getItemGraphicEl(diffStatus[i].idx1) as SymbolExtended;
S
sushuang 已提交
878 879 880 881 882
                if (el) {
                    updatedDataInfo.push({
                        el: el,
                        ptIdx: i    // Index of points
                    });
L
lang 已提交
883
                }
L
lang 已提交
884
            }
S
sushuang 已提交
885
        }
L
lang 已提交
886

S
sushuang 已提交
887 888
        if (polyline.animators && polyline.animators.length) {
            polyline.animators[0].during(function () {
889
                const points = (polyline.shape as any).__points;
890
                for (let i = 0; i < updatedDataInfo.length; i++) {
891
                    const el = updatedDataInfo[i].el;
892 893 894
                    const offset = updatedDataInfo[i].ptIdx * 2;
                    el.x = points[offset];
                    el.y = points[offset + 1];
895
                    el.markRedraw();
896 897
                }
            });
L
lang 已提交
898
        }
P
pissang 已提交
899
    }
S
sushuang 已提交
900

P
pissang 已提交
901
    remove(ecModel: GlobalModel) {
902 903
        const group = this.group;
        const oldData = this._data;
S
sushuang 已提交
904 905 906
        this._lineGroup.removeAll();
        this._symbolDraw.remove(true);
        // Remove temporary created elements when highlighting
P
pissang 已提交
907
        oldData && oldData.eachItemGraphicEl(function (el: SymbolExtended, idx) {
S
sushuang 已提交
908 909 910 911 912 913
            if (el.__temp) {
                group.remove(el);
                oldData.setItemGraphicEl(idx, null);
            }
        });

S
sushuang 已提交
914 915 916 917 918 919
        this._polyline =
            this._polygon =
            this._coordSys =
            this._points =
            this._stackedOnPoints =
            this._data = null;
S
sushuang 已提交
920
    }
P
pissang 已提交
921 922 923 924
}

ChartView.registerClass(LineView);

925
export default ChartView;