SeriesData.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /* global Int32Array */
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import Model from '../model/Model.js';
  43. import DataDiffer from './DataDiffer.js';
  44. import { DefaultDataProvider } from './helper/dataProvider.js';
  45. import { summarizeDimensions } from './helper/dimensionHelper.js';
  46. import SeriesDimensionDefine from './SeriesDimensionDefine.js';
  47. import { SOURCE_FORMAT_TYPED_ARRAY, SOURCE_FORMAT_ORIGINAL } from '../util/types.js';
  48. import { convertOptionIdName, isDataItemOption } from '../util/model.js';
  49. import { setCommonECData } from '../util/innerStore.js';
  50. import { isSourceInstance } from './Source.js';
  51. import DataStore from './DataStore.js';
  52. import { isSeriesDataSchema } from './helper/SeriesDataSchema.js';
  53. var isObject = zrUtil.isObject;
  54. var map = zrUtil.map;
  55. var CtorInt32Array = typeof Int32Array === 'undefined' ? Array : Int32Array;
  56. // Use prefix to avoid index to be the same as otherIdList[idx],
  57. // which will cause weird update animation.
  58. var ID_PREFIX = 'e\0\0';
  59. var INDEX_NOT_FOUND = -1;
  60. // type SeriesDimensionIndex = DimensionIndex;
  61. var TRANSFERABLE_PROPERTIES = ['hasItemOption', '_nameList', '_idList', '_invertedIndicesMap', '_dimSummary', 'userOutput', '_rawData', '_dimValueGetter', '_nameDimIdx', '_idDimIdx', '_nameRepeatCount'];
  62. var CLONE_PROPERTIES = ['_approximateExtent'];
  63. // -----------------------------
  64. // Internal method declarations:
  65. // -----------------------------
  66. var prepareInvertedIndex;
  67. var getId;
  68. var getIdNameFromStore;
  69. var normalizeDimensions;
  70. var transferProperties;
  71. var cloneListForMapAndSample;
  72. var makeIdFromName;
  73. var SeriesData = /** @class */function () {
  74. /**
  75. * @param dimensionsInput.dimensions
  76. * For example, ['someDimName', {name: 'someDimName', type: 'someDimType'}, ...].
  77. * Dimensions should be concrete names like x, y, z, lng, lat, angle, radius
  78. */
  79. function SeriesData(dimensionsInput, hostModel) {
  80. this.type = 'list';
  81. this._dimOmitted = false;
  82. this._nameList = [];
  83. this._idList = [];
  84. // Models of data option is stored sparse for optimizing memory cost
  85. // Never used yet (not used yet).
  86. // private _optionModels: Model[] = [];
  87. // Global visual properties after visual coding
  88. this._visual = {};
  89. // Global layout properties.
  90. this._layout = {};
  91. // Item visual properties after visual coding
  92. this._itemVisuals = [];
  93. // Item layout properties after layout
  94. this._itemLayouts = [];
  95. // Graphic elements
  96. this._graphicEls = [];
  97. // key: dim, value: extent
  98. this._approximateExtent = {};
  99. this._calculationInfo = {};
  100. // Having detected that there is data item is non primitive type
  101. // (in type `OptionDataItemObject`).
  102. // Like `data: [ { value: xx, itemStyle: {...} }, ...]`
  103. // At present it only happen in `SOURCE_FORMAT_ORIGINAL`.
  104. this.hasItemOption = false;
  105. // Methods that create a new list based on this list should be listed here.
  106. // Notice that those method should `RETURN` the new list.
  107. this.TRANSFERABLE_METHODS = ['cloneShallow', 'downSample', 'minmaxDownSample', 'lttbDownSample', 'map'];
  108. // Methods that change indices of this list should be listed here.
  109. this.CHANGABLE_METHODS = ['filterSelf', 'selectRange'];
  110. this.DOWNSAMPLE_METHODS = ['downSample', 'minmaxDownSample', 'lttbDownSample'];
  111. var dimensions;
  112. var assignStoreDimIdx = false;
  113. if (isSeriesDataSchema(dimensionsInput)) {
  114. dimensions = dimensionsInput.dimensions;
  115. this._dimOmitted = dimensionsInput.isDimensionOmitted();
  116. this._schema = dimensionsInput;
  117. } else {
  118. assignStoreDimIdx = true;
  119. dimensions = dimensionsInput;
  120. }
  121. dimensions = dimensions || ['x', 'y'];
  122. var dimensionInfos = {};
  123. var dimensionNames = [];
  124. var invertedIndicesMap = {};
  125. var needsHasOwn = false;
  126. var emptyObj = {};
  127. for (var i = 0; i < dimensions.length; i++) {
  128. // Use the original dimensions[i], where other flag props may exists.
  129. var dimInfoInput = dimensions[i];
  130. var dimensionInfo = zrUtil.isString(dimInfoInput) ? new SeriesDimensionDefine({
  131. name: dimInfoInput
  132. }) : !(dimInfoInput instanceof SeriesDimensionDefine) ? new SeriesDimensionDefine(dimInfoInput) : dimInfoInput;
  133. var dimensionName = dimensionInfo.name;
  134. dimensionInfo.type = dimensionInfo.type || 'float';
  135. if (!dimensionInfo.coordDim) {
  136. dimensionInfo.coordDim = dimensionName;
  137. dimensionInfo.coordDimIndex = 0;
  138. }
  139. var otherDims = dimensionInfo.otherDims = dimensionInfo.otherDims || {};
  140. dimensionNames.push(dimensionName);
  141. dimensionInfos[dimensionName] = dimensionInfo;
  142. if (emptyObj[dimensionName] != null) {
  143. needsHasOwn = true;
  144. }
  145. if (dimensionInfo.createInvertedIndices) {
  146. invertedIndicesMap[dimensionName] = [];
  147. }
  148. if (process.env.NODE_ENV !== 'production') {
  149. zrUtil.assert(assignStoreDimIdx || dimensionInfo.storeDimIndex >= 0);
  150. }
  151. if (assignStoreDimIdx) {
  152. dimensionInfo.storeDimIndex = i;
  153. }
  154. if (otherDims.itemName === 0) {
  155. this._nameDimIdx = dimensionInfo.storeDimIndex;
  156. }
  157. if (otherDims.itemId === 0) {
  158. this._idDimIdx = dimensionInfo.storeDimIndex;
  159. }
  160. }
  161. this.dimensions = dimensionNames;
  162. this._dimInfos = dimensionInfos;
  163. this._initGetDimensionInfo(needsHasOwn);
  164. this.hostModel = hostModel;
  165. this._invertedIndicesMap = invertedIndicesMap;
  166. if (this._dimOmitted) {
  167. var dimIdxToName_1 = this._dimIdxToName = zrUtil.createHashMap();
  168. zrUtil.each(dimensionNames, function (dimName) {
  169. dimIdxToName_1.set(dimensionInfos[dimName].storeDimIndex, dimName);
  170. });
  171. }
  172. }
  173. /**
  174. *
  175. * Get concrete dimension name by dimension name or dimension index.
  176. * If input a dimension name, do not validate whether the dimension name exits.
  177. *
  178. * @caution
  179. * @param dim Must make sure the dimension is `SeriesDimensionLoose`.
  180. * Because only those dimensions will have auto-generated dimension names if not
  181. * have a user-specified name, and other dimensions will get a return of null/undefined.
  182. *
  183. * @notice Because of this reason, should better use `getDimensionIndex` instead, for examples:
  184. * ```js
  185. * const val = data.getStore().get(data.getDimensionIndex(dim), dataIdx);
  186. * ```
  187. *
  188. * @return Concrete dim name.
  189. */
  190. SeriesData.prototype.getDimension = function (dim) {
  191. var dimIdx = this._recognizeDimIndex(dim);
  192. if (dimIdx == null) {
  193. return dim;
  194. }
  195. dimIdx = dim;
  196. if (!this._dimOmitted) {
  197. return this.dimensions[dimIdx];
  198. }
  199. // Retrieve from series dimension definition because it probably contains
  200. // generated dimension name (like 'x', 'y').
  201. var dimName = this._dimIdxToName.get(dimIdx);
  202. if (dimName != null) {
  203. return dimName;
  204. }
  205. var sourceDimDef = this._schema.getSourceDimension(dimIdx);
  206. if (sourceDimDef) {
  207. return sourceDimDef.name;
  208. }
  209. };
  210. /**
  211. * Get dimension index in data store. Return -1 if not found.
  212. * Can be used to index value from getRawValue.
  213. */
  214. SeriesData.prototype.getDimensionIndex = function (dim) {
  215. var dimIdx = this._recognizeDimIndex(dim);
  216. if (dimIdx != null) {
  217. return dimIdx;
  218. }
  219. if (dim == null) {
  220. return -1;
  221. }
  222. var dimInfo = this._getDimInfo(dim);
  223. return dimInfo ? dimInfo.storeDimIndex : this._dimOmitted ? this._schema.getSourceDimensionIndex(dim) : -1;
  224. };
  225. /**
  226. * The meanings of the input parameter `dim`:
  227. *
  228. * + If dim is a number (e.g., `1`), it means the index of the dimension.
  229. * For example, `getDimension(0)` will return 'x' or 'lng' or 'radius'.
  230. * + If dim is a number-like string (e.g., `"1"`):
  231. * + If there is the same concrete dim name defined in `series.dimensions` or `dataset.dimensions`,
  232. * it means that concrete name.
  233. * + If not, it will be converted to a number, which means the index of the dimension.
  234. * (why? because of the backward compatibility. We have been tolerating number-like string in
  235. * dimension setting, although now it seems that it is not a good idea.)
  236. * For example, `visualMap[i].dimension: "1"` is the same meaning as `visualMap[i].dimension: 1`,
  237. * if no dimension name is defined as `"1"`.
  238. * + If dim is a not-number-like string, it means the concrete dim name.
  239. * For example, it can be be default name `"x"`, `"y"`, `"z"`, `"lng"`, `"lat"`, `"angle"`, `"radius"`,
  240. * or customized in `dimensions` property of option like `"age"`.
  241. *
  242. * @return recognized `DimensionIndex`. Otherwise return null/undefined (means that dim is `DimensionName`).
  243. */
  244. SeriesData.prototype._recognizeDimIndex = function (dim) {
  245. if (zrUtil.isNumber(dim)
  246. // If being a number-like string but not being defined as a dimension name.
  247. || dim != null && !isNaN(dim) && !this._getDimInfo(dim) && (!this._dimOmitted || this._schema.getSourceDimensionIndex(dim) < 0)) {
  248. return +dim;
  249. }
  250. };
  251. SeriesData.prototype._getStoreDimIndex = function (dim) {
  252. var dimIdx = this.getDimensionIndex(dim);
  253. if (process.env.NODE_ENV !== 'production') {
  254. if (dimIdx == null) {
  255. throw new Error('Unknown dimension ' + dim);
  256. }
  257. }
  258. return dimIdx;
  259. };
  260. /**
  261. * Get type and calculation info of particular dimension
  262. * @param dim
  263. * Dimension can be concrete names like x, y, z, lng, lat, angle, radius
  264. * Or a ordinal number. For example getDimensionInfo(0) will return 'x' or 'lng' or 'radius'
  265. */
  266. SeriesData.prototype.getDimensionInfo = function (dim) {
  267. // Do not clone, because there may be categories in dimInfo.
  268. return this._getDimInfo(this.getDimension(dim));
  269. };
  270. SeriesData.prototype._initGetDimensionInfo = function (needsHasOwn) {
  271. var dimensionInfos = this._dimInfos;
  272. this._getDimInfo = needsHasOwn ? function (dimName) {
  273. return dimensionInfos.hasOwnProperty(dimName) ? dimensionInfos[dimName] : undefined;
  274. } : function (dimName) {
  275. return dimensionInfos[dimName];
  276. };
  277. };
  278. /**
  279. * concrete dimension name list on coord.
  280. */
  281. SeriesData.prototype.getDimensionsOnCoord = function () {
  282. return this._dimSummary.dataDimsOnCoord.slice();
  283. };
  284. SeriesData.prototype.mapDimension = function (coordDim, idx) {
  285. var dimensionsSummary = this._dimSummary;
  286. if (idx == null) {
  287. return dimensionsSummary.encodeFirstDimNotExtra[coordDim];
  288. }
  289. var dims = dimensionsSummary.encode[coordDim];
  290. return dims ? dims[idx] : null;
  291. };
  292. SeriesData.prototype.mapDimensionsAll = function (coordDim) {
  293. var dimensionsSummary = this._dimSummary;
  294. var dims = dimensionsSummary.encode[coordDim];
  295. return (dims || []).slice();
  296. };
  297. SeriesData.prototype.getStore = function () {
  298. return this._store;
  299. };
  300. /**
  301. * Initialize from data
  302. * @param data source or data or data store.
  303. * @param nameList The name of a datum is used on data diff and
  304. * default label/tooltip.
  305. * A name can be specified in encode.itemName,
  306. * or dataItem.name (only for series option data),
  307. * or provided in nameList from outside.
  308. */
  309. SeriesData.prototype.initData = function (data, nameList, dimValueGetter) {
  310. var _this = this;
  311. var store;
  312. if (data instanceof DataStore) {
  313. store = data;
  314. }
  315. if (!store) {
  316. var dimensions = this.dimensions;
  317. var provider = isSourceInstance(data) || zrUtil.isArrayLike(data) ? new DefaultDataProvider(data, dimensions.length) : data;
  318. store = new DataStore();
  319. var dimensionInfos = map(dimensions, function (dimName) {
  320. return {
  321. type: _this._dimInfos[dimName].type,
  322. property: dimName
  323. };
  324. });
  325. store.initData(provider, dimensionInfos, dimValueGetter);
  326. }
  327. this._store = store;
  328. // Reset
  329. this._nameList = (nameList || []).slice();
  330. this._idList = [];
  331. this._nameRepeatCount = {};
  332. this._doInit(0, store.count());
  333. // Cache summary info for fast visit. See "dimensionHelper".
  334. // Needs to be initialized after store is prepared.
  335. this._dimSummary = summarizeDimensions(this, this._schema);
  336. this.userOutput = this._dimSummary.userOutput;
  337. };
  338. /**
  339. * Caution: Can be only called on raw data (before `this._indices` created).
  340. */
  341. SeriesData.prototype.appendData = function (data) {
  342. var range = this._store.appendData(data);
  343. this._doInit(range[0], range[1]);
  344. };
  345. /**
  346. * Caution: Can be only called on raw data (before `this._indices` created).
  347. * This method does not modify `rawData` (`dataProvider`), but only
  348. * add values to store.
  349. *
  350. * The final count will be increased by `Math.max(values.length, names.length)`.
  351. *
  352. * @param values That is the SourceType: 'arrayRows', like
  353. * [
  354. * [12, 33, 44],
  355. * [NaN, 43, 1],
  356. * ['-', 'asdf', 0]
  357. * ]
  358. * Each item is exactly corresponding to a dimension.
  359. */
  360. SeriesData.prototype.appendValues = function (values, names) {
  361. var _a = this._store.appendValues(values, names && names.length),
  362. start = _a.start,
  363. end = _a.end;
  364. var shouldMakeIdFromName = this._shouldMakeIdFromName();
  365. this._updateOrdinalMeta();
  366. if (names) {
  367. for (var idx = start; idx < end; idx++) {
  368. var sourceIdx = idx - start;
  369. this._nameList[idx] = names[sourceIdx];
  370. if (shouldMakeIdFromName) {
  371. makeIdFromName(this, idx);
  372. }
  373. }
  374. }
  375. };
  376. SeriesData.prototype._updateOrdinalMeta = function () {
  377. var store = this._store;
  378. var dimensions = this.dimensions;
  379. for (var i = 0; i < dimensions.length; i++) {
  380. var dimInfo = this._dimInfos[dimensions[i]];
  381. if (dimInfo.ordinalMeta) {
  382. store.collectOrdinalMeta(dimInfo.storeDimIndex, dimInfo.ordinalMeta);
  383. }
  384. }
  385. };
  386. SeriesData.prototype._shouldMakeIdFromName = function () {
  387. var provider = this._store.getProvider();
  388. return this._idDimIdx == null && provider.getSource().sourceFormat !== SOURCE_FORMAT_TYPED_ARRAY && !provider.fillStorage;
  389. };
  390. SeriesData.prototype._doInit = function (start, end) {
  391. if (start >= end) {
  392. return;
  393. }
  394. var store = this._store;
  395. var provider = store.getProvider();
  396. this._updateOrdinalMeta();
  397. var nameList = this._nameList;
  398. var idList = this._idList;
  399. var sourceFormat = provider.getSource().sourceFormat;
  400. var isFormatOriginal = sourceFormat === SOURCE_FORMAT_ORIGINAL;
  401. // Each data item is value
  402. // [1, 2]
  403. // 2
  404. // Bar chart, line chart which uses category axis
  405. // only gives the 'y' value. 'x' value is the indices of category
  406. // Use a tempValue to normalize the value to be a (x, y) value
  407. // If dataItem is {name: ...} or {id: ...}, it has highest priority.
  408. // This kind of ids and names are always stored `_nameList` and `_idList`.
  409. if (isFormatOriginal && !provider.pure) {
  410. var sharedDataItem = [];
  411. for (var idx = start; idx < end; idx++) {
  412. // NOTICE: Try not to write things into dataItem
  413. var dataItem = provider.getItem(idx, sharedDataItem);
  414. if (!this.hasItemOption && isDataItemOption(dataItem)) {
  415. this.hasItemOption = true;
  416. }
  417. if (dataItem) {
  418. var itemName = dataItem.name;
  419. if (nameList[idx] == null && itemName != null) {
  420. nameList[idx] = convertOptionIdName(itemName, null);
  421. }
  422. var itemId = dataItem.id;
  423. if (idList[idx] == null && itemId != null) {
  424. idList[idx] = convertOptionIdName(itemId, null);
  425. }
  426. }
  427. }
  428. }
  429. if (this._shouldMakeIdFromName()) {
  430. for (var idx = start; idx < end; idx++) {
  431. makeIdFromName(this, idx);
  432. }
  433. }
  434. prepareInvertedIndex(this);
  435. };
  436. /**
  437. * Optimize for the scenario that data is filtered by a given extent.
  438. * Consider that if data amount is more than hundreds of thousand,
  439. * extent calculation will cost more than 10ms and the cache will
  440. * be erased because of the filtering.
  441. */
  442. SeriesData.prototype.getApproximateExtent = function (dim, filter) {
  443. return this._approximateExtent[dim] || this._store.getDataExtent(this._getStoreDimIndex(dim), filter);
  444. };
  445. /**
  446. * NOTICE: `_approximateExtent` does not support filter. Callers must ensure the input extent
  447. * to be handled by `scale.sanitizeExtent`.
  448. *
  449. * Calculate extent on a filtered data might be time consuming.
  450. * Approximate extent is only used for: calculate extent of filtered data outside.
  451. */
  452. SeriesData.prototype.setApproximateExtent = function (extent, dim) {
  453. dim = this.getDimension(dim);
  454. this._approximateExtent[dim] = extent.slice();
  455. };
  456. SeriesData.prototype.getCalculationInfo = function (key) {
  457. return this._calculationInfo[key];
  458. };
  459. SeriesData.prototype.setCalculationInfo = function (key, value) {
  460. isObject(key) ? zrUtil.extend(this._calculationInfo, key) : this._calculationInfo[key] = value;
  461. };
  462. /**
  463. * @return Never be null/undefined. `number` will be converted to string. Because:
  464. * In most cases, name is used in display, where returning a string is more convenient.
  465. * In other cases, name is used in query (see `indexOfName`), where we can keep the
  466. * rule that name `2` equals to name `'2'`.
  467. */
  468. SeriesData.prototype.getName = function (idx) {
  469. var rawIndex = this.getRawIndex(idx);
  470. var name = this._nameList[rawIndex];
  471. if (name == null && this._nameDimIdx != null) {
  472. name = getIdNameFromStore(this, this._nameDimIdx, rawIndex);
  473. }
  474. if (name == null) {
  475. name = '';
  476. }
  477. return name;
  478. };
  479. SeriesData.prototype._getCategory = function (dimIdx, idx) {
  480. var ordinal = this._store.get(dimIdx, idx);
  481. var ordinalMeta = this._store.getOrdinalMeta(dimIdx);
  482. if (ordinalMeta) {
  483. return ordinalMeta.categories[ordinal];
  484. }
  485. return ordinal;
  486. };
  487. /**
  488. * @return Never null/undefined. `number` will be converted to string. Because:
  489. * In all cases having encountered at present, id is used in making diff comparison, which
  490. * are usually based on hash map. We can keep the rule that the internal id are always string
  491. * (treat `2` is the same as `'2'`) to make the related logic simple.
  492. */
  493. SeriesData.prototype.getId = function (idx) {
  494. return getId(this, this.getRawIndex(idx));
  495. };
  496. SeriesData.prototype.count = function () {
  497. return this._store.count();
  498. };
  499. /**
  500. * Get value. Return NaN if idx is out of range.
  501. *
  502. * @notice Should better to use `data.getStore().get(dimIndex, dataIdx)` instead.
  503. */
  504. SeriesData.prototype.get = function (dim, idx) {
  505. var store = this._store;
  506. var dimInfo = this._dimInfos[dim];
  507. if (dimInfo) {
  508. return store.get(dimInfo.storeDimIndex, idx);
  509. }
  510. };
  511. /**
  512. * @notice Should better to use `data.getStore().getByRawIndex(dimIndex, dataIdx)` instead.
  513. */
  514. SeriesData.prototype.getByRawIndex = function (dim, rawIdx) {
  515. var store = this._store;
  516. var dimInfo = this._dimInfos[dim];
  517. if (dimInfo) {
  518. return store.getByRawIndex(dimInfo.storeDimIndex, rawIdx);
  519. }
  520. };
  521. SeriesData.prototype.getIndices = function () {
  522. return this._store.getIndices();
  523. };
  524. SeriesData.prototype.getDataExtent = function (dim) {
  525. return this._store.getDataExtent(this._getStoreDimIndex(dim), null);
  526. };
  527. SeriesData.prototype.getSum = function (dim) {
  528. return this._store.getSum(this._getStoreDimIndex(dim));
  529. };
  530. SeriesData.prototype.getMedian = function (dim) {
  531. return this._store.getMedian(this._getStoreDimIndex(dim));
  532. };
  533. SeriesData.prototype.getValues = function (dimensions, idx) {
  534. var _this = this;
  535. var store = this._store;
  536. return zrUtil.isArray(dimensions) ? store.getValues(map(dimensions, function (dim) {
  537. return _this._getStoreDimIndex(dim);
  538. }), idx) : store.getValues(dimensions);
  539. };
  540. /**
  541. * If value is NaN. Including '-'
  542. * Only check the coord dimensions.
  543. */
  544. SeriesData.prototype.hasValue = function (idx) {
  545. var dataDimIndicesOnCoord = this._dimSummary.dataDimIndicesOnCoord;
  546. for (var i = 0, len = dataDimIndicesOnCoord.length; i < len; i++) {
  547. // Ordinal type originally can be string or number.
  548. // But when an ordinal type is used on coord, it can
  549. // not be string but only number. So we can also use isNaN.
  550. if (isNaN(this._store.get(dataDimIndicesOnCoord[i], idx))) {
  551. return false;
  552. }
  553. }
  554. return true;
  555. };
  556. /**
  557. * Retrieve the index with given name
  558. */
  559. SeriesData.prototype.indexOfName = function (name) {
  560. for (var i = 0, len = this._store.count(); i < len; i++) {
  561. if (this.getName(i) === name) {
  562. return i;
  563. }
  564. }
  565. return -1;
  566. };
  567. SeriesData.prototype.getRawIndex = function (idx) {
  568. return this._store.getRawIndex(idx);
  569. };
  570. SeriesData.prototype.indexOfRawIndex = function (rawIndex) {
  571. return this._store.indexOfRawIndex(rawIndex);
  572. };
  573. /**
  574. * Only support the dimension which inverted index created.
  575. * Do not support other cases until required.
  576. * @param dim concrete dim
  577. * @param value ordinal index
  578. * @return rawIndex
  579. */
  580. SeriesData.prototype.rawIndexOf = function (dim, value) {
  581. var invertedIndices = dim && this._invertedIndicesMap[dim];
  582. if (process.env.NODE_ENV !== 'production') {
  583. if (!invertedIndices) {
  584. throw new Error('Do not supported yet');
  585. }
  586. }
  587. var rawIndex = invertedIndices && invertedIndices[value];
  588. if (rawIndex == null || isNaN(rawIndex)) {
  589. return INDEX_NOT_FOUND;
  590. }
  591. return rawIndex;
  592. };
  593. SeriesData.prototype.each = function (dims, cb, ctx) {
  594. 'use strict';
  595. if (zrUtil.isFunction(dims)) {
  596. ctx = cb;
  597. cb = dims;
  598. dims = [];
  599. }
  600. // ctxCompat just for compat echarts3
  601. var fCtx = ctx || this;
  602. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  603. this._store.each(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  604. };
  605. SeriesData.prototype.filterSelf = function (dims, cb, ctx) {
  606. 'use strict';
  607. if (zrUtil.isFunction(dims)) {
  608. ctx = cb;
  609. cb = dims;
  610. dims = [];
  611. }
  612. // ctxCompat just for compat echarts3
  613. var fCtx = ctx || this;
  614. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  615. this._store = this._store.filter(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  616. return this;
  617. };
  618. /**
  619. * Select data in range. (For optimization of filter)
  620. * (Manually inline code, support 5 million data filtering in data zoom.)
  621. */
  622. SeriesData.prototype.selectRange = function (range) {
  623. 'use strict';
  624. var _this = this;
  625. var innerRange = {};
  626. var dims = zrUtil.keys(range);
  627. var dimIndices = [];
  628. zrUtil.each(dims, function (dim) {
  629. var dimIdx = _this._getStoreDimIndex(dim);
  630. innerRange[dimIdx] = range[dim];
  631. dimIndices.push(dimIdx);
  632. });
  633. this._store = this._store.selectRange(innerRange);
  634. return this;
  635. };
  636. /* eslint-enable max-len */
  637. SeriesData.prototype.mapArray = function (dims, cb, ctx) {
  638. 'use strict';
  639. if (zrUtil.isFunction(dims)) {
  640. ctx = cb;
  641. cb = dims;
  642. dims = [];
  643. }
  644. // ctxCompat just for compat echarts3
  645. ctx = ctx || this;
  646. var result = [];
  647. this.each(dims, function () {
  648. result.push(cb && cb.apply(this, arguments));
  649. }, ctx);
  650. return result;
  651. };
  652. SeriesData.prototype.map = function (dims, cb, ctx, ctxCompat) {
  653. 'use strict';
  654. // ctxCompat just for compat echarts3
  655. var fCtx = ctx || ctxCompat || this;
  656. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  657. var list = cloneListForMapAndSample(this);
  658. list._store = this._store.map(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  659. return list;
  660. };
  661. SeriesData.prototype.modify = function (dims, cb, ctx, ctxCompat) {
  662. var _this = this;
  663. // ctxCompat just for compat echarts3
  664. var fCtx = ctx || ctxCompat || this;
  665. if (process.env.NODE_ENV !== 'production') {
  666. zrUtil.each(normalizeDimensions(dims), function (dim) {
  667. var dimInfo = _this.getDimensionInfo(dim);
  668. if (!dimInfo.isCalculationCoord) {
  669. console.error('Danger: only stack dimension can be modified');
  670. }
  671. });
  672. }
  673. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  674. // If do shallow clone here, if there are too many stacked series,
  675. // it still cost lots of memory, because `_store.dimensions` are not shared.
  676. // We should consider there probably be shallow clone happen in each series
  677. // in consequent filter/map.
  678. this._store.modify(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  679. };
  680. /**
  681. * Large data down sampling on given dimension
  682. * @param sampleIndex Sample index for name and id
  683. */
  684. SeriesData.prototype.downSample = function (dimension, rate, sampleValue, sampleIndex) {
  685. var list = cloneListForMapAndSample(this);
  686. list._store = this._store.downSample(this._getStoreDimIndex(dimension), rate, sampleValue, sampleIndex);
  687. return list;
  688. };
  689. /**
  690. * Large data down sampling using min-max
  691. * @param {string} valueDimension
  692. * @param {number} rate
  693. */
  694. SeriesData.prototype.minmaxDownSample = function (valueDimension, rate) {
  695. var list = cloneListForMapAndSample(this);
  696. list._store = this._store.minmaxDownSample(this._getStoreDimIndex(valueDimension), rate);
  697. return list;
  698. };
  699. /**
  700. * Large data down sampling using largest-triangle-three-buckets
  701. * @param {string} valueDimension
  702. * @param {number} targetCount
  703. */
  704. SeriesData.prototype.lttbDownSample = function (valueDimension, rate) {
  705. var list = cloneListForMapAndSample(this);
  706. list._store = this._store.lttbDownSample(this._getStoreDimIndex(valueDimension), rate);
  707. return list;
  708. };
  709. SeriesData.prototype.getRawDataItem = function (idx) {
  710. return this._store.getRawDataItem(idx);
  711. };
  712. /**
  713. * Get model of one data item.
  714. */
  715. // TODO: Type of data item
  716. SeriesData.prototype.getItemModel = function (idx) {
  717. var hostModel = this.hostModel;
  718. var dataItem = this.getRawDataItem(idx);
  719. return new Model(dataItem, hostModel, hostModel && hostModel.ecModel);
  720. };
  721. /**
  722. * Create a data differ
  723. */
  724. SeriesData.prototype.diff = function (otherList) {
  725. var thisList = this;
  726. return new DataDiffer(otherList ? otherList.getStore().getIndices() : [], this.getStore().getIndices(), function (idx) {
  727. return getId(otherList, idx);
  728. }, function (idx) {
  729. return getId(thisList, idx);
  730. });
  731. };
  732. /**
  733. * Get visual property.
  734. */
  735. SeriesData.prototype.getVisual = function (key) {
  736. var visual = this._visual;
  737. return visual && visual[key];
  738. };
  739. SeriesData.prototype.setVisual = function (kvObj, val) {
  740. this._visual = this._visual || {};
  741. if (isObject(kvObj)) {
  742. zrUtil.extend(this._visual, kvObj);
  743. } else {
  744. this._visual[kvObj] = val;
  745. }
  746. };
  747. /**
  748. * Get visual property of single data item
  749. */
  750. // eslint-disable-next-line
  751. SeriesData.prototype.getItemVisual = function (idx, key) {
  752. var itemVisual = this._itemVisuals[idx];
  753. var val = itemVisual && itemVisual[key];
  754. if (val == null) {
  755. // Use global visual property
  756. return this.getVisual(key);
  757. }
  758. return val;
  759. };
  760. /**
  761. * If exists visual property of single data item
  762. */
  763. SeriesData.prototype.hasItemVisual = function () {
  764. return this._itemVisuals.length > 0;
  765. };
  766. /**
  767. * Make sure itemVisual property is unique
  768. */
  769. // TODO: use key to save visual to reduce memory.
  770. SeriesData.prototype.ensureUniqueItemVisual = function (idx, key) {
  771. var itemVisuals = this._itemVisuals;
  772. var itemVisual = itemVisuals[idx];
  773. if (!itemVisual) {
  774. itemVisual = itemVisuals[idx] = {};
  775. }
  776. var val = itemVisual[key];
  777. if (val == null) {
  778. val = this.getVisual(key);
  779. // TODO Performance?
  780. if (zrUtil.isArray(val)) {
  781. val = val.slice();
  782. } else if (isObject(val)) {
  783. val = zrUtil.extend({}, val);
  784. }
  785. itemVisual[key] = val;
  786. }
  787. return val;
  788. };
  789. // eslint-disable-next-line
  790. SeriesData.prototype.setItemVisual = function (idx, key, value) {
  791. var itemVisual = this._itemVisuals[idx] || {};
  792. this._itemVisuals[idx] = itemVisual;
  793. if (isObject(key)) {
  794. zrUtil.extend(itemVisual, key);
  795. } else {
  796. itemVisual[key] = value;
  797. }
  798. };
  799. /**
  800. * Clear itemVisuals and list visual.
  801. */
  802. SeriesData.prototype.clearAllVisual = function () {
  803. this._visual = {};
  804. this._itemVisuals = [];
  805. };
  806. SeriesData.prototype.setLayout = function (key, val) {
  807. isObject(key) ? zrUtil.extend(this._layout, key) : this._layout[key] = val;
  808. };
  809. /**
  810. * Get layout property.
  811. */
  812. SeriesData.prototype.getLayout = function (key) {
  813. return this._layout[key];
  814. };
  815. /**
  816. * Get layout of single data item
  817. */
  818. SeriesData.prototype.getItemLayout = function (idx) {
  819. return this._itemLayouts[idx];
  820. };
  821. /**
  822. * Set layout of single data item
  823. */
  824. SeriesData.prototype.setItemLayout = function (idx, layout, merge) {
  825. this._itemLayouts[idx] = merge ? zrUtil.extend(this._itemLayouts[idx] || {}, layout) : layout;
  826. };
  827. /**
  828. * Clear all layout of single data item
  829. */
  830. SeriesData.prototype.clearItemLayouts = function () {
  831. this._itemLayouts.length = 0;
  832. };
  833. /**
  834. * Set graphic element relative to data. It can be set as null
  835. */
  836. SeriesData.prototype.setItemGraphicEl = function (idx, el) {
  837. var seriesIndex = this.hostModel && this.hostModel.seriesIndex;
  838. setCommonECData(seriesIndex, this.dataType, idx, el);
  839. this._graphicEls[idx] = el;
  840. };
  841. SeriesData.prototype.getItemGraphicEl = function (idx) {
  842. return this._graphicEls[idx];
  843. };
  844. SeriesData.prototype.eachItemGraphicEl = function (cb, context) {
  845. zrUtil.each(this._graphicEls, function (el, idx) {
  846. if (el) {
  847. cb && cb.call(context, el, idx);
  848. }
  849. });
  850. };
  851. /**
  852. * Shallow clone a new list except visual and layout properties, and graph elements.
  853. * New list only change the indices.
  854. */
  855. SeriesData.prototype.cloneShallow = function (list) {
  856. if (!list) {
  857. list = new SeriesData(this._schema ? this._schema : map(this.dimensions, this._getDimInfo, this), this.hostModel);
  858. }
  859. transferProperties(list, this);
  860. list._store = this._store;
  861. return list;
  862. };
  863. /**
  864. * Wrap some method to add more feature
  865. */
  866. SeriesData.prototype.wrapMethod = function (methodName, injectFunction) {
  867. var originalMethod = this[methodName];
  868. if (!zrUtil.isFunction(originalMethod)) {
  869. return;
  870. }
  871. this.__wrappedMethods = this.__wrappedMethods || [];
  872. this.__wrappedMethods.push(methodName);
  873. this[methodName] = function () {
  874. var res = originalMethod.apply(this, arguments);
  875. return injectFunction.apply(this, [res].concat(zrUtil.slice(arguments)));
  876. };
  877. };
  878. // ----------------------------------------------------------
  879. // A work around for internal method visiting private member.
  880. // ----------------------------------------------------------
  881. SeriesData.internalField = function () {
  882. prepareInvertedIndex = function (data) {
  883. var invertedIndicesMap = data._invertedIndicesMap;
  884. zrUtil.each(invertedIndicesMap, function (invertedIndices, dim) {
  885. var dimInfo = data._dimInfos[dim];
  886. // Currently, only dimensions that has ordinalMeta can create inverted indices.
  887. var ordinalMeta = dimInfo.ordinalMeta;
  888. var store = data._store;
  889. if (ordinalMeta) {
  890. invertedIndices = invertedIndicesMap[dim] = new CtorInt32Array(ordinalMeta.categories.length);
  891. // The default value of TypedArray is 0. To avoid miss
  892. // mapping to 0, we should set it as INDEX_NOT_FOUND.
  893. for (var i = 0; i < invertedIndices.length; i++) {
  894. invertedIndices[i] = INDEX_NOT_FOUND;
  895. }
  896. for (var i = 0; i < store.count(); i++) {
  897. // Only support the case that all values are distinct.
  898. invertedIndices[store.get(dimInfo.storeDimIndex, i)] = i;
  899. }
  900. }
  901. });
  902. };
  903. getIdNameFromStore = function (data, dimIdx, idx) {
  904. return convertOptionIdName(data._getCategory(dimIdx, idx), null);
  905. };
  906. /**
  907. * @see the comment of `List['getId']`.
  908. */
  909. getId = function (data, rawIndex) {
  910. var id = data._idList[rawIndex];
  911. if (id == null && data._idDimIdx != null) {
  912. id = getIdNameFromStore(data, data._idDimIdx, rawIndex);
  913. }
  914. if (id == null) {
  915. id = ID_PREFIX + rawIndex;
  916. }
  917. return id;
  918. };
  919. normalizeDimensions = function (dimensions) {
  920. if (!zrUtil.isArray(dimensions)) {
  921. dimensions = dimensions != null ? [dimensions] : [];
  922. }
  923. return dimensions;
  924. };
  925. /**
  926. * Data in excludeDimensions is copied, otherwise transferred.
  927. */
  928. cloneListForMapAndSample = function (original) {
  929. var list = new SeriesData(original._schema ? original._schema : map(original.dimensions, original._getDimInfo, original), original.hostModel);
  930. // FIXME If needs stackedOn, value may already been stacked
  931. transferProperties(list, original);
  932. return list;
  933. };
  934. transferProperties = function (target, source) {
  935. zrUtil.each(TRANSFERABLE_PROPERTIES.concat(source.__wrappedMethods || []), function (propName) {
  936. if (source.hasOwnProperty(propName)) {
  937. target[propName] = source[propName];
  938. }
  939. });
  940. target.__wrappedMethods = source.__wrappedMethods;
  941. zrUtil.each(CLONE_PROPERTIES, function (propName) {
  942. target[propName] = zrUtil.clone(source[propName]);
  943. });
  944. target._calculationInfo = zrUtil.extend({}, source._calculationInfo);
  945. };
  946. makeIdFromName = function (data, idx) {
  947. var nameList = data._nameList;
  948. var idList = data._idList;
  949. var nameDimIdx = data._nameDimIdx;
  950. var idDimIdx = data._idDimIdx;
  951. var name = nameList[idx];
  952. var id = idList[idx];
  953. if (name == null && nameDimIdx != null) {
  954. nameList[idx] = name = getIdNameFromStore(data, nameDimIdx, idx);
  955. }
  956. if (id == null && idDimIdx != null) {
  957. idList[idx] = id = getIdNameFromStore(data, idDimIdx, idx);
  958. }
  959. if (id == null && name != null) {
  960. var nameRepeatCount = data._nameRepeatCount;
  961. var nmCnt = nameRepeatCount[name] = (nameRepeatCount[name] || 0) + 1;
  962. id = name;
  963. if (nmCnt > 1) {
  964. id += '__ec__' + nmCnt;
  965. }
  966. idList[idx] = id;
  967. }
  968. };
  969. }();
  970. return SeriesData;
  971. }();
  972. export default SeriesData;