List.ts 81.8 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.
*/

S
sushuang 已提交
20 21
/* global Float64Array, Int32Array, Uint32Array, Uint16Array */

L
lang 已提交
22 23 24
/**
 * List for data storage
 */
L
lang 已提交
25

S
sushuang 已提交
26
import * as zrUtil from 'zrender/src/core/util';
S
sushuang 已提交
27 28
import Model from '../model/Model';
import DataDiffer from './DataDiffer';
29
import {DefaultDataProvider, DataProvider} from './helper/dataProvider';
30
import {summarizeDimensions, DimensionSummary} from './helper/dimensionHelper';
S
fix:  
SHUANG SU 已提交
31
import DataDimensionInfo from './DataDimensionInfo';
32 33 34
import {ArrayLike, Dictionary, FunctionPropertyNames} from 'zrender/src/core/types';
import Element from 'zrender/src/Element';
import {
35
    DimensionIndex, DimensionName, DimensionLoose, OptionDataItem,
36 37
    ParsedValue, ParsedValueNumeric, OrdinalNumber, DimensionUserOuput,
    ModelOption, SeriesDataType, OrdinalRawValue
38
} from '../util/types';
39
import {isDataItemOption, convertOptionIdName} from '../util/model';
S
susiwen8 已提交
40
import { getECData } from '../util/ecData';
41
import { PathStyleProps } from 'zrender/src/graphic/Path';
P
pissang 已提交
42 43
import type Graph from './Graph';
import type Tree from './Tree';
44
import type { VisualMeta } from '../component/visualMap/VisualMapModel';
45
import { parseDataValue } from './helper/dataValueHelper';
1
fix:  
100pah 已提交
46
import { isSourceInstance } from './Source';
47

48
const mathFloor = Math.floor;
49
const isObject = zrUtil.isObject;
S
sushuang 已提交
50

51 52
const UNDEFINED = 'undefined';
const INDEX_NOT_FOUND = -1;
S
sushuang 已提交
53

54 55
// Use prefix to avoid index to be the same as otherIdList[idx],
// which will cause weird udpate animation.
56
const ID_PREFIX = 'e\0\0';
57

58
const dataCtors = {
S
sushuang 已提交
59 60 61 62
    'float': typeof Float64Array === UNDEFINED
        ? Array : Float64Array,
    'int': typeof Int32Array === UNDEFINED
        ? Array : Int32Array,
S
sushuang 已提交
63 64 65 66 67 68
    // Ordinal data type can be string or int
    'ordinal': Array,
    'number': Array,
    'time': Array
};

69 70
export type ListDimensionType = keyof typeof dataCtors;

S
sushuang 已提交
71 72
// Caution: MUST not use `new CtorUint32Array(arr, 0, len)`, because the Ctor of array is
// different from the Ctor of typed array.
73 74 75
const CtorUint32Array = typeof Uint32Array === UNDEFINED ? Array : Uint32Array;
const CtorInt32Array = typeof Int32Array === UNDEFINED ? Array : Int32Array;
const CtorUint16Array = typeof Uint16Array === UNDEFINED ? Array : Uint16Array;
P
pissang 已提交
76

77 78 79 80 81 82 83 84 85 86 87
type DataTypedArray = Uint32Array | Int32Array | Uint16Array | Float64Array;
type DataTypedArrayConstructor = typeof Uint32Array | typeof Int32Array | typeof Uint16Array | typeof Float64Array;
type DataArrayLikeConstructor = typeof Array | DataTypedArrayConstructor;


type DimValueGetter = (
    this: List,
    dataItem: any,
    dimName: DimensionName,
    dataIndex: number,
    dimIndex: DimensionIndex
88
) => ParsedValue;
89

90
type DataValueChunk = ArrayLike<ParsedValue>;
91 92 93 94 95 96 97 98
type DataStorage = {[dimName: string]: DataValueChunk[]};
type NameRepeatCount = {[name: string]: number};


type ItrParamDims = DimensionLoose | Array<DimensionLoose>;
// If Ctx not specified, use List as Ctx
type CtxOrList<Ctx> = unknown extends Ctx ? List : Ctx;
type EachCb0<Ctx> = (this: CtxOrList<Ctx>, idx: number) => void;
99 100
type EachCb1<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, idx: number) => void;
type EachCb2<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, y: ParsedValue, idx: number) => void;
101 102
type EachCb<Ctx> = (this: CtxOrList<Ctx>, ...args: any) => void;
type FilterCb0<Ctx> = (this: CtxOrList<Ctx>, idx: number) => boolean;
103 104
type FilterCb1<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, idx: number) => boolean;
type FilterCb2<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, y: ParsedValue, idx: number) => boolean;
105 106
type FilterCb<Ctx> = (this: CtxOrList<Ctx>, ...args: any) => boolean;
type MapArrayCb0<Ctx> = (this: CtxOrList<Ctx>, idx: number) => any;
107 108
type MapArrayCb1<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, idx: number) => any;
type MapArrayCb2<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, y: ParsedValue, idx: number) => any;
109
type MapArrayCb<Ctx> = (this: CtxOrList<Ctx>, ...args: any) => any;
110 111 112 113
type MapCb1<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, idx: number) => ParsedValue | ParsedValue[];
type MapCb2<Ctx> = (this: CtxOrList<Ctx>, x: ParsedValue, y: ParsedValue, idx: number) =>
    ParsedValue | ParsedValue[];
type MapCb<Ctx> = (this: CtxOrList<Ctx>, ...args: any) => ParsedValue | ParsedValue[];
114

S
sushuang 已提交
115

116
const TRANSFERABLE_PROPERTIES = [
117 118 119 120
    'hasItemOption', '_nameList', '_idList', '_invertedIndicesMap',
    '_rawData', '_chunkSize', '_chunkCount', '_dimValueGetter',
    '_count', '_rawCount', '_nameDimIdx', '_idDimIdx'
];
121
const CLONE_PROPERTIES = [
122
    '_extent', '_approximateExtent', '_rawExtent'
S
sushuang 已提交
123 124
];

125 126
export interface DefaultDataVisual {
    style: PathStyleProps
127 128
    // Draw type determined which prop should be set with encoded color.
    // It's only available on the global visual. Use getVisual('drawType') to access it.
129
    // It will be set in visual/style.ts module in the first priority.
130
    drawType: 'fill' | 'stroke'
131 132 133

    symbol?: string
    symbolSize?: number | number[]
134
    symbolRotate?: number
135 136 137 138 139 140 141 142
    symbolKeepAspect?: boolean

    liftZ?: number
    // For legend.
    legendSymbol?: string

    // visualMap will inject visualMeta data
    visualMeta?: VisualMeta[]
143 144 145

    // If color is encoded from palette
    colorFromPalette?: boolean
146
}
L
lang 已提交
147

148 149 150 151 152 153 154 155 156
export interface DataCalculationInfo<SERIES_MODEL> {
    stackedDimension: string;
    stackedByDimension: string;
    isStackedByIndex: boolean;
    stackedOverDimension: string;
    stackResultDimension: string;
    stackedOnSeries?: SERIES_MODEL;
}

157 158 159 160 161
// -----------------------------
// Internal method declarations:
// -----------------------------
let defaultDimValueGetters: {[sourceFormat: string]: DimValueGetter};
let prepareInvertedIndex: (list: List) => void;
162
let getRawValueFromStore: (list: List, dimIndex: number, rawIndex: number) => ParsedValue | OrdinalRawValue;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
let getIndicesCtor: (list: List) => DataArrayLikeConstructor;
let prepareChunks: (
    storage: DataStorage, dimInfo: DataDimensionInfo, chunkSize: number, chunkCount: number, end: number
) => void;
let getRawIndexWithoutIndices: (this: List, idx: number) => number;
let getRawIndexWithIndices: (this: List, idx: number) => number;
let getId: (list: List, rawIndex: number) => string;
let normalizeDimensions: (dimensions: ItrParamDims) => Array<DimensionLoose>;
let validateDimensions: (list: List, dims: DimensionName[]) => void;
let cloneListForMapAndSample: (original: List, excludeDimensions: DimensionName[]) => List;
let cloneDimStore: (originalDimStore: DataValueChunk[]) => DataValueChunk[];
let getInitialExtent: () => [number, number];
let setItemDataAndSeriesIndex: (this: Element, child: Element) => void;
let transferProperties: (target: List, source: List) => void;

178 179 180 181
class List<
    HostModel extends Model = Model,
    Visual extends DefaultDataVisual = DefaultDataVisual
