SeriesData.js 36 KB

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