SeriesData.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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', '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', '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 (otherDims.itemName === 0) {
  149. this._nameDimIdx = i;
  150. }
  151. if (otherDims.itemId === 0) {
  152. this._idDimIdx = i;
  153. }
  154. if (process.env.NODE_ENV !== 'production') {
  155. zrUtil.assert(assignStoreDimIdx || dimensionInfo.storeDimIndex >= 0);
  156. }
  157. if (assignStoreDimIdx) {
  158. dimensionInfo.storeDimIndex = i;
  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.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. * PENDING: In fact currently this function is only used to short-circuit
  438. * the calling of `scale.unionExtentFromData` when data have been filtered by modules
  439. * like "dataZoom". `scale.unionExtentFromData` is used to calculate data extent for series on
  440. * an axis, but if a "axis related data filter module" is used, the extent of the axis have
  441. * been fixed and no need to calling `scale.unionExtentFromData` actually.
  442. * But if we add "custom data filter" in future, which is not "axis related", this method may
  443. * be still needed.
  444. *
  445. * Optimize for the scenario that data is filtered by a given extent.
  446. * Consider that if data amount is more than hundreds of thousand,
  447. * extent calculation will cost more than 10ms and the cache will
  448. * be erased because of the filtering.
  449. */
  450. SeriesData.prototype.getApproximateExtent = function (dim) {
  451. return this._approximateExtent[dim] || this._store.getDataExtent(this._getStoreDimIndex(dim));
  452. };
  453. /**
  454. * Calculate extent on a filtered data might be time consuming.
  455. * Approximate extent is only used for: calculate extent of filtered data outside.
  456. */
  457. SeriesData.prototype.setApproximateExtent = function (extent, dim) {
  458. dim = this.getDimension(dim);
  459. this._approximateExtent[dim] = extent.slice();
  460. };
  461. SeriesData.prototype.getCalculationInfo = function (key) {
  462. return this._calculationInfo[key];
  463. };
  464. SeriesData.prototype.setCalculationInfo = function (key, value) {
  465. isObject(key) ? zrUtil.extend(this._calculationInfo, key) : this._calculationInfo[key] = value;
  466. };
  467. /**
  468. * @return Never be null/undefined. `number` will be converted to string. Because:
  469. * In most cases, name is used in display, where returning a string is more convenient.
  470. * In other cases, name is used in query (see `indexOfName`), where we can keep the
  471. * rule that name `2` equals to name `'2'`.
  472. */
  473. SeriesData.prototype.getName = function (idx) {
  474. var rawIndex = this.getRawIndex(idx);
  475. var name = this._nameList[rawIndex];
  476. if (name == null && this._nameDimIdx != null) {
  477. name = getIdNameFromStore(this, this._nameDimIdx, rawIndex);
  478. }
  479. if (name == null) {
  480. name = '';
  481. }
  482. return name;
  483. };
  484. SeriesData.prototype._getCategory = function (dimIdx, idx) {
  485. var ordinal = this._store.get(dimIdx, idx);
  486. var ordinalMeta = this._store.getOrdinalMeta(dimIdx);
  487. if (ordinalMeta) {
  488. return ordinalMeta.categories[ordinal];
  489. }
  490. return ordinal;
  491. };
  492. /**
  493. * @return Never null/undefined. `number` will be converted to string. Because:
  494. * In all cases having encountered at present, id is used in making diff comparison, which
  495. * are usually based on hash map. We can keep the rule that the internal id are always string
  496. * (treat `2` is the same as `'2'`) to make the related logic simple.
  497. */
  498. SeriesData.prototype.getId = function (idx) {
  499. return getId(this, this.getRawIndex(idx));
  500. };
  501. SeriesData.prototype.count = function () {
  502. return this._store.count();
  503. };
  504. /**
  505. * Get value. Return NaN if idx is out of range.
  506. *
  507. * @notice Should better to use `data.getStore().get(dimIndex, dataIdx)` instead.
  508. */
  509. SeriesData.prototype.get = function (dim, idx) {
  510. var store = this._store;
  511. var dimInfo = this._dimInfos[dim];
  512. if (dimInfo) {
  513. return store.get(dimInfo.storeDimIndex, idx);
  514. }
  515. };
  516. /**
  517. * @notice Should better to use `data.getStore().getByRawIndex(dimIndex, dataIdx)` instead.
  518. */
  519. SeriesData.prototype.getByRawIndex = function (dim, rawIdx) {
  520. var store = this._store;
  521. var dimInfo = this._dimInfos[dim];
  522. if (dimInfo) {
  523. return store.getByRawIndex(dimInfo.storeDimIndex, rawIdx);
  524. }
  525. };
  526. SeriesData.prototype.getIndices = function () {
  527. return this._store.getIndices();
  528. };
  529. SeriesData.prototype.getDataExtent = function (dim) {
  530. return this._store.getDataExtent(this._getStoreDimIndex(dim));
  531. };
  532. SeriesData.prototype.getSum = function (dim) {
  533. return this._store.getSum(this._getStoreDimIndex(dim));
  534. };
  535. SeriesData.prototype.getMedian = function (dim) {
  536. return this._store.getMedian(this._getStoreDimIndex(dim));
  537. };
  538. SeriesData.prototype.getValues = function (dimensions, idx) {
  539. var _this = this;
  540. var store = this._store;
  541. return zrUtil.isArray(dimensions) ? store.getValues(map(dimensions, function (dim) {
  542. return _this._getStoreDimIndex(dim);
  543. }), idx) : store.getValues(dimensions);
  544. };
  545. /**
  546. * If value is NaN. Including '-'
  547. * Only check the coord dimensions.
  548. */
  549. SeriesData.prototype.hasValue = function (idx) {
  550. var dataDimIndicesOnCoord = this._dimSummary.dataDimIndicesOnCoord;
  551. for (var i = 0, len = dataDimIndicesOnCoord.length; i < len; i++) {
  552. // Ordinal type originally can be string or number.
  553. // But when an ordinal type is used on coord, it can
  554. // not be string but only number. So we can also use isNaN.
  555. if (isNaN(this._store.get(dataDimIndicesOnCoord[i], idx))) {
  556. return false;
  557. }
  558. }
  559. return true;
  560. };
  561. /**
  562. * Retrieve the index with given name
  563. */
  564. SeriesData.prototype.indexOfName = function (name) {
  565. for (var i = 0, len = this._store.count(); i < len; i++) {
  566. if (this.getName(i) === name) {
  567. return i;
  568. }
  569. }
  570. return -1;
  571. };
  572. SeriesData.prototype.getRawIndex = function (idx) {
  573. return this._store.getRawIndex(idx);
  574. };
  575. SeriesData.prototype.indexOfRawIndex = function (rawIndex) {
  576. return this._store.indexOfRawIndex(rawIndex);
  577. };
  578. /**
  579. * Only support the dimension which inverted index created.
  580. * Do not support other cases until required.
  581. * @param dim concrete dim
  582. * @param value ordinal index
  583. * @return rawIndex
  584. */
  585. SeriesData.prototype.rawIndexOf = function (dim, value) {
  586. var invertedIndices = dim && this._invertedIndicesMap[dim];
  587. if (process.env.NODE_ENV !== 'production') {
  588. if (!invertedIndices) {
  589. throw new Error('Do not supported yet');
  590. }
  591. }
  592. var rawIndex = invertedIndices[value];
  593. if (rawIndex == null || isNaN(rawIndex)) {
  594. return INDEX_NOT_FOUND;
  595. }
  596. return rawIndex;
  597. };
  598. /**
  599. * Retrieve the index of nearest value
  600. * @param dim
  601. * @param value
  602. * @param [maxDistance=Infinity]
  603. * @return If and only if multiple indices has
  604. * the same value, they are put to the result.
  605. */
  606. SeriesData.prototype.indicesOfNearest = function (dim, value, maxDistance) {
  607. return this._store.indicesOfNearest(this._getStoreDimIndex(dim), value, maxDistance);
  608. };
  609. SeriesData.prototype.each = function (dims, cb, ctx) {
  610. 'use strict';
  611. if (zrUtil.isFunction(dims)) {
  612. ctx = cb;
  613. cb = dims;
  614. dims = [];
  615. }
  616. // ctxCompat just for compat echarts3
  617. var fCtx = ctx || this;
  618. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  619. this._store.each(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  620. };
  621. SeriesData.prototype.filterSelf = function (dims, cb, ctx) {
  622. 'use strict';
  623. if (zrUtil.isFunction(dims)) {
  624. ctx = cb;
  625. cb = dims;
  626. dims = [];
  627. }
  628. // ctxCompat just for compat echarts3
  629. var fCtx = ctx || this;
  630. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  631. this._store = this._store.filter(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  632. return this;
  633. };
  634. /**
  635. * Select data in range. (For optimization of filter)
  636. * (Manually inline code, support 5 million data filtering in data zoom.)
  637. */
  638. SeriesData.prototype.selectRange = function (range) {
  639. 'use strict';
  640. var _this = this;
  641. var innerRange = {};
  642. var dims = zrUtil.keys(range);
  643. var dimIndices = [];
  644. zrUtil.each(dims, function (dim) {
  645. var dimIdx = _this._getStoreDimIndex(dim);
  646. innerRange[dimIdx] = range[dim];
  647. dimIndices.push(dimIdx);
  648. });
  649. this._store = this._store.selectRange(innerRange);
  650. return this;
  651. };
  652. /* eslint-enable max-len */
  653. SeriesData.prototype.mapArray = function (dims, cb, ctx) {
  654. 'use strict';
  655. if (zrUtil.isFunction(dims)) {
  656. ctx = cb;
  657. cb = dims;
  658. dims = [];
  659. }
  660. // ctxCompat just for compat echarts3
  661. ctx = ctx || this;
  662. var result = [];
  663. this.each(dims, function () {
  664. result.push(cb && cb.apply(this, arguments));
  665. }, ctx);
  666. return result;
  667. };
  668. SeriesData.prototype.map = function (dims, cb, ctx, ctxCompat) {
  669. 'use strict';
  670. // ctxCompat just for compat echarts3
  671. var fCtx = ctx || ctxCompat || this;
  672. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  673. var list = cloneListForMapAndSample(this);
  674. list._store = this._store.map(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  675. return list;
  676. };
  677. SeriesData.prototype.modify = function (dims, cb, ctx, ctxCompat) {
  678. var _this = this;
  679. // ctxCompat just for compat echarts3
  680. var fCtx = ctx || ctxCompat || this;
  681. if (process.env.NODE_ENV !== 'production') {
  682. zrUtil.each(normalizeDimensions(dims), function (dim) {
  683. var dimInfo = _this.getDimensionInfo(dim);
  684. if (!dimInfo.isCalculationCoord) {
  685. console.error('Danger: only stack dimension can be modified');
  686. }
  687. });
  688. }
  689. var dimIndices = map(normalizeDimensions(dims), this._getStoreDimIndex, this);
  690. // If do shallow clone here, if there are too many stacked series,
  691. // it still cost lots of memory, because `_store.dimensions` are not shared.
  692. // We should consider there probably be shallow clone happen in each series
  693. // in consequent filter/map.
  694. this._store.modify(dimIndices, fCtx ? zrUtil.bind(cb, fCtx) : cb);
  695. };
  696. /**
  697. * Large data down sampling on given dimension
  698. * @param sampleIndex Sample index for name and id
  699. */
  700. SeriesData.prototype.downSample = function (dimension, rate, sampleValue, sampleIndex) {
  701. var list = cloneListForMapAndSample(this);
  702. list._store = this._store.downSample(this._getStoreDimIndex(dimension), rate, sampleValue, sampleIndex);
  703. return list;
  704. };
  705. /**
  706. * Large data down sampling using largest-triangle-three-buckets
  707. * @param {string} valueDimension
  708. * @param {number} targetCount
  709. */
  710. SeriesData.prototype.lttbDownSample = function (valueDimension, rate) {
  711. var list = cloneListForMapAndSample(this);
  712. list._store = this._store.lttbDownSample(this._getStoreDimIndex(valueDimension), rate);
  713. return list;
  714. };
  715. SeriesData.prototype.getRawDataItem = function (idx) {
  716. return this._store.getRawDataItem(idx);
  717. };
  718. /**
  719. * Get model of one data item.
  720. */
  721. // TODO: Type of data item
  722. SeriesData.prototype.getItemModel = function (idx) {
  723. var hostModel = this.hostModel;
  724. var dataItem = this.getRawDataItem(idx);
  725. return new Model(dataItem, hostModel, hostModel && hostModel.ecModel);
  726. };
  727. /**
  728. * Create a data differ
  729. */
  730. SeriesData.prototype.diff = function (otherList) {
  731. var thisList = this;
  732. return new DataDiffer(otherList ? otherList.getStore().getIndices() : [], this.getStore().getIndices(), function (idx) {
  733. return getId(otherList, idx);
  734. }, function (idx) {
  735. return getId(thisList, idx);
  736. });
  737. };
  738. /**
  739. * Get visual property.
  740. */
  741. SeriesData.prototype.getVisual = function (key) {
  742. var visual = this._visual;
  743. return visual && visual[key];
  744. };
  745. SeriesData.prototype.setVisual = function (kvObj, val) {
  746. this._visual = this._visual || {};
  747. if (isObject(kvObj)) {
  748. zrUtil.extend(this._visual, kvObj);
  749. } else {
  750. this._visual[kvObj] = val;
  751. }
  752. };
  753. /**
  754. * Get visual property of single data item
  755. */
  756. // eslint-disable-next-line
  757. SeriesData.prototype.getItemVisual = function (idx, key) {
  758. var itemVisual = this._itemVisuals[idx];
  759. var val = itemVisual && itemVisual[key];
  760. if (val == null) {
  761. // Use global visual property
  762. return this.getVisual(key);
  763. }
  764. return val;
  765. };
  766. /**
  767. * If exists visual property of single data item
  768. */
  769. SeriesData.prototype.hasItemVisual = function () {
  770. return this._itemVisuals.length > 0;
  771. };
  772. /**
  773. * Make sure itemVisual property is unique
  774. */
  775. // TODO: use key to save visual to reduce memory.
  776. SeriesData.prototype.ensureUniqueItemVisual = function (idx, key) {
  777. var itemVisuals = this._itemVisuals;
  778. var itemVisual = itemVisuals[idx];
  779. if (!itemVisual) {
  780. itemVisual = itemVisuals[idx] = {};
  781. }
  782. var val = itemVisual[key];
  783. if (val == null) {
  784. val = this.getVisual(key);
  785. // TODO Performance?
  786. if (zrUtil.isArray(val)) {
  787. val = val.slice();
  788. } else if (isObject(val)) {
  789. val = zrUtil.extend({}, val);
  790. }
  791. itemVisual[key] = val;
  792. }
  793. return val;
  794. };
  795. // eslint-disable-next-line
  796. SeriesData.prototype.setItemVisual = function (idx, key, value) {
  797. var itemVisual = this._itemVisuals[idx] || {};
  798. this._itemVisuals[idx] = itemVisual;
  799. if (isObject(key)) {
  800. zrUtil.extend(itemVisual, key);
  801. } else {
  802. itemVisual[key] = value;
  803. }
  804. };
  805. /**
  806. * Clear itemVisuals and list visual.
  807. */
  808. SeriesData.prototype.clearAllVisual = function () {
  809. this._visual = {};
  810. this._itemVisuals = [];
  811. };
  812. SeriesData.prototype.setLayout = function (key, val) {
  813. isObject(key) ? zrUtil.extend(this._layout, key) : this._layout[key] = val;
  814. };
  815. /**
  816. * Get layout property.
  817. */
  818. SeriesData.prototype.getLayout = function (key) {
  819. return this._layout[key];
  820. };
  821. /**
  822. * Get layout of single data item
  823. */
  824. SeriesData.prototype.getItemLayout = function (idx) {
  825. return this._itemLayouts[idx];
  826. };
  827. /**
  828. * Set layout of single data item
  829. */
  830. SeriesData.prototype.setItemLayout = function (idx, layout, merge) {
  831. this._itemLayouts[idx] = merge ? zrUtil.extend(this._itemLayouts[idx] || {}, layout) : layout;
  832. };
  833. /**
  834. * Clear all layout of single data item
  835. */
  836. SeriesData.prototype.clearItemLayouts = function () {
  837. this._itemLayouts.length = 0;
  838. };
  839. /**
  840. * Set graphic element relative to data. It can be set as null
  841. */
  842. SeriesData.prototype.setItemGraphicEl = function (idx, el) {
  843. var seriesIndex = this.hostModel && this.hostModel.seriesIndex;
  844. setCommonECData(seriesIndex, this.dataType, idx, el);
  845. this._graphicEls[idx] = el;
  846. };
  847. SeriesData.prototype.getItemGraphicEl = function (idx) {
  848. return this._graphicEls[idx];
  849. };
  850. SeriesData.prototype.eachItemGraphicEl = function (cb, context) {
  851. zrUtil.each(this._graphicEls, function (el, idx) {
  852. if (el) {
  853. cb && cb.call(context, el, idx);
  854. }
  855. });
  856. };
  857. /**
  858. * Shallow clone a new list except visual and layout properties, and graph elements.
  859. * New list only change the indices.
  860. */
  861. SeriesData.prototype.cloneShallow = function (list) {
  862. if (!list) {
  863. list = new SeriesData(this._schema ? this._schema : map(this.dimensions, this._getDimInfo, this), this.hostModel);
  864. }
  865. transferProperties(list, this);
  866. list._store = this._store;
  867. return list;
  868. };
  869. /**
  870. * Wrap some method to add more feature
  871. */
  872. SeriesData.prototype.wrapMethod = function (methodName, injectFunction) {
  873. var originalMethod = this[methodName];
  874. if (!zrUtil.isFunction(originalMethod)) {
  875. return;
  876. }
  877. this.__wrappedMethods = this.__wrappedMethods || [];
  878. this.__wrappedMethods.push(methodName);
  879. this[methodName] = function () {
  880. var res = originalMethod.apply(this, arguments);
  881. return injectFunction.apply(this, [res].concat(zrUtil.slice(arguments)));
  882. };
  883. };
  884. // ----------------------------------------------------------
  885. // A work around for internal method visiting private member.
  886. // ----------------------------------------------------------
  887. SeriesData.internalField = function () {
  888. prepareInvertedIndex = function (data) {
  889. var invertedIndicesMap = data._invertedIndicesMap;
  890. zrUtil.each(invertedIndicesMap, function (invertedIndices, dim) {
  891. var dimInfo = data._dimInfos[dim];
  892. // Currently, only dimensions that has ordinalMeta can create inverted indices.
  893. var ordinalMeta = dimInfo.ordinalMeta;
  894. var store = data._store;
  895. if (ordinalMeta) {
  896. invertedIndices = invertedIndicesMap[dim] = new CtorInt32Array(ordinalMeta.categories.length);
  897. // The default value of TypedArray is 0. To avoid miss
  898. // mapping to 0, we should set it as INDEX_NOT_FOUND.
  899. for (var i = 0; i < invertedIndices.length; i++) {
  900. invertedIndices[i] = INDEX_NOT_FOUND;
  901. }
  902. for (var i = 0; i < store.count(); i++) {
  903. // Only support the case that all values are distinct.
  904. invertedIndices[store.get(dimInfo.storeDimIndex, i)] = i;
  905. }
  906. }
  907. });
  908. };
  909. getIdNameFromStore = function (data, dimIdx, idx) {
  910. return convertOptionIdName(data._getCategory(dimIdx, idx), null);
  911. };
  912. /**
  913. * @see the comment of `List['getId']`.
  914. */
  915. getId = function (data, rawIndex) {
  916. var id = data._idList[rawIndex];
  917. if (id == null && data._idDimIdx != null) {
  918. id = getIdNameFromStore(data, data._idDimIdx, rawIndex);
  919. }
  920. if (id == null) {
  921. id = ID_PREFIX + rawIndex;
  922. }
  923. return id;
  924. };
  925. normalizeDimensions = function (dimensions) {
  926. if (!zrUtil.isArray(dimensions)) {
  927. dimensions = dimensions != null ? [dimensions] : [];
  928. }
  929. return dimensions;
  930. };
  931. /**
  932. * Data in excludeDimensions is copied, otherwise transferred.
  933. */
  934. cloneListForMapAndSample = function (original) {
  935. var list = new SeriesData(original._schema ? original._schema : map(original.dimensions, original._getDimInfo, original), original.hostModel);
  936. // FIXME If needs stackedOn, value may already been stacked
  937. transferProperties(list, original);
  938. return list;
  939. };
  940. transferProperties = function (target, source) {
  941. zrUtil.each(TRANSFERABLE_PROPERTIES.concat(source.__wrappedMethods || []), function (propName) {
  942. if (source.hasOwnProperty(propName)) {
  943. target[propName] = source[propName];
  944. }
  945. });
  946. target.__wrappedMethods = source.__wrappedMethods;
  947. zrUtil.each(CLONE_PROPERTIES, function (propName) {
  948. target[propName] = zrUtil.clone(source[propName]);
  949. });
  950. target._calculationInfo = zrUtil.extend({}, source._calculationInfo);
  951. };
  952. makeIdFromName = function (data, idx) {
  953. var nameList = data._nameList;
  954. var idList = data._idList;
  955. var nameDimIdx = data._nameDimIdx;
  956. var idDimIdx = data._idDimIdx;
  957. var name = nameList[idx];
  958. var id = idList[idx];
  959. if (name == null && nameDimIdx != null) {
  960. nameList[idx] = name = getIdNameFromStore(data, nameDimIdx, idx);
  961. }
  962. if (id == null && idDimIdx != null) {
  963. idList[idx] = id = getIdNameFromStore(data, idDimIdx, idx);
  964. }
  965. if (id == null && name != null) {
  966. var nameRepeatCount = data._nameRepeatCount;
  967. var nmCnt = nameRepeatCount[name] = (nameRepeatCount[name] || 0) + 1;
  968. id = name;
  969. if (nmCnt > 1) {
  970. id += '__ec__' + nmCnt;
  971. }
  972. idList[idx] = id;
  973. }
  974. };
  975. }();
  976. return SeriesData;
  977. }();
  978. export default SeriesData;