> {
L
lang 已提交
182

183
    readonly type = 'list';
L
lang 已提交
184

185
    readonly dimensions: string[];
186

187 188
    // Infomation of each data dimension, like data type.
    private _dimensionInfos: {[dimName: string]: DataDimensionInfo};
189

P
pissang 已提交
190
    readonly hostModel: HostModel;
191

192 193 194 195
    /**
     * @readonly
     */
    dataType: SeriesDataType;
196

P
pissang 已提交
197
    /**
198
     * @readonly
P
pissang 已提交
199 200
     * Host graph if List is used to store graph nodes / edges.
     */
201 202
    graph?: Graph;

P
pissang 已提交
203
    /**
204
     * @readonly
P
pissang 已提交
205 206
     * Host tree if List is used to store tree ndoes.
     */
207
    tree?: Tree;
P
pissang 已提交
208

209 210 211
    // Indices stores the indices of data subset after filtered.
    // This data subset will be used in chart.
    private _indices: ArrayLike<any>;
S
sushuang 已提交
212

213 214 215
    private _count: number = 0;
    private _rawCount: number = 0;
    private _storage: DataStorage = {};
216
    private _storageArr: DataValueChunk[][] = [];
217 218
    private _nameList: string[] = [];
    private _idList: string[] = [];
S
sushuang 已提交
219

220 221 222
    // Models of data option is stored sparse for optimizing memory cost
    // Never used yet (not used yet).
    // private _optionModels: Model[] = [];
S
sushuang 已提交
223

224 225
    // Global visual properties after visual coding
    private _visual: Dictionary<any> = {};
S
sushuang 已提交
226

227 228
    // Globel layout properties.
    private _layout: Dictionary<any> = {};
S
sushuang 已提交
229

230 231
    // Item visual properties after visual coding
    private _itemVisuals: Dictionary<any>[] = [];
S
sushuang 已提交
232

233 234
    // Item layout properties after layout
    private _itemLayouts: any[] = [];
S
sushuang 已提交
235

236 237
    // Graphic elemnents
    private _graphicEls: Element[] = [];
O
Ovilia 已提交
238

239 240
    // Max size of each chunk.
    private _chunkSize: number = 1e5;
O
Ovilia 已提交
241

242
    private _chunkCount: number = 0;
L
lang 已提交
243

244
    private _rawData: DataProvider;
P
pah100 已提交
245

246 247 248
    // Raw extent will not be cloned, but only transfered.
    // It will not be calculated util needed.
    private _rawExtent: {[dimName: string]: [number, number]} = {};
L
lang 已提交
249

250
    private _extent: {[dimName: string]: [number, number]} = {};
L
lang 已提交
251

252 253
    // key: dim, value: extent
    private _approximateExtent: {[dimName: string]: [number, number]} = {};
S
sushuang 已提交
254

255
    private _dimensionsSummary: DimensionSummary;
P
pah100 已提交
256

257
    private _invertedIndicesMap: {[dimName: string]: ArrayLike<number>};
L
lang 已提交
258

259
    private _calculationInfo: DataCalculationInfo<HostModel> = {} as DataCalculationInfo<HostModel>;
P
pah100 已提交
260

261 262 263 264 265 266 267 268
    // User output info of this data.
    // DO NOT use it in other places!
    // When preparing user params for user callbacks, we have
    // to clone these inner data structures to prevent users
    // from modifying them to effect built-in logic. And for
    // performance consideration we make this `userOutput` to
    // avoid clone them too many times.
    readonly userOutput: DimensionUserOuput;
O
Ovilia 已提交
269

270 271
    // If each data item has it's own option
    hasItemOption: boolean = true;
S
sushuang 已提交
272

273 274 275 276
    // @readonly
    defaultDimValueGetter: DimValueGetter;
    private _dimValueGetter: DimValueGetter;
    private _dimValueGetterArrayRows: DimValueGetter;
L
lang 已提交
277

278 279 280
    private _nameRepeatCount: NameRepeatCount;
    private _nameDimIdx: number;
    private _idDimIdx: number;
S
sushuang 已提交
281

282
    private __wrappedMethods: string[];
S
sushuang 已提交
283

284 285
    // Methods that create a new list based on this list should be listed here.
    // Notice that those method should `RETURN` the new list.
286
    TRANSFERABLE_METHODS = ['cloneShallow', 'downSample', 'lttbDownSample', 'map'] as const;
287
    // Methods that change indices of this list should be listed here.
288
    CHANGABLE_METHODS = ['filterSelf', 'selectRange'] as const;
289
    DOWNSAMPLE_METHODS = ['downSample', 'lttbDownSample'] as const;
290 291

    /**
292 293 294
     * @param dimensions
     *        For example, ['someDimName', {name: 'someDimName', type: 'someDimType'}, ...].
     *        Dimensions should be concrete names like x, y, z, lng, lat, angle, radius
295
     */
P
pissang 已提交
296
    constructor(dimensions: Array<string | object | DataDimensionInfo>, hostModel: HostModel) {
297 298
        dimensions = dimensions || ['x', 'y'];

299 300 301
        const dimensionInfos: Dictionary<DataDimensionInfo> = {};
        const dimensionNames = [];
        const invertedIndicesMap: Dictionary<number[]> = {};
302

303
        for (let i = 0; i < dimensions.length; i++) {
304
            // Use the original dimensions[i], where other flag props may exists.
305
            const dimInfoInput = dimensions[i];
306

307
            const dimensionInfo: DataDimensionInfo =
308 309 310 311 312 313
                zrUtil.isString(dimInfoInput)
                ? new DataDimensionInfo({name: dimInfoInput})
                : !(dimInfoInput instanceof DataDimensionInfo)
                ? new DataDimensionInfo(dimInfoInput)
                : dimInfoInput;

314
            const dimensionName = dimensionInfo.name;
315 316 317 318 319
            dimensionInfo.type = dimensionInfo.type || 'float';
            if (!dimensionInfo.coordDim) {
                dimensionInfo.coordDim = dimensionName;
                dimensionInfo.coordDimIndex = 0;
            }
320

321 322 323
            dimensionInfo.otherDims = dimensionInfo.otherDims || {};
            dimensionNames.push(dimensionName);
            dimensionInfos[dimensionName] = dimensionInfo;
L
lang 已提交
324

325
            dimensionInfo.index = i;
S
sushuang 已提交
326

327 328 329 330
            if (dimensionInfo.createInvertedIndices) {
                invertedIndicesMap[dimensionName] = [];
            }
        }
S
sushuang 已提交
331

332 333 334 335 336 337 338 339 340 341 342
        this.dimensions = dimensionNames;
        this._dimensionInfos = dimensionInfos;
        this.hostModel = hostModel;

        // Cache summary info for fast visit. See "dimensionHelper".
        this._dimensionsSummary = summarizeDimensions(this);

        this._invertedIndicesMap = invertedIndicesMap;

        this.userOutput = this._dimensionsSummary.userOutput;
    }
343 344

    /**
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
     * The meanings of the input parameter `dim`:
     *
     * + If dim is a number (e.g., `1`), it means the index of the dimension.
     *   For example, `getDimension(0)` will return 'x' or 'lng' or 'radius'.
     * + If dim is a number-like string (e.g., `"1"`):
     *     + If there is the same concrete dim name defined in `this.dimensions`, it means that concrete name.
     *     + If not, it will be converted to a number, which means the index of the dimension.
     *        (why? because of the backward compatbility. We have been tolerating number-like string in
     *        dimension setting, although now it seems that it is not a good idea.)
     *     For example, `visualMap[i].dimension: "1"` is the same meaning as `visualMap[i].dimension: 1`,
     *     if no dimension name is defined as `"1"`.
     * + If dim is a not-number-like string, it means the concrete dim name.
     *   For example, it can be be default name `"x"`, `"y"`, `"z"`, `"lng"`, `"lat"`, `"angle"`, `"radius"`,
     *   or customized in `dimensions` property of option like `"age"`.
     *
     * Get dimension name
     * @param dim See above.
     * @return Concrete dim name.
363
     */
364 365 366 367 368 369 370 371 372
    getDimension(dim: DimensionLoose): DimensionName {
        if (typeof dim === 'number'
            // If being a number-like string but not being defined a dimension name.
            || (!isNaN(dim as any) && !this._dimensionInfos.hasOwnProperty(dim))
        ) {
            dim = this.dimensions[dim as DimensionIndex];
        }
        return dim as DimensionName;
    }
S
sushuang 已提交
373 374

    /**
375 376 377 378
     * Get type and calculation info of particular dimension
     * @param dim
     *        Dimension can be concrete names like x, y, z, lng, lat, angle, radius
     *        Or a ordinal number. For example getDimensionInfo(0) will return 'x' or 'lng' or 'radius'
S
sushuang 已提交
379
     */
380 381 382 383
    getDimensionInfo(dim: DimensionLoose): DataDimensionInfo {
        // Do not clone, because there may be categories in dimInfo.
        return this._dimensionInfos[this.getDimension(dim)];
    }
S
sushuang 已提交
384 385

    /**
386
     * concrete dimension name list on coord.
S
sushuang 已提交
387
     */
388 389 390
    getDimensionsOnCoord(): DimensionName[] {
        return this._dimensionsSummary.dataDimsOnCoord.slice();
    }
391 392

    /**
393 394
     * @param coordDim
     * @param idx A coordDim may map to more than one data dim.
395 396
     *        If not specified, return the first dim not extra.
     * @return concrete data dim. If not found, return null/undefined
397
     */
398 399
    mapDimension(coordDim: DimensionName): DimensionName;
    mapDimension(coordDim: DimensionName, idx: number): DimensionName;
400
    mapDimension(coordDim: DimensionName, idx?: number): DimensionName {
401
        const dimensionsSummary = this._dimensionsSummary;
402 403 404 405

        if (idx == null) {
            return dimensionsSummary.encodeFirstDimNotExtra[coordDim] as any;
        }
L
lang 已提交
406

407
        const dims = dimensionsSummary.encode[coordDim];
408 409 410 411 412 413 414
        return dims ? dims[idx as number] as any : null;
    }

    mapDimensionsAll(coordDim: DimensionName): DimensionName[] {
        const dimensionsSummary = this._dimensionsSummary;
        const dims = dimensionsSummary.encode[coordDim];
        return (dims || []).slice();
S
sushuang 已提交
415
    }
L
lang 已提交
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    /**
     * Initialize from data
     * @param data source or data or data provider.
     * @param nameLIst The name of a datum is used on data diff and
     *        defualt label/tooltip.
     *        A name can be specified in encode.itemName,
     *        or dataItem.name (only for series option data),
     *        or provided in nameList from outside.
     */
    initData(
        data: any,
        nameList?: string[],
        dimValueGetter?: DimValueGetter
    ): void {

1
fix:  
100pah 已提交
432
        const notProvider = isSourceInstance(data) || zrUtil.isArrayLike(data);
433 434 435
        if (notProvider) {
            data = new DefaultDataProvider(data, this.dimensions.length);
        }
L
lang 已提交
436

437 438 439 440 441 442 443
        if (__DEV__) {
            if (!notProvider
                && (typeof data.getItem !== 'function' || typeof data.count !== 'function')
            ) {
                throw new Error('Inavlid data provider.');
            }
        }
S
sushuang 已提交
444

445
        this._rawData = data;
S
sushuang 已提交
446

447 448
        // Clear
        this._storage = {};
449
        this._storageArr = [];
450
        this._indices = null;
S
sushuang 已提交
451

452
        this._nameList = nameList || [];
453

454
        this._idList = [];
455

456
        this._nameRepeatCount = {};
457

458 459
        if (!dimValueGetter) {
            this.hasItemOption = false;
L
lang 已提交
460
        }
461

462 463 464 465 466 467 468
        this.defaultDimValueGetter = defaultDimValueGetters[
            this._rawData.getSource().sourceFormat
        ];
        // Default dim value getter
        this._dimValueGetter = dimValueGetter = dimValueGetter
            || this.defaultDimValueGetter;
        this._dimValueGetterArrayRows = defaultDimValueGetters.arrayRows;
L
lang 已提交
469

470 471
        // Reset raw extent.
        this._rawExtent = {};
L
lang 已提交
472

473
        this._initDataFromProvider(0, data.count());
L
lang 已提交
474

475 476 477 478 479
        // If data has no item option.
        if (data.pure) {
            this.hasItemOption = false;
        }
    }
L
lang 已提交
480

481 482
    getProvider(): DataProvider {
        return this._rawData;
S
sushuang 已提交
483
    }
484 485

    /**
486
     * Caution: Can be only called on raw data (before `this._indices` created).
487
     */
488 489 490 491
    appendData(data: ArrayLike<any>): void {
        if (__DEV__) {
            zrUtil.assert(!this._indices, 'appendData can only be called on raw data.');
        }
S
sushuang 已提交
492

493 494
        const rawData = this._rawData;
        const start = this.count();
495
        rawData.appendData(data);
496
        let end = rawData.count();
497 498 499 500
        if (!rawData.persistent) {
            end += start;
        }
        this._initDataFromProvider(start, end);
501
    }
502

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
    /**
     * Caution: Can be only called on raw data (before `this._indices` created).
     * This method does not modify `rawData` (`dataProvider`), but only
     * add values to storage.
     *
     * The final count will be increased by `Math.max(values.length, names.length)`.
     *
     * @param values That is the SourceType: 'arrayRows', like
     *        [
     *            [12, 33, 44],
     *            [NaN, 43, 1],
     *            ['-', 'asdf', 0]
     *        ]
     *        Each item is exaclty cooresponding to a dimension.
     */
    appendValues(values: any[][], names?: string[]): void {
519 520
        const chunkSize = this._chunkSize;
        const storage = this._storage;
521
        const storageArr = this._storageArr;
522 523 524
        const dimensions = this.dimensions;
        const dimLen = dimensions.length;
        const rawExtent = this._rawExtent;
525

526 527 528
        const start = this.count();
        const end = start + Math.max(values.length, names ? names.length : 0);
        const originalChunkCount = this._chunkCount;
529

530
        for (let i = 0; i < dimLen; i++) {
531
            const dim = dimensions[i];
532 533 534 535
            if (!rawExtent[dim]) {
                rawExtent[dim] = getInitialExtent();
            }
            if (!storage[dim]) {
536 537 538
                const store: DataValueChunk[] = [];
                storage[dim] = store;
                storageArr.push(store);
539 540 541
            }
            prepareChunks(storage, this._dimensionInfos[dim], chunkSize, originalChunkCount, end);
            this._chunkCount = storage[dim].length;
S
sushuang 已提交
542
        }
543

544 545 546 547
        const rawExtentArr = zrUtil.map(dimensions, (dim) => {
            return rawExtent[dim];
        });

548
        const emptyDataItem = new Array(dimLen);
549
        for (let idx = start; idx < end; idx++) {
550
            const sourceIdx = idx - start;
551
            const chunkIndex = mathFloor(idx / chunkSize);
552
            const chunkOffset = idx % chunkSize;
553 554

            // Store the data by dimensions
555 556
            for (let dimIdx = 0; dimIdx < dimLen; dimIdx++) {
                const dim = dimensions[dimIdx];
557
                const val = this._dimValueGetterArrayRows(
558
                    values[sourceIdx] || emptyDataItem, dim, sourceIdx, dimIdx
559
                ) as ParsedValueNumeric;
560
                storageArr[dimIdx][chunkIndex][chunkOffset] = val;
561

562
                const dimRawExtent = rawExtentArr[dimIdx];
563 564 565
                val < dimRawExtent[0] && (dimRawExtent[0] = val);
                val > dimRawExtent[1] && (dimRawExtent[1] = val);
            }
566

567 568 569
            if (names) {
                this._nameList[idx] = names[sourceIdx];
            }
570 571
        }

572
        this._rawCount = this._count = end;
573

574 575
        // Reset data extent
        this._extent = {};
576

577 578
        prepareInvertedIndex(this);
    }
579

580 581 582 583
    private _initDataFromProvider(start: number, end: number): void {
        if (start >= end) {
            return;
        }
584

585 586 587
        const chunkSize = this._chunkSize;
        const rawData = this._rawData;
        const storage = this._storage;
588
        const storageArr = this._storageArr;
589 590 591 592 593 594 595
        const dimensions = this.dimensions;
        const dimLen = dimensions.length;
        const dimensionInfoMap = this._dimensionInfos;
        const nameList = this._nameList;
        const idList = this._idList;
        const rawExtent = this._rawExtent;
        const nameRepeatCount: NameRepeatCount = this._nameRepeatCount = {};
596 597
        let nameDimIdx;

598
        const originalChunkCount = this._chunkCount;
599

600
        for (let i = 0; i < dimLen; i++) {
601
            const dim = dimensions[i];
602 603 604
            if (!rawExtent[dim]) {
                rawExtent[dim] = getInitialExtent();
            }
S
sushuang 已提交
605

606
            const dimInfo = dimensionInfoMap[dim];
607 608 609 610 611 612
            if (dimInfo.otherDims.itemName === 0) {
                nameDimIdx = this._nameDimIdx = i;
            }
            if (dimInfo.otherDims.itemId === 0) {
                this._idDimIdx = i;
            }
S
sushuang 已提交
613

614
            if (!storage[dim]) {
615 616 617
                const store: DataValueChunk[] = [];
                storage[dim] = store;
                storageArr.push(store);
618
            }
S
tweak  
sushuang 已提交
619

620
            prepareChunks(storage, dimInfo, chunkSize, originalChunkCount, end);
621

622
            this._chunkCount = storage[dim].length;
S
sushuang 已提交
623
        }
624

625 626 627
        const rawExtentArr = zrUtil.map(dimensions, (dim) => {
            return rawExtent[dim];
        });
628

P
pissang 已提交
629
        let dataItem = [] as OptionDataItem;
630
        for (let idx = start; idx < end; idx++) {
631 632 633 634 635 636 637 638
            // NOTICE: Try not to write things into dataItem
            dataItem = rawData.getItem(idx, dataItem);
            // Each data item is value
            // [1, 2]
            // 2
            // Bar chart, line chart which uses category axis
            // only gives the 'y' value. 'x' value is the indices of category
            // Use a tempValue to normalize the value to be a (x, y) value
639
            const chunkIndex = mathFloor(idx / chunkSize);
640
            const chunkOffset = idx % chunkSize;
641 642

            // Store the data by dimensions
643 644 645
            for (let dimIdx = 0; dimIdx < dimLen; dimIdx++) {
                const dim = dimensions[dimIdx];
                const dimStorage = storageArr[dimIdx][chunkIndex];
646
                // PENDING NULL is empty or zero
647
                const val = this._dimValueGetter(dataItem, dim, idx, dimIdx) as ParsedValueNumeric;
648 649
                dimStorage[chunkOffset] = val;

650
                const dimRawExtent = rawExtentArr[dimIdx];
651 652 653
                val < dimRawExtent[0] && (dimRawExtent[0] = val);
                val > dimRawExtent[1] && (dimRawExtent[1] = val);
            }
S
sushuang 已提交
654

655 656 657
            // ??? FIXME not check by pure but sourceFormat?
            // TODO refactor these logic.
            if (!rawData.pure) {
658
                let name: string = nameList[idx];
659 660 661 662 663 664 665

                if (dataItem && name == null) {
                    // If dataItem is {name: ...}, it has highest priority.
                    // That is appropriate for many common cases.
                    if ((dataItem as any).name != null) {
                        // There is no other place to persistent dataItem.name,
                        // so save it to nameList.
666
                        nameList[idx] = name = convertOptionIdName((dataItem as any).name, null);
667 668
                    }
                    else if (nameDimIdx != null) {
669 670
                        const nameDim = dimensions[nameDimIdx];
                        const nameDimChunk = storage[nameDim][chunkIndex];
671
                        if (nameDimChunk) {
672
                            const ordinalMeta = dimensionInfoMap[nameDim].ordinalMeta;
673 674 675 676 677 678
                            name = convertOptionIdName(
                                (ordinalMeta && ordinalMeta.categories.length)
                                    ? ordinalMeta.categories[nameDimChunk[chunkOffset] as number]
                                    : nameDimChunk[chunkOffset],
                                null
                            );
679 680 681
                        }
                    }
                }
S
sushuang 已提交
682

683 684
                // Try using the id in option
                // id or name is used on dynamical data, mapping old and new items.
685
                let id: string = dataItem == null ? null : convertOptionIdName((dataItem as any).id, null);
S
sushuang 已提交
686

687 688 689 690 691 692 693 694
                if (id == null && name != null) {
                    // Use name as id and add counter to avoid same name
                    nameRepeatCount[name] = nameRepeatCount[name] || 0;
                    id = name;
                    if (nameRepeatCount[name] > 0) {
                        id += '__ec__' + nameRepeatCount[name];
                    }
                    nameRepeatCount[name]++;
S
sushuang 已提交
695
                }
696
                id != null && (idList[idx] = id);
S
sushuang 已提交
697 698
            }
        }
S
sushuang 已提交
699

700 701 702 703
        if (!rawData.persistent && rawData.clean) {
            // Clean unused data if data source is typed array.
            rawData.clean();
        }
P
pissang 已提交
704

705
        this._rawCount = this._count = end;
S
sushuang 已提交
706

707 708
        // Reset data extent
        this._extent = {};
S
tweak  
sushuang 已提交
709

710
        prepareInvertedIndex(this);
711 712
    }

713 714
    count(): number {
        return this._count;
715 716
    }

717
    getIndices(): ArrayLike<number> {
718
        let newIndices;
S
sushuang 已提交
719

720
        const indices = this._indices;
721
        if (indices) {
722 723
            const Ctor = indices.constructor as DataArrayLikeConstructor;
            const thisCount = this._count;
724 725 726
            // `new Array(a, b, c)` is different from `new Uint32Array(a, b, c)`.
            if (Ctor === Array) {
                newIndices = new Ctor(thisCount);
1
100pah 已提交
727
                for (let i = 0; i < thisCount; i++) {
728 729
                    newIndices[i] = indices[i];
                }
S
sushuang 已提交
730
            }
731 732 733 734
            else {
                newIndices = new (Ctor as DataTypedArrayConstructor)(
                    (indices as DataTypedArray).buffer, 0, thisCount
                );
S
sushuang 已提交
735 736
            }
        }
737
        else {
738
            const Ctor = getIndicesCtor(this);
739
            newIndices = new Ctor(this.count());
1
100pah 已提交
740
            for (let i = 0; i < newIndices.length; i++) {
741
                newIndices[i] = i;
742
            }
743 744
        }

745 746
        return newIndices;
    }
747

748 749 750 751
    // Get data by index of dimension.
    // Because in v8 access array by number variable is faster than access object by string variable
    // Not sure why but the optimization just works.
    getByDimIdx(dimIdx: number, idx: number): ParsedValue {
752 753 754 755
        if (!(idx >= 0 && idx < this._count)) {
            return NaN;
        }

756 757 758 759 760 761 762 763 764 765 766 767 768
        const dimStore = this._storageArr[dimIdx];
        const chunkSize = this._chunkSize;
        if (!dimStore) {
            return NaN;
        }
        idx = this.getRawIndex(idx);

        const chunkIndex = mathFloor(idx / chunkSize);
        const chunkOffset = idx % chunkSize;

        return dimStore[chunkIndex][chunkOffset];
    }

769 770 771 772
    /**
     * Get value. Return NaN if idx is out of range.
     * @param dim Dim must be concrete name.
     */
773
    get(dim: DimensionName, idx: number): ParsedValue {
774 775
        if (!(idx >= 0 && idx < this._count)) {
            return NaN;
S
sushuang 已提交
776
        }
777 778 779
        const dimStore = this._storage[dim];
        const chunkSize = this._chunkSize;
        if (!dimStore) {
780
            return NaN;
S
sushuang 已提交
781
        }
782 783 784

        idx = this.getRawIndex(idx);

785 786
        const chunkIndex = mathFloor(idx / chunkSize);
        const chunkOffset = idx % chunkSize;
787

788
        return dimStore[chunkIndex][chunkOffset];
789
    }
S
sushuang 已提交
790

791 792 793
    /**
     * @param dim concrete dim
     */
794
    getByRawIndex(dim: DimensionName, rawIdx: number): ParsedValue {
795 796 797
        if (!(rawIdx >= 0 && rawIdx < this._rawCount)) {
            return NaN;
        }
798
        const dimStore = this._storage[dim];
799
        const chunkSize = this._chunkSize;
800 801 802 803
        if (!dimStore) {
            // TODO Warn ?
            return NaN;
        }
S
sushuang 已提交
804

805 806
        const chunkIndex = mathFloor(rawIdx / chunkSize);
        const chunkOffset = rawIdx % chunkSize;
807
        const chunkStore = dimStore[chunkIndex];
808
        return chunkStore[chunkOffset];
P
pissang 已提交
809
    }
810 811 812 813 814

    /**
     * FIXME Use `get` on chrome maybe slow(in filterSelf and selectRange).
     * Hack a much simpler _getFast
     */
P
pissang 已提交
815
    private _getFast(dimIdx: number, rawIdx: number): ParsedValue {
816 817 818
        const chunkSize = this._chunkSize;
        const chunkIndex = mathFloor(rawIdx / chunkSize);
        const chunkOffset = rawIdx % chunkSize;
P
pissang 已提交
819
        return this._storageArr[dimIdx][chunkIndex][chunkOffset];
820
    }
P
pissang 已提交
821

822 823 824 825
    /**
     * Get value for multi dimensions.
     * @param dimensions If ignored, using all dimensions.
     */
826
    getValues(idx: number): ParsedValue[];
827 828
    getValues(dimensions: readonly DimensionName[], idx: number): ParsedValue[];
    getValues(dimensions: readonly DimensionName[] | number, idx?: number): ParsedValue[] {
829
        const values = [];
830 831 832

        if (!zrUtil.isArray(dimensions)) {
            // stack = idx;
833
            idx = dimensions as number;
834 835
            dimensions = this.dimensions;
        }
P
pissang 已提交
836

837
        for (let i = 0, len = dimensions.length; i < len; i++) {
838 839
            values.push(this.get(dimensions[i], idx /*, stack */));
        }
S
sushuang 已提交
840

841
        return values;
S
sushuang 已提交
842
    }
843 844 845 846 847 848

    /**
     * If value is NaN. Inlcuding '-'
     * Only check the coord dimensions.
     */
    hasValue(idx: number): boolean {
849
        const dataDimsOnCoord = this._dimensionsSummary.dataDimsOnCoord;
850
        for (let i = 0, len = dataDimsOnCoord.length; i < len; i++) {
851 852 853 854 855 856 857 858
            // Ordinal type originally can be string or number.
            // But when an ordinal type is used on coord, it can
            // not be string but only number. So we can also use isNaN.
            if (isNaN(this.get(dataDimsOnCoord[i], idx) as any)) {
                return false;
            }
        }
        return true;
S
sushuang 已提交
859 860
    }

861 862 863 864 865 866
    /**
     * Get extent of data in one dimension
     */
    getDataExtent(dim: DimensionLoose): [number, number] {
        // Make sure use concrete dim as cache name.
        dim = this.getDimension(dim);
867 868
        const dimData = this._storage[dim];
        const initialExtent = getInitialExtent();
P
pissang 已提交
869
        const chunkSize = this._chunkSize;
S
sushuang 已提交
870

871
        // stack = !!((stack || false) && this.getCalculationInfo(dim));
P
pissang 已提交
872

873 874 875
        if (!dimData) {
            return initialExtent;
        }
S
sushuang 已提交
876

877
        // Make more strict checkings to ensure hitting cache.
878
        const currEnd = this.count();
879 880
        // let cacheName = [dim, !!stack].join('_');
        // let cacheName = dim;
S
sushuang 已提交
881

882 883 884
        // Consider the most cases when using data zoom, `getDataExtent`
        // happened before filtering. We cache raw extent, which is not
        // necessary to be cleared and recalculated when restore data.
885
        const useRaw = !this._indices; // && !stack;
886
        let dimExtent: [number, number];
S
sushuang 已提交
887

888 889 890 891 892 893 894 895
        if (useRaw) {
            return this._rawExtent[dim].slice() as [number, number];
        }
        dimExtent = this._extent[dim];
        if (dimExtent) {
            return dimExtent.slice() as [number, number];
        }
        dimExtent = initialExtent;
S
sushuang 已提交
896

897 898
        let min = dimExtent[0];
        let max = dimExtent[1];
899

900
        for (let i = 0; i < currEnd; i++) {
P
pissang 已提交
901 902 903 904
            const rawIdx = this.getRawIndex(i);
            const chunkIndex = mathFloor(rawIdx / chunkSize);
            const chunkOffset = rawIdx % chunkSize;
            const value = dimData[chunkIndex][chunkOffset] as ParsedValueNumeric;
905 906
            value < min && (min = value);
            value > max && (max = value);
L
lang 已提交
907
        }
L
lang 已提交
908

909
        dimExtent = [min, max];
S
sushuang 已提交
910

911
        this._extent[dim] = dimExtent;
P
pissang 已提交
912

913
        return dimExtent;
S
sushuang 已提交
914
    }
S
sushuang 已提交
915

916
    /**
917 918 919 920 921 922 923 924
     * PENDING: In fact currently this function is only used to short-circuit
     * the calling of `scale.unionExtentFromData` when data have been filtered by modules
     * like "dataZoom". `scale.unionExtentFromData` is used to calculate data extent for series on
     * an axis, but if a "axis related data filter module" is used, the extent of the axis have
     * been fixed and no need to calling `scale.unionExtentFromData` actually.
     * But if we add "custom data filter" in future, which is not "axis related", this method may
     * be still needed.
     *
925 926 927 928 929 930 931
     * Optimize for the scenario that data is filtered by a given extent.
     * Consider that if data amount is more than hundreds of thousand,
     * extent calculation will cost more than 10ms and the cache will
     * be erased because of the filtering.
     */
    getApproximateExtent(dim: DimensionLoose): [number, number] {
        dim = this.getDimension(dim);
932
        return this._approximateExtent[dim] || this.getDataExtent(dim);
933
    }
S
sushuang 已提交
934

935 936 937 938
    /**
     * Calculate extent on a filtered data might be time consuming.
     * Approximate extent is only used for: calculte extent of filtered data outside.
     */
939 940 941
    setApproximateExtent(extent: [number, number], dim: DimensionLoose): void {
        dim = this.getDimension(dim);
        this._approximateExtent[dim] = extent.slice() as [number, number];
S
sushuang 已提交
942
    }
943

944 945 946
    getCalculationInfo<CALC_INFO_KEY extends keyof DataCalculationInfo<HostModel>>(
        key: CALC_INFO_KEY
    ): DataCalculationInfo<HostModel>[CALC_INFO_KEY] {
947
        return this._calculationInfo[key];
S
sushuang 已提交
948 949
    }

950 951 952
    /**
     * @param key or k-v object
     */
953 954 955 956 957 958 959 960 961 962 963
    setCalculationInfo(
        key: DataCalculationInfo<HostModel>
    ): void;
    setCalculationInfo<CALC_INFO_KEY extends keyof DataCalculationInfo<HostModel>>(
        key: CALC_INFO_KEY,
        value: DataCalculationInfo<HostModel>[CALC_INFO_KEY]
    ): void;
    setCalculationInfo(
        key: (keyof DataCalculationInfo<HostModel>) | DataCalculationInfo<HostModel>,
        value?: DataCalculationInfo<HostModel>[keyof DataCalculationInfo<HostModel>]
    ): void {
964 965
        isObject(key)
            ? zrUtil.extend(this._calculationInfo, key as object)
966
            : ((this._calculationInfo as any)[key] = value);
967
    }
S
sushuang 已提交
968

969 970 971 972
    /**
     * Get sum of data in one dimension
     */
    getSum(dim: DimensionName): number {
973
        const dimData = this._storage[dim];
974
        let sum = 0;
975
        if (dimData) {
976
            for (let i = 0, len = this.count(); i < len; i++) {
977
                const value = this.get(dim, i) as number;
978 979 980 981 982 983
                if (!isNaN(value)) {
                    sum += value;
                }
            }
        }
        return sum;
S
sushuang 已提交
984
    }
S
sushuang 已提交
985

986 987 988 989
    /**
     * Get median of data in one dimension
     */
    getMedian(dim: DimensionLoose): number {
990
        const dimDataArray: ParsedValue[] = [];
991 992 993 994 995 996 997 998 999
        // map all data of one dimension
        this.each(dim, function (val) {
            if (!isNaN(val as number)) {
                dimDataArray.push(val);
            }
        });

        // TODO
        // Use quick select?
1000
        const sortedDimDataArray = dimDataArray.sort(function (a: number, b: number) {
1001
            return a - b;
1002
        }) as number[];
1003
        const len = this.count();
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        // calculate median
        return len === 0
            ? 0
            : len % 2 === 1
            ? sortedDimDataArray[(len - 1) / 2]
            : (sortedDimDataArray[len / 2] + sortedDimDataArray[len / 2 - 1]) / 2;
    }

    // /**
    //  * Retreive the index with given value
    //  * @param {string} dim Concrete dimension.
    //  * @param {number} value
    //  * @return {number}
    //  */
    // Currently incorrect: should return dataIndex but not rawIndex.
    // Do not fix it until this method is to be used somewhere.
    // FIXME Precision of float value
    // indexOf(dim, value) {
1022 1023 1024
    //     let storage = this._storage;
    //     let dimData = storage[dim];
    //     let chunkSize = this._chunkSize;
1025
    //     if (dimData) {
1026
    //         for (let i = 0, len = this.count(); i < len; i++) {
1027
    //             let chunkIndex = mathFloor(i / chunkSize);
1028
    //             let chunkOffset = i % chunkSize;
1029 1030 1031 1032 1033 1034 1035
    //             if (dimData[chunkIndex][chunkOffset] === value) {
    //                 return i;
    //             }
    //         }
    //     }
    //     return -1;
    // }
S
sushuang 已提交
1036

1037 1038 1039 1040 1041 1042 1043
    /**
     * Only support the dimension which inverted index created.
     * Do not support other cases until required.
     * @param dim concrete dim
     * @param value ordinal index
     * @return rawIndex
     */
1
100pah 已提交
1044
    rawIndexOf(dim: DimensionName, value: OrdinalNumber): number {
1045
        const invertedIndices = dim && this._invertedIndicesMap[dim];
1046 1047 1048 1049 1050
        if (__DEV__) {
            if (!invertedIndices) {
                throw new Error('Do not supported yet');
            }
        }
1051
        const rawIndex = invertedIndices[value];
1052 1053 1054 1055 1056
        if (rawIndex == null || isNaN(rawIndex)) {
            return INDEX_NOT_FOUND;
        }
        return rawIndex;
    }
S
sushuang 已提交
1057

1058 1059 1060 1061
    /**
     * Retreive the index with given name
     */
    indexOfName(name: string): number {
1062
        for (let i = 0, len = this.count(); i < len; i++) {
1063 1064
            if (this.getName(i) === name) {
                return i;
L
lang 已提交
1065 1066
            }
        }
H
hustcc 已提交
1067

1068 1069
        return -1;
    }
S
sushuang 已提交
1070

1071 1072 1073 1074 1075 1076
    /**
     * Retreive the index with given raw data index
     */
    indexOfRawIndex(rawIndex: number): number {
        if (rawIndex >= this._rawCount || rawIndex < 0) {
            return -1;
L
lang 已提交
1077 1078
        }

1079 1080
        if (!this._indices) {
            return rawIndex;
1081 1082
        }

1083
        // Indices are ascending
1084
        const indices = this._indices;
L
lang 已提交
1085

1086
        // If rawIndex === dataIndex
1087
        const rawDataIndex = indices[rawIndex];
1088 1089 1090 1091
        if (rawDataIndex != null && rawDataIndex < this._count && rawDataIndex === rawIndex) {
            return rawIndex;
        }

1092 1093
        let left = 0;
        let right = this._count - 1;
1094
        while (left <= right) {
1095
            const mid = (left + right) / 2 | 0;
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
            if (indices[mid] < rawIndex) {
                left = mid + 1;
            }
            else if (indices[mid] > rawIndex) {
                right = mid - 1;
            }
            else {
                return mid;
            }
        }
1106 1107 1108
        return -1;
    }

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    /**
     * Retreive the index of nearest value
     * @param dim
     * @param value
     * @param [maxDistance=Infinity]
     * @return If and only if multiple indices has
     *         the same value, they are put to the result.
     */
    indicesOfNearest(
        dim: DimensionName, value: number, maxDistance?: number
    ): number[] {
1120 1121 1122
        const storage = this._storage;
        const dimData = storage[dim];
        const nearestIndices: number[] = [];
1123
        const chunkSize = this._chunkSize;
1124 1125 1126 1127

        if (!dimData) {
            return nearestIndices;
        }
1128

1129 1130 1131
        if (maxDistance == null) {
            maxDistance = Infinity;
        }
S
sushuang 已提交
1132

1133 1134 1135
        let minDist = Infinity;
        let minDiff = -1;
        let nearestIndicesLen = 0;
1136

1137

1138
        // Check the test case of `test/ut/spec/data/List.js`.
1139
        for (let i = 0, len = this.count(); i < len; i++) {
1140 1141 1142
            const chunkIndex = mathFloor(i / chunkSize);
            const chunkOffset = i % chunkSize;
            const diff = value - (dimData[chunkIndex][chunkOffset] as number);
1143
            const dist = Math.abs(diff);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
            if (dist <= maxDistance) {
                // When the `value` is at the middle of `this.get(dim, i)` and `this.get(dim, i+1)`,
                // we'd better not push both of them to `nearestIndices`, otherwise it is easy to
                // get more than one item in `nearestIndices` (more specifically, in `tooltip`).
                // So we chose the one that `diff >= 0` in this csae.
                // But if `this.get(dim, i)` and `this.get(dim, j)` get the same value, both of them
                // should be push to `nearestIndices`.
                if (dist < minDist
                    || (dist === minDist && diff >= 0 && minDiff < 0)
                ) {
                    minDist = dist;
                    minDiff = diff;
                    nearestIndicesLen = 0;
                }
                if (diff === minDiff) {
                    nearestIndices[nearestIndicesLen++] = i;
                }
            }
L
lang 已提交
1162
        }
1163
        nearestIndices.length = nearestIndicesLen;
L
lang 已提交
1164

S
sushuang 已提交
1165 1166
        return nearestIndices;
    }
L
lang 已提交
1167

1168 1169 1170 1171 1172 1173
    /**
     * Get raw data index.
     * Do not initialize.
     * Default `getRawIndex`. And it can be changed.
     */
    getRawIndex: (idx: number) => number = getRawIndexWithoutIndices;
L
lang 已提交
1174

1175 1176 1177 1178 1179
    /**
     * Get raw data item
     */
    getRawDataItem(idx: number): OptionDataItem {
        if (!this._rawData.persistent) {
1180
            const val = [];
1181
            for (let i = 0; i < this.dimensions.length; i++) {
1182
                const dim = this.dimensions[i];
1183
                val.push(this.get(dim, idx));
S
sushuang 已提交
1184
            }
1185 1186 1187 1188
            return val;
        }
        else {
            return this._rawData.getItem(this.getRawIndex(idx));
L
lang 已提交
1189 1190
        }
    }
P
pissang 已提交
1191

1192 1193 1194 1195 1196 1197
    /**
     * @return Never be null/undefined. `number` will be converted to string. Becuase:
     * In most cases, name is used in display, where returning a string is more convenient.
     * In other cases, name is used in query (see `indexOfName`), where we can keep the
     * rule that name `2` equals to name `'2'`.
     */
1198
    getName(idx: number): string {
1199
        const rawIndex = this.getRawIndex(idx);
1200
        return this._nameList[rawIndex]
1201
            || convertOptionIdName(getRawValueFromStore(this, this._nameDimIdx, rawIndex), '')
1202 1203
            || '';
    }
P
pissang 已提交
1204

1205 1206 1207 1208 1209 1210
    /**
     * @return Never null/undefined. `number` will be converted to string. Becuase:
     * In all cases having encountered at present, id is used in making diff comparison, which
     * are usually based on hash map. We can keep the rule that the internal id are always string
     * (treat `2` is the same as `'2'`) to make the related logic simple.
     */
1211 1212
    getId(idx: number): string {
        return getId(this, this.getRawIndex(idx));
1213
    }
L
lang 已提交
1214

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
    /**
     * Data iteration
     * @param ctx default this
     * @example
     *  list.each('x', function (x, idx) {});
     *  list.each(['x', 'y'], function (x, y, idx) {});
     *  list.each(function (idx) {})
     */
    each<Ctx>(cb: EachCb0<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): void;
    each<Ctx>(dims: DimensionLoose, cb: EachCb1<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): void;
    each<Ctx>(dims: [DimensionLoose], cb: EachCb1<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): void;
    each<Ctx>(dims: [DimensionLoose, DimensionLoose], cb: EachCb2<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): void;
    each<Ctx>(dims: ItrParamDims, cb: EachCb<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): void;
    each<Ctx>(
        dims: ItrParamDims | EachCb<Ctx>,
        cb: EachCb<Ctx> | Ctx,
        ctx?: Ctx,
        ctxCompat?: Ctx
    ): void {
        'use strict';

        if (!this._count) {
            return;
1238
        }
L
lang 已提交
1239

1240 1241 1242 1243 1244 1245
        if (typeof dims === 'function') {
            ctxCompat = ctx;
            ctx = cb as Ctx;
            cb = dims;
            dims = [];
        }
L
lang 已提交
1246

1247
        // ctxCompat just for compat echarts3
1248
        const fCtx = (ctx || ctxCompat || this) as CtxOrList<Ctx>;
L
lang 已提交
1249

1250
        const dimNames = zrUtil.map(normalizeDimensions(dims), this.getDimension, this);
L
lang 已提交
1251

1252 1253 1254
        if (__DEV__) {
            validateDimensions(this, dimNames);
        }
L
lang 已提交
1255

1256
        const dimSize = dimNames.length;
P
pissang 已提交
1257 1258 1259
        const dimIndices = zrUtil.map(dimNames, (dimName) => {
            return this._dimensionInfos[dimName].index;
        });
1260

1261
        for (let i = 0; i < this.count(); i++) {
1262 1263 1264 1265 1266 1267
            // Simple optimization
            switch (dimSize) {
                case 0:
                    (cb as EachCb0<Ctx>).call(fCtx, i);
                    break;
                case 1:
P
pissang 已提交
1268
                    (cb as EachCb1<Ctx>).call(fCtx, this._getFast(dimIndices[0], i), i);
1269 1270
                    break;
                case 2:
P
pissang 已提交
1271 1272 1273
                    (cb as EachCb2<Ctx>).call(
                        fCtx, this._getFast(dimIndices[0], i), this._getFast(dimIndices[1], i), i
                    );
1274 1275
                    break;
                default:
1276
                    let k = 0;
1277
                    const value = [];
1278
                    for (; k < dimSize; k++) {
P
pissang 已提交
1279
                        value[k] = this._getFast(dimIndices[k], i);
1280 1281 1282 1283 1284
                    }
                    // Index
                    value[k] = i;
                    (cb as EachCb<Ctx>).apply(fCtx, value);
            }
1285 1286 1287
        }
    }

1288 1289 1290
    /**
     * Data filter
     */
P
pissang 已提交
1291 1292 1293 1294 1295
    filterSelf<Ctx>(cb: FilterCb0<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): this;
    filterSelf<Ctx>(dims: DimensionLoose, cb: FilterCb1<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): this;
    filterSelf<Ctx>(dims: [DimensionLoose], cb: FilterCb1<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): this;
    filterSelf<Ctx>(dims: [DimensionLoose, DimensionLoose], cb: FilterCb2<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): this;
    filterSelf<Ctx>(dims: ItrParamDims, cb: FilterCb<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): this;
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    filterSelf<Ctx>(
        dims: ItrParamDims | FilterCb<Ctx>,
        cb: FilterCb<Ctx> | Ctx,
        ctx?: Ctx,
        ctxCompat?: Ctx
    ): List {
        'use strict';

        if (!this._count) {
            return;
        }
1307

1308 1309 1310 1311 1312 1313
        if (typeof dims === 'function') {
            ctxCompat = ctx;
            ctx = cb as Ctx;
            cb = dims;
            dims = [];
        }
P
pissang 已提交
1314

1315
        // ctxCompat just for compat echarts3
1316
        const fCtx = (ctx || ctxCompat || this) as CtxOrList<Ctx>;
S
sushuang 已提交
1317

1318
        const dimNames = zrUtil.map(
1319 1320
            normalizeDimensions(dims), this.getDimension, this
        );
S
sushuang 已提交
1321

1322 1323 1324
        if (__DEV__) {
            validateDimensions(this, dimNames);
        }
S
sushuang 已提交
1325

1326

1327 1328 1329 1330 1331
        const count = this.count();
        const Ctor = getIndicesCtor(this);
        const newIndices = new Ctor(count);
        const value = [];
        const dimSize = dimNames.length;
1332

1333
        let offset = 0;
P
pissang 已提交
1334 1335 1336 1337
        const dimIndices = zrUtil.map(dimNames, (dimName) => {
            return this._dimensionInfos[dimName].index;
        });
        const dim0 = dimIndices[0];
1338

1339 1340
        for (let i = 0; i < count; i++) {
            let keep;
1341
            const rawIdx = this.getRawIndex(i);
1342 1343 1344 1345 1346
            // Simple optimization
            if (dimSize === 0) {
                keep = (cb as FilterCb0<Ctx>).call(fCtx, i);
            }
            else if (dimSize === 1) {
1347
                const val = this._getFast(dim0, rawIdx);
1348 1349 1350
                keep = (cb as FilterCb1<Ctx>).call(fCtx, val, i);
            }
            else {
1
100pah 已提交
1351 1352
                let k = 0;
                for (; k < dimSize; k++) {
P
pissang 已提交
1353
                    value[k] = this._getFast(dimIndices[k], rawIdx);
L
lang 已提交
1354 1355
                }
                value[k] = i;
1356 1357 1358 1359 1360
                keep = (cb as FilterCb<Ctx>).apply(fCtx, value);
            }
            if (keep) {
                newIndices[offset++] = rawIdx;
            }
L
lang 已提交
1361
        }
P
pissang 已提交
1362

1363 1364 1365 1366 1367 1368 1369
        // Set indices after filtered.
        if (offset < count) {
            this._indices = newIndices;
        }
        this._count = offset;
        // Reset data extent
        this._extent = {};
P
pissang 已提交
1370

1371
        this.getRawIndex = this._indices ? getRawIndexWithIndices : getRawIndexWithoutIndices;
S
tweak.  
sushuang 已提交
1372

1373
        return this;
1374 1375
    }

1376 1377 1378 1379
    /**
     * Select data in range. (For optimization of filter)
     * (Manually inline code, support 5 million data filtering in data zoom.)
     */
P
pissang 已提交
1380
    selectRange(range: {[dimName: string]: [number, number]}): List {
1381
        'use strict';
1382

1383 1384
        if (!this._count) {
            return;
S
tweak.  
sushuang 已提交
1385
        }
1386

1387 1388
        const dimensions = [];
        for (const dim in range) {
1389 1390
            if (range.hasOwnProperty(dim)) {
                dimensions.push(dim);
S
tweak.  
sushuang 已提交
1391 1392
            }
        }
P
pissang 已提交
1393

1394 1395
        if (__DEV__) {
            validateDimensions(this, dimensions);
S
sushuang 已提交
1396
        }
1397

1398
        const dimSize = dimensions.length;
1399 1400 1401
        if (!dimSize) {
            return;
        }
P
pissang 已提交
1402

1403 1404 1405
        const originalCount = this.count();
        const Ctor = getIndicesCtor(this);
        const newIndices = new Ctor(originalCount);
1406

1407
        let offset = 0;
1408
        const dim0 = dimensions[0];
P
pissang 已提交
1409 1410 1411
        const dimIndices = zrUtil.map(dimensions, (dimName) => {
            return this._dimensionInfos[dimName].index;
        });
1412

1413 1414
        const min = range[dim0][0];
        const max = range[dim0][1];
1415

1416
        let quickFinished = false;
1417 1418
        if (!this._indices) {
            // Extreme optimization for common case. About 2x faster in chrome.
1419
            let idx = 0;
1420
            if (dimSize === 1) {
P
pissang 已提交
1421
                const dimStorage = this._storage[dim0];
1
100pah 已提交
1422
                for (let k = 0; k < this._chunkCount; k++) {
1423 1424
                    const chunkStorage = dimStorage[k];
                    const len = Math.min(this._count - k * this._chunkSize, this._chunkSize);
1
100pah 已提交
1425
                    for (let i = 0; i < len; i++) {
1426
                        const val = chunkStorage[i];
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
                        // NaN will not be filtered. Consider the case, in line chart, empty
                        // value indicates the line should be broken. But for the case like
                        // scatter plot, a data item with empty value will not be rendered,
                        // but the axis extent may be effected if some other dim of the data
                        // item has value. Fortunately it is not a significant negative effect.
                        if (
                            (val >= min && val <= max) || isNaN(val as any)
                        ) {
                            newIndices[offset++] = idx;
                        }
                        idx++;
1438 1439
                    }
                }
1440
                quickFinished = true;
P
pissang 已提交
1441
            }
1442
            else if (dimSize === 2) {
1443 1444 1445 1446
                const dimStorage = this._storage[dim0];
                const dimStorage2 = this._storage[dimensions[1]];
                const min2 = range[dimensions[1]][0];
                const max2 = range[dimensions[1]][1];
1
100pah 已提交
1447
                for (let k = 0; k < this._chunkCount; k++) {
1448 1449 1450
                    const chunkStorage = dimStorage[k];
                    const chunkStorage2 = dimStorage2[k];
                    const len = Math.min(this._count - k * this._chunkSize, this._chunkSize);
1
100pah 已提交
1451
                    for (let i = 0; i < len; i++) {
1452 1453
                        const val = chunkStorage[i];
                        const val2 = chunkStorage2[i];
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
                        // Do not filter NaN, see comment above.
                        if ((
                                (val >= min && val <= max) || isNaN(val as any)
                            )
                            && (
                                (val2 >= min2 && val2 <= max2) || isNaN(val2 as any)
                            )
                        ) {
                            newIndices[offset++] = idx;
                        }
                        idx++;
P
pissang 已提交
1465
                    }
P
pissang 已提交
1466
                }
1467
                quickFinished = true;
P
pissang 已提交
1468 1469
            }
        }
1470 1471
        if (!quickFinished) {
            if (dimSize === 1) {
1
100pah 已提交
1472
                for (let i = 0; i < originalCount; i++) {
1473
                    const rawIndex = this.getRawIndex(i);
P
pissang 已提交
1474
                    const val = this._getFast(dimIndices[0], rawIndex);
1475
                    // Do not filter NaN, see comment above.
1476 1477 1478 1479
                    if (
                        (val >= min && val <= max) || isNaN(val as any)
                    ) {
                        newIndices[offset++] = rawIndex;
P
pissang 已提交
1480 1481
                    }
                }
1482 1483
            }
            else {
1
100pah 已提交
1484 1485
                for (let i = 0; i < originalCount; i++) {
                    let keep = true;
1486
                    const rawIndex = this.getRawIndex(i);
1
100pah 已提交
1487
                    for (let k = 0; k < dimSize; k++) {
1488
                        const dimk = dimensions[k];
P
pissang 已提交
1489
                        const val = this._getFast(dimIndices[k], rawIndex);
1490 1491 1492 1493 1494 1495 1496 1497
                        // Do not filter NaN, see comment above.
                        if (val < range[dimk][0] || val > range[dimk][1]) {
                            keep = false;
                        }
                    }
                    if (keep) {
                        newIndices[offset++] = this.getRawIndex(i);
                    }
P
pissang 已提交
1498 1499 1500 1501
                }
            }
        }

1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
        // Set indices after filtered.
        if (offset < originalCount) {
            this._indices = newIndices;
        }
        this._count = offset;
        // Reset data extent
        this._extent = {};

        this.getRawIndex = this._indices ? getRawIndexWithIndices : getRawIndexWithoutIndices;

        return this;
P
pissang 已提交
1513 1514
    }

1515 1516 1517
    /**
     * Data mapping to a plain array
     */
P
pissang 已提交
1518
    mapArray<Ctx, Cb extends MapArrayCb0<Ctx>>(cb: Cb, ctx?: Ctx, ctxCompat?: Ctx): ReturnType<Cb>[];
1519
    /* eslint-disable */
P
pissang 已提交
1520 1521 1522 1523
    mapArray<Ctx, Cb extends MapArrayCb1<Ctx>>(dims: DimensionLoose, cb: Cb, ctx?: Ctx, ctxCompat?: Ctx): ReturnType<Cb>[];
    mapArray<Ctx, Cb extends MapArrayCb1<Ctx>>(dims: [DimensionLoose], cb: Cb, ctx?: Ctx, ctxCompat?: Ctx): ReturnType<Cb>[];
    mapArray<Ctx, Cb extends MapArrayCb2<Ctx>>(dims: [DimensionLoose, DimensionLoose], cb: Cb, ctx?: Ctx, ctxCompat?: Ctx): ReturnType<Cb>[];
    mapArray<Ctx, Cb extends MapArrayCb<Ctx>>(dims: ItrParamDims, cb: Cb, ctx?: Ctx, ctxCompat?: Ctx): ReturnType<Cb>[];
1524
    /* eslint-enable */
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    mapArray<Ctx>(
        dims: ItrParamDims | MapArrayCb<Ctx>,
        cb: MapArrayCb<Ctx> | Ctx,
        ctx?: Ctx,
        ctxCompat?: Ctx
    ): any[] {
        'use strict';

        if (typeof dims === 'function') {
            ctxCompat = ctx;
            ctx = cb as Ctx;
            cb = dims;
            dims = [];
        }
P
pissang 已提交
1539

1540 1541
        // ctxCompat just for compat echarts3
        ctx = (ctx || ctxCompat || this) as Ctx;
P
pissang 已提交
1542

1543
        const result: any[] = [];
1544 1545 1546 1547
        this.each(dims, function () {
            result.push(cb && (cb as MapArrayCb<Ctx>).apply(this, arguments));
        }, ctx);
        return result;
S
sushuang 已提交
1548 1549
    }

1550 1551 1552
    /**
     * Data mapping to a new List with given dimensions
     */
P
pissang 已提交
1553 1554 1555
    map<Ctx>(dims: DimensionLoose, cb: MapCb1<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): List<HostModel>;
    map<Ctx>(dims: [DimensionLoose], cb: MapCb1<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): List<HostModel>;
    map<Ctx>(dims: [DimensionLoose, DimensionLoose], cb: MapCb2<Ctx>, ctx?: Ctx, ctxCompat?: Ctx): List<HostModel>;
1556 1557 1558 1559 1560 1561 1562 1563 1564
    map<Ctx>(
        dims: ItrParamDims,
        cb: MapCb<Ctx>,
        ctx?: Ctx,
        ctxCompat?: Ctx
    ): List {
        'use strict';

        // ctxCompat just for compat echarts3
1565
        const fCtx = (ctx || ctxCompat || this) as CtxOrList<Ctx>;
1566

1567
        const dimNames = zrUtil.map(
1568 1569 1570 1571 1572 1573 1574
            normalizeDimensions(dims), this.getDimension, this
        );

        if (__DEV__) {
            validateDimensions(this, dimNames);
        }

1575
        const list = cloneListForMapAndSample(this, dimNames);
S
sushuang 已提交
1576

1577 1578 1579 1580 1581
        // Following properties are all immutable.
        // So we can reference to the same value
        list._indices = this._indices;
        list.getRawIndex = list._indices ? getRawIndexWithIndices : getRawIndexWithoutIndices;

1582
        const storage = list._storage;
S
sushuang 已提交
1583

1584 1585 1586 1587 1588 1589
        const tmpRetValue = [];
        const chunkSize = this._chunkSize;
        const dimSize = dimNames.length;
        const dataCount = this.count();
        const values = [];
        const rawExtent = list._rawExtent;
1590

1591 1592
        for (let dataIndex = 0; dataIndex < dataCount; dataIndex++) {
            for (let dimIndex = 0; dimIndex < dimSize; dimIndex++) {
1593
                values[dimIndex] = this.get(dimNames[dimIndex], dataIndex);
S
sushuang 已提交
1594
            }
1595 1596
            values[dimSize] = dataIndex;

1597
            let retValue = cb && cb.apply(fCtx, values);
1598 1599 1600 1601 1602 1603 1604
            if (retValue != null) {
                // a number or string (in oridinal dimension)?
                if (typeof retValue !== 'object') {
                    tmpRetValue[0] = retValue;
                    retValue = tmpRetValue;
                }

1605
                const rawIndex = this.getRawIndex(dataIndex);
1606
                const chunkIndex = mathFloor(rawIndex / chunkSize);
1607
                const chunkOffset = rawIndex % chunkSize;
1608

1609
                for (let i = 0; i < retValue.length; i++) {
1610 1611 1612
                    const dim = dimNames[i];
                    const val = retValue[i];
                    const rawExtentOnDim = rawExtent[dim];
1613

1614
                    const dimStore = storage[dim];
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
                    if (dimStore) {
                        dimStore[chunkIndex][chunkOffset] = val;
                    }

                    if (val < rawExtentOnDim[0]) {
                        rawExtentOnDim[0] = val as number;
                    }
                    if (val > rawExtentOnDim[1]) {
                        rawExtentOnDim[1] = val as number;
                    }
                }
S
sushuang 已提交
1626
            }
S
sushuang 已提交
1627
        }
S
sushuang 已提交
1628

1629
        return list;
S
sushuang 已提交
1630 1631
    }

1632 1633 1634 1635 1636 1637 1638
    /**
     * Large data down sampling on given dimension
     * @param sampleIndex Sample index for name and id
     */
    downSample(
        dimension: DimensionName,
        rate: number,
1639 1640
        sampleValue: (frameValues: ArrayLike<ParsedValue>) => ParsedValueNumeric,
        sampleIndex: (frameValues: ArrayLike<ParsedValue>, value: ParsedValueNumeric) => number
P
pissang 已提交
1641
    ): List<HostModel> {
1642 1643
        const list = cloneListForMapAndSample(this, [dimension]);
        const targetStorage = list._storage;
1644

1645
        const frameValues = [];
1646
        let frameSize = mathFloor(1 / rate);
1647

1648 1649 1650 1651
        const dimStore = targetStorage[dimension];
        const len = this.count();
        const chunkSize = this._chunkSize;
        const rawExtentOnDim = list._rawExtent[dimension];
1652

1653
        const newIndices = new (getIndicesCtor(this))(len);
1654

1655 1656
        let offset = 0;
        for (let i = 0; i < len; i += frameSize) {
1657 1658 1659 1660 1661
            // Last frame
            if (frameSize > len - i) {
                frameSize = len - i;
                frameValues.length = frameSize;
            }
1662
            for (let k = 0; k < frameSize; k++) {
1663
                const dataIdx = this.getRawIndex(i + k);
1664
                const originalChunkIndex = mathFloor(dataIdx / chunkSize);
1665
                const originalChunkOffset = dataIdx % chunkSize;
1666 1667
                frameValues[k] = dimStore[originalChunkIndex][originalChunkOffset];
            }
1668 1669
            const value = sampleValue(frameValues);
            const sampleFrameIdx = this.getRawIndex(
1670 1671
                Math.min(i + sampleIndex(frameValues, value) || 0, len - 1)
            );
1672
            const sampleChunkIndex = mathFloor(sampleFrameIdx / chunkSize);
1673
            const sampleChunkOffset = sampleFrameIdx % chunkSize;
1674 1675
            // Only write value on the filtered data
            dimStore[sampleChunkIndex][sampleChunkOffset] = value;
S
sushuang 已提交
1676

1677 1678 1679 1680 1681 1682 1683 1684 1685
            if (value < rawExtentOnDim[0]) {
                rawExtentOnDim[0] = value;
            }
            if (value > rawExtentOnDim[1]) {
                rawExtentOnDim[1] = value;
            }

            newIndices[offset++] = sampleFrameIdx;
        }
1686

1687 1688
        list._count = offset;
        list._indices = newIndices;
S
sushuang 已提交
1689

1690
        list.getRawIndex = getRawIndexWithIndices;
S
sushuang 已提交
1691

P
pissang 已提交
1692
        return list as List<HostModel>;
1693 1694
    }

1695 1696 1697 1698
    /**
     * Large data down sampling using largest-triangle-three-buckets
     * @param {string} baseDimension
     * @param {string} valueDimension
1699
     * @param {number} rate
1700 1701
     */
    lttbDownSample(
1702 1703
        baseDimension: DimensionName,
        valueDimension: DimensionName,
1704
        rate: number
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
    ) {
        const list = cloneListForMapAndSample(this, [baseDimension, valueDimension]);
        const targetStorage = list._storage;
        const baseDimStore = targetStorage[baseDimension];
        const valueDimStore = targetStorage[valueDimension];
        const len = this.count();
        const chunkSize = this._chunkSize;
        const newIndices = new (getIndicesCtor(this))(len);

        let sampledIndex = 0;

1716
        const frameSize = mathFloor(1 / rate);
1717

1718
        let currentSelectedIdx = 0;
1719 1720
        let maxArea;
        let area;
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
        let nextSelectedIdx;

        for (let chunkIdx = 0; chunkIdx < this._chunkCount; chunkIdx++) {
            const chunkOffset = chunkSize * chunkIdx;
            const selfChunkSize = Math.min(len - chunkOffset, chunkSize);
            const chunkFrameCount = Math.ceil((selfChunkSize - 2) / frameSize);
            const baseDimChunk = baseDimStore[chunkIdx];
            const valueDimChunk = valueDimStore[chunkIdx];

            // The first frame is the first data.
            newIndices[sampledIndex++] = currentSelectedIdx;

            for (let frame = 0; frame < chunkFrameCount - 2; frame++) {
                let avgX = 0;
                let avgY = 0;
                let avgRangeStart = (frame + 1) * frameSize + 1 + chunkOffset;
                const avgRangeEnd = Math.min((frame + 2) * frameSize + 1, selfChunkSize) + chunkOffset;

                const avgRangeLength = avgRangeEnd - avgRangeStart;

                for (; avgRangeStart < avgRangeEnd; avgRangeStart++) {
                    const x = baseDimChunk[avgRangeStart] as number;
                    const y = valueDimChunk[avgRangeStart] as number;
                    if (isNaN(x) || isNaN(y)) {
                        continue;
                    }
                    avgX += x;
                    avgY += y;
                }
                avgX /= avgRangeLength;
                avgY /= avgRangeLength;
1752

1753 1754 1755
                // Get the range for this bucket
                let rangeOffs = (frame) * frameSize + 1 + chunkOffset;
                const rangeTo = (frame + 1) * frameSize + 1 + chunkOffset;
1756

1757 1758 1759 1760
                // Point A
                const pointAX = baseDimChunk[currentSelectedIdx] as number;
                const pointAY = valueDimChunk[currentSelectedIdx] as number;
                let allNaN = true;
1761

1762
                maxArea = area = -1;
1763

1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
                for (; rangeOffs < rangeTo; rangeOffs++) {
                    const y = valueDimChunk[rangeOffs] as number;
                    const x = baseDimChunk[rangeOffs] as number;
                    if (isNaN(x) || isNaN(y)) {
                        continue;
                    }
                    allNaN = false;
                    // Calculate triangle area over three buckets
                    area = Math.abs((pointAX - avgX) * (y - pointAY)
                                - (pointAX - x) * (avgY - pointAY)
                            );
                    if (area > maxArea) {
                        maxArea = area;
                        nextSelectedIdx = rangeOffs; // Next a is this b
                    }
1779 1780
                }

1781 1782 1783
                if (!allNaN) {
                    newIndices[sampledIndex++] = nextSelectedIdx;
                }
1784

1785 1786 1787 1788
                currentSelectedIdx = nextSelectedIdx; // This a is the next a (chosen b)
            }
            // The last frame is the last data.
            newIndices[sampledIndex++] = selfChunkSize - 1;
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
        }

        list._count = sampledIndex;
        list._indices = newIndices;

        list.getRawIndex = getRawIndexWithIndices;
        return list;
    }


1799 1800 1801
    /**
     * Get model of one data item.
     */
P
pissang 已提交
1802
    // TODO: Type of data item
P
pissang 已提交
1803 1804 1805 1806
    getItemModel<ItemOpts extends unknown = unknown>(idx: number): Model<ItemOpts
        // Extract item option with value key. FIXME will cause incompatitable issue
        // Extract<HostModel['option']['data'][number], { value?: any }>
    > {
1807 1808
        const hostModel = this.hostModel;
        const dataItem = this.getRawDataItem(idx) as ModelOption;
1809
        return new Model(dataItem, hostModel, hostModel && hostModel.ecModel);
1810
    }
S
sushuang 已提交
1811

1812 1813 1814 1815
    /**
     * Create a data differ
     */
    diff(otherList: List): DataDiffer {
1816
        const thisList = this;
1817 1818 1819 1820

        return new DataDiffer(
            otherList ? otherList.getIndices() : [],
            this.getIndices(),
1
100pah 已提交
1821
            function (idx: number) {
1822 1823
                return getId(otherList, idx);
            },
1
100pah 已提交
1824
            function (idx: number) {
1825 1826 1827 1828
                return getId(thisList, idx);
            }
        );
    }
S
sushuang 已提交
1829

1830 1831 1832
    /**
     * Get visual property.
     */
1833 1834
    getVisual<K extends keyof Visual>(key: K): Visual[K] {
        const visual = this._visual as Visual;
1835 1836
        return visual && visual[key];
    }
S
sushuang 已提交
1837

1838 1839 1840 1841 1842 1843 1844 1845 1846
    /**
     * Set visual property
     *
     * @example
     *  setVisual('color', color);
     *  setVisual({
     *      'color': color
     *  });
     */
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
    setVisual<K extends keyof Visual>(key: K, val: Visual[K]): void;
    setVisual(kvObj: Partial<Visual>): void;
    setVisual(kvObj: string | Partial<Visual>, val?: any): void {
        this._visual = this._visual || {};
        if (isObject(kvObj)) {
            zrUtil.extend(this._visual, kvObj);
        }
        else {
            this._visual[kvObj as string] = val;
        }
    }

    /**
     * Get visual property of single data item
     */
    // eslint-disable-next-line
    getItemVisual<K extends keyof Visual>(idx: number, key: K): Visual[K] {
        const itemVisual = this._itemVisuals[idx] as Visual;
        const val = itemVisual && itemVisual[key];
        if (val == null) {
            // Use global visual property
            return this.getVisual(key);
        }
        return val;
    }

    /**
     * Make sure itemVisual property is unique
     */
    // TODO: use key to save visual to reduce memory.
    // eslint-disable-next-line
    ensureUniqueItemVisual<K extends keyof Visual>(idx: number, key: K): Visual[K] {
        const itemVisuals = this._itemVisuals;
        let itemVisual = itemVisuals[idx] as Visual;
        if (!itemVisual) {
            itemVisual = itemVisuals[idx] = {} as Visual;
        }
        let val = itemVisual[key];
        if (!val) {
            val = this.getVisual(key);

            // TODO Performance?
            if (zrUtil.isArray(val)) {
                val = val.slice() as unknown as Visual[K];
1891
            }
1892 1893 1894 1895 1896
            else if (isObject(val)) {
                val = zrUtil.extend({}, val);
            }

            itemVisual[key] = val;
S
sushuang 已提交
1897
        }
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
        return val;
    }
    /**
     * Set visual property of single data item
     *
     * @param {number} idx
     * @param {string|Object} key
     * @param {*} [value]
     *
     * @example
     *  setItemVisual(0, 'color', color);
     *  setItemVisual(0, {
     *      'color': color
     *  });
     */
    // eslint-disable-next-line
    setItemVisual<K extends keyof Visual>(idx: number, key: K, value: Visual[K]): void;
    setItemVisual(idx: number, kvObject: Partial<Visual>): void;
    // eslint-disable-next-line
    setItemVisual<K extends keyof Visual>(idx: number, key: K | Partial<Visual>, value?: Visual[K]): void {
        const itemVisual = this._itemVisuals[idx] || {};
        this._itemVisuals[idx] = itemVisual;

        if (isObject(key)) {
            zrUtil.extend(itemVisual, key);
        }
        else {
            itemVisual[key as string] = value;
        }
    }

    /**
     * Clear itemVisuals and list visual.
     */
    clearAllVisual(): void {
        this._visual = {};
        this._itemVisuals = [];
1935
    }
S
sushuang 已提交
1936

1937 1938 1939 1940 1941 1942
    /**
     * Set layout property.
     */
    setLayout(key: string, val: any): void;
    setLayout(kvObj: Dictionary<any>): void;
    setLayout(key: string | Dictionary<any>, val?: any): void {
1943
        if (isObject(key)) {
1944
            for (const name in key) {
1945 1946 1947
                if (key.hasOwnProperty(name)) {
                    this.setLayout(name, key[name]);
                }
L
lang 已提交
1948
            }
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
            return;
        }
        this._layout[key] = val;
    }

    /**
     * Get layout property.
     */
    getLayout(key: string): any {
        return this._layout[key];
    }
S
sushuang 已提交
1960

1961 1962 1963
    /**
     * Get layout of single data item
     */
P
pissang 已提交
1964
    getItemLayout(idx: number): any {
1965 1966
        return this._itemLayouts[idx];
    }
S
sushuang 已提交
1967

1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
    /**
     * Set layout of single data item
     */
    setItemLayout<M = false>(
        idx: number,
        layout: (M extends true ? Dictionary<any> : any),
        merge?: M
    ): void {
        this._itemLayouts[idx] = merge
            ? zrUtil.extend(this._itemLayouts[idx] || {}, layout)
            : layout;
    }
S
sushuang 已提交
1980

1981 1982 1983 1984 1985 1986
    /**
     * Clear all layout of single data item
     */
    clearItemLayouts(): void {
        this._itemLayouts.length = 0;
    }
S
sushuang 已提交
1987

1988 1989 1990 1991
    /**
     * Set graphic element relative to data. It can be set as null
     */
    setItemGraphicEl(idx: number, el: Element): void {
1992
        const hostModel = this.hostModel;
P
pissang 已提交
1993

1994
        if (el) {
1995
            const ecData = getECData(el);
1996 1997
            // Add data index and series index for indexing the data by element
            // Useful in tooltip
1998 1999 2000
            ecData.dataIndex = idx;
            ecData.dataType = this.dataType;
            ecData.seriesIndex = hostModel && (hostModel as any).seriesIndex;
2001 2002

            // TODO: not store dataIndex on children.
2003 2004 2005
            if (el.type === 'group') {
                el.traverse(setItemDataAndSeriesIndex, el);
            }
S
sushuang 已提交
2006 2007
        }

2008
        this._graphicEls[idx] = el;
S
sushuang 已提交
2009
    }
L
lang 已提交
2010

2011 2012 2013
    getItemGraphicEl(idx: number): Element {
        return this._graphicEls[idx];
    }
P
pissang 已提交
2014

2015 2016 2017 2018 2019 2020 2021 2022 2023 2024
    eachItemGraphicEl<Ctx = unknown>(
        cb: (this: Ctx, el: Element, idx: number) => void,
        context?: Ctx
    ): void {
        zrUtil.each(this._graphicEls, function (el, idx) {
            if (el) {
                cb && cb.call(context, el, idx);
            }
        });
    }
P
pissang 已提交
2025

2026 2027 2028 2029
    /**
     * Shallow clone a new list except visual and layout properties, and graph elements.
     * New list only change the indices.
     */
P
pissang 已提交
2030
    cloneShallow(list?: List<HostModel>): List<HostModel> {
2031
        if (!list) {
2032
            const dimensionInfoList = zrUtil.map(this.dimensions, this.getDimensionInfo, this);
2033 2034
            list = new List(dimensionInfoList, this.hostModel);
        }
L
lang 已提交
2035

2036 2037
        // FIXME
        list._storage = this._storage;
2038
        list._storageArr = this._storageArr;
S
sushuang 已提交
2039

2040
        transferProperties(list, this);
S
sushuang 已提交
2041

2042 2043
        // Clone will not change the data extent and indices
        if (this._indices) {
2044
            const Ctor = this._indices.constructor as DataArrayLikeConstructor;
2045
            if (Ctor === Array) {
2046
                const thisCount = this._indices.length;
2047
                list._indices = new Ctor(thisCount);
2048
                for (let i = 0; i < thisCount; i++) {
2049 2050 2051 2052 2053
                    list._indices[i] = this._indices[i];
                }
            }
            else {
                list._indices = new (Ctor as DataTypedArrayConstructor)(this._indices);
L
lang 已提交
2054
            }
L
lang 已提交
2055
        }
2056 2057 2058 2059 2060 2061
        else {
            list._indices = null;
        }
        list.getRawIndex = list._indices ? getRawIndexWithIndices : getRawIndexWithoutIndices;

        return list;
S
sushuang 已提交
2062
    }
L
lang 已提交
2063

2064 2065 2066 2067 2068 2069 2070
    /**
     * Wrap some method to add more feature
     */
    wrapMethod(
        methodName: FunctionPropertyNames<List>,
        injectFunction: (...args: any) => any
    ): void {
2071
        const originalMethod = this[methodName];
2072 2073
        if (typeof originalMethod !== 'function') {
            return;
2074
        }
2075 2076 2077
        this.__wrappedMethods = this.__wrappedMethods || [];
        this.__wrappedMethods.push(methodName);
        this[methodName] = function () {
2078
            const res = (originalMethod as any).apply(this, arguments);
2079 2080
            return injectFunction.apply(this, [res].concat(zrUtil.slice(arguments)));
        };
S
sushuang 已提交
2081
    }
D
deqingli 已提交
2082 2083


2084 2085 2086
    // ----------------------------------------------------------
    // A work around for internal method visiting private member.
    // ----------------------------------------------------------
P
pissang 已提交
2087
    private static internalField = (function () {
L
lang 已提交
2088

2089
        defaultDimValueGetters = {
2090

2091
            arrayRows: getDimValueSimply,
L
lang 已提交
2092

2093 2094
            objectRows: function (
                this: List, dataItem: Dictionary<any>, dimName: string, dataIndex: number, dimIndex: number
2095
            ): ParsedValue {
1
100pah 已提交
2096
                return parseDataValue(dataItem[dimName], this._dimensionInfos[dimName]);
2097
            },
L
lang 已提交
2098

2099 2100 2101 2102
            keyedColumns: getDimValueSimply,

            original: function (
                this: List, dataItem: any, dimName: string, dataIndex: number, dimIndex: number
2103
            ): ParsedValue {
2104
                // Performance sensitive, do not use modelUtil.getDataItemValue.
2105
                // If dataItem is an plain object with no value field, the let `value`
2106 2107
                // will be assigned with the object, but it will be tread correctly
                // in the `convertDataValue`.
2108
                const value = dataItem && (dataItem.value == null ? dataItem : dataItem.value);
2109 2110 2111 2112 2113

                // If any dataItem is like { value: 10 }
                if (!this._rawData.pure && isDataItemOption(dataItem)) {
                    this.hasItemOption = true;
                }
1
100pah 已提交
2114
                return parseDataValue(
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
                    (value instanceof Array)
                        ? value[dimIndex]
                        // If value is a single number or something else not array.
                        : value,
                    this._dimensionInfos[dimName]
                );
            },

            typedArray: function (
                this: List, dataItem: any, dimName: string, dataIndex: number, dimIndex: number
2125
            ): ParsedValue {
2126
                return dataItem[dimIndex];
L
lang 已提交
2127
            }
P
pah100 已提交
2128

2129
        };
S
sushuang 已提交
2130

2131 2132
        function getDimValueSimply(
            this: List, dataItem: any, dimName: string, dataIndex: number, dimIndex: number
2133
        ): ParsedValue {
1
100pah 已提交
2134 2135
            return parseDataValue(dataItem[dimIndex], this._dimensionInfos[dimName]);
        }
2136 2137

        prepareInvertedIndex = function (list: List): void {
2138
            const invertedIndicesMap = list._invertedIndicesMap;
2139
            zrUtil.each(invertedIndicesMap, function (invertedIndices, dim) {
2140
                const dimInfo = list._dimensionInfos[dim];
2141 2142

                // Currently, only dimensions that has ordinalMeta can create inverted indices.
2143
                const ordinalMeta = dimInfo.ordinalMeta;
2144 2145 2146 2147 2148 2149
                if (ordinalMeta) {
                    invertedIndices = invertedIndicesMap[dim] = new CtorInt32Array(
                        ordinalMeta.categories.length
                    );
                    // The default value of TypedArray is 0. To avoid miss
                    // mapping to 0, we should set it as INDEX_NOT_FOUND.
1
100pah 已提交
2150
                    for (let i = 0; i < invertedIndices.length; i++) {
2151 2152
                        invertedIndices[i] = INDEX_NOT_FOUND;
                    }
1
100pah 已提交
2153
                    for (let i = 0; i < list._count; i++) {
2154 2155 2156 2157 2158 2159 2160
                        // Only support the case that all values are distinct.
                        invertedIndices[list.get(dim, i) as number] = i;
                    }
                }
            });
        };

2161 2162 2163
        getRawValueFromStore = function (
            list: List, dimIndex: number, rawIndex: number
        ): ParsedValue | OrdinalRawValue {
2164
            let val;
2165
            if (dimIndex != null) {
2166
                const chunkSize = list._chunkSize;
2167
                const chunkIndex = mathFloor(rawIndex / chunkSize);
2168 2169 2170
                const chunkOffset = rawIndex % chunkSize;
                const dim = list.dimensions[dimIndex];
                const chunk = list._storage[dim][chunkIndex];
2171 2172
                if (chunk) {
                    val = chunk[chunkOffset];
2173
                    const ordinalMeta = list._dimensionInfos[dim].ordinalMeta;
2174
                    if (ordinalMeta && ordinalMeta.categories.length) {
1
100pah 已提交
2175
                        val = ordinalMeta.categories[val as OrdinalNumber];
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
                    }
                }
            }
            return val;
        };

        getIndicesCtor = function (list: List): DataArrayLikeConstructor {
            // The possible max value in this._indicies is always this._rawCount despite of filtering.
            return list._rawCount > 65535 ? CtorUint32Array : CtorUint16Array;
        };

        prepareChunks = function (
            storage: DataStorage,
            dimInfo: DataDimensionInfo,
            chunkSize: number,
            chunkCount: number,
            end: number
        ): void {
2194 2195 2196 2197
            const DataCtor = dataCtors[dimInfo.type];
            const lastChunkIndex = chunkCount - 1;
            const dim = dimInfo.name;
            const resizeChunkArray = storage[dim][lastChunkIndex];
2198
            if (resizeChunkArray && resizeChunkArray.length < chunkSize) {
2199
                const newStore = new DataCtor(Math.min(end - lastChunkIndex * chunkSize, chunkSize));
2200 2201
                // The cost of the copy is probably inconsiderable
                // within the initial chunkSize.
2202
                for (let j = 0; j < resizeChunkArray.length; j++) {
2203 2204 2205 2206 2207 2208
                    newStore[j] = resizeChunkArray[j];
                }
                storage[dim][lastChunkIndex] = newStore;
            }

            // Create new chunks.
2209
            for (let k = chunkCount * chunkSize; k < end; k += chunkSize) {
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
                storage[dim].push(new DataCtor(Math.min(end - k, chunkSize)));
            }
        };

        getRawIndexWithoutIndices = function (this: List, idx: number): number {
            return idx;
        };

        getRawIndexWithIndices = function (this: List, idx: number): number {
            if (idx < this._count && idx >= 0) {
                return this._indices[idx];
            }
            return -1;
        };

2225 2226 2227
        /**
         * @see the comment of `List['getId']`.
         */
2228
        getId = function (list: List, rawIndex: number): string {
2229
            let id = list._idList[rawIndex];
2230
            if (id == null) {
2231
                id = convertOptionIdName(getRawValueFromStore(list, list._idDimIdx, rawIndex), null);
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
            }
            if (id == null) {
                // FIXME Check the usage in graph, should not use prefix.
                id = ID_PREFIX + rawIndex;
            }
            return id;
        };

        normalizeDimensions = function (
            dimensions: ItrParamDims
        ): Array<DimensionLoose> {
            if (!zrUtil.isArray(dimensions)) {
                dimensions = [dimensions];
            }
            return dimensions;
        };

        validateDimensions = function (list: List, dims: DimensionName[]): void {
2250
            for (let i = 0; i < dims.length; i++) {
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
                // stroage may be empty when no data, so use
                // dimensionInfos to check.
                if (!list._dimensionInfos[dims[i]]) {
                    console.error('Unkown dimension ' + dims[i]);
                }
            }
        };

        // Data in excludeDimensions is copied, otherwise transfered.
        cloneListForMapAndSample = function (
            original: List, excludeDimensions: DimensionName[]
        ): List {
2263 2264
            const allDimensions = original.dimensions;
            const list = new List(
2265 2266 2267 2268 2269 2270
                zrUtil.map(allDimensions, original.getDimensionInfo, original),
                original.hostModel
            );
            // FIXME If needs stackedOn, value may already been stacked
            transferProperties(list, original);

2271 2272
            const storage = list._storage = {} as DataStorage;
            const originalStorage = original._storage;
P
pissang 已提交
2273
            const storageArr: DataValueChunk[][] = list._storageArr = [];
2274 2275

            // Init storage
2276
            for (let i = 0; i < allDimensions.length; i++) {
2277
                const dim = allDimensions[i];
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
                if (originalStorage[dim]) {
                    // Notice that we do not reset invertedIndicesMap here, becuase
                    // there is no scenario of mapping or sampling ordinal dimension.
                    if (zrUtil.indexOf(excludeDimensions, dim) >= 0) {
                        storage[dim] = cloneDimStore(originalStorage[dim]);
                        list._rawExtent[dim] = getInitialExtent();
                        list._extent[dim] = null;
                    }
                    else {
                        // Direct reference for other dimensions
                        storage[dim] = originalStorage[dim];
                    }
P
pissang 已提交
2290
                    storageArr.push(storage[dim]);
2291 2292 2293 2294 2295 2296
                }
            }
            return list;
        };

        cloneDimStore = function (originalDimStore: DataValueChunk[]): DataValueChunk[] {
2297
            const newDimStore = new Array(originalDimStore.length);
2298
            for (let j = 0; j < originalDimStore.length; j++) {
2299 2300 2301 2302 2303 2304
                newDimStore[j] = cloneChunk(originalDimStore[j]);
            }
            return newDimStore;
        };

        function cloneChunk(originalChunk: DataValueChunk): DataValueChunk {
2305
            const Ctor = originalChunk.constructor;
2306 2307
            // Only shallow clone is enough when Array.
            return Ctor === Array
2308
                ? (originalChunk as Array<ParsedValue>).slice()
2309
                : new (Ctor as DataTypedArrayConstructor)(originalChunk as DataTypedArray);
S
sushuang 已提交
2310
        }
L
lang 已提交
2311

2312 2313 2314 2315 2316
        getInitialExtent = function (): [number, number] {
            return [Infinity, -Infinity];
        };

        setItemDataAndSeriesIndex = function (this: Element, child: Element): void {
2317 2318
            const childECData = getECData(child);
            const thisECData = getECData(this);
2319 2320 2321
            childECData.seriesIndex = thisECData.seriesIndex;
            childECData.dataIndex = thisECData.dataIndex;
            childECData.dataType = thisECData.dataType;
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332
        };

        transferProperties = function (target: List, source: List): void {
            zrUtil.each(
                TRANSFERABLE_PROPERTIES.concat(source.__wrappedMethods || []),
                function (propName) {
                    if (source.hasOwnProperty(propName)) {
                        (target as any)[propName] = (source as any)[propName];
                    }
                }
            );
L
lang 已提交
2333

2334
            target.__wrappedMethods = source.__wrappedMethods;
L
lang 已提交
2335

2336 2337 2338
            zrUtil.each(CLONE_PROPERTIES, function (propName) {
                (target as any)[propName] = zrUtil.clone((source as any)[propName]);
            });
L
lang 已提交
2339

2340 2341
            target._calculationInfo = zrUtil.extend({}, source._calculationInfo);
        };
P
pah100 已提交
2342

2343
    })();
L
lang 已提交
2344

2345 2346
}

2347 2348 2349 2350 2351
interface List {
    getLinkedData(dataType?: SeriesDataType): List;
    getLinkedDataAll(): { data: List, type?: SeriesDataType }[];
}

S
sushuang 已提交
2352
export default List;