SeriesDataSchema.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. import { createHashMap, isObject, retrieve2 } from 'zrender/lib/core/util.js';
  41. import { makeInner } from '../../util/model.js';
  42. import { shouldRetrieveDataByName } from '../Source.js';
  43. var inner = makeInner();
  44. var dimTypeShort = {
  45. float: 'f',
  46. int: 'i',
  47. ordinal: 'o',
  48. number: 'n',
  49. time: 't'
  50. };
  51. /**
  52. * Represents the dimension requirement of a series.
  53. *
  54. * NOTICE:
  55. * When there are too many dimensions in dataset and many series, only the used dimensions
  56. * (i.e., used by coord sys and declared in `series.encode`) are add to `dimensionDefineList`.
  57. * But users may query data by other unused dimension names.
  58. * In this case, users can only query data if and only if they have defined dimension names
  59. * via ec option, so we provide `getDimensionIndexFromSource`, which only query them from
  60. * `source` dimensions.
  61. */
  62. var SeriesDataSchema = /** @class */function () {
  63. function SeriesDataSchema(opt) {
  64. this.dimensions = opt.dimensions;
  65. this._dimOmitted = opt.dimensionOmitted;
  66. this.source = opt.source;
  67. this._fullDimCount = opt.fullDimensionCount;
  68. this._updateDimOmitted(opt.dimensionOmitted);
  69. }
  70. SeriesDataSchema.prototype.isDimensionOmitted = function () {
  71. return this._dimOmitted;
  72. };
  73. SeriesDataSchema.prototype._updateDimOmitted = function (dimensionOmitted) {
  74. this._dimOmitted = dimensionOmitted;
  75. if (!dimensionOmitted) {
  76. return;
  77. }
  78. if (!this._dimNameMap) {
  79. this._dimNameMap = ensureSourceDimNameMap(this.source);
  80. }
  81. };
  82. /**
  83. * @caution Can only be used when `dimensionOmitted: true`.
  84. *
  85. * Get index by user defined dimension name (i.e., not internal generate name).
  86. * That is, get index from `dimensionsDefine`.
  87. * If no `dimensionsDefine`, or no name get, return -1.
  88. */
  89. SeriesDataSchema.prototype.getSourceDimensionIndex = function (dimName) {
  90. return retrieve2(this._dimNameMap.get(dimName), -1);
  91. };
  92. /**
  93. * @caution Can only be used when `dimensionOmitted: true`.
  94. *
  95. * Notice: may return `null`/`undefined` if user not specify dimension names.
  96. */
  97. SeriesDataSchema.prototype.getSourceDimension = function (dimIndex) {
  98. var dimensionsDefine = this.source.dimensionsDefine;
  99. if (dimensionsDefine) {
  100. return dimensionsDefine[dimIndex];
  101. }
  102. };
  103. SeriesDataSchema.prototype.makeStoreSchema = function () {
  104. var dimCount = this._fullDimCount;
  105. var willRetrieveDataByName = shouldRetrieveDataByName(this.source);
  106. var makeHashStrict = !shouldOmitUnusedDimensions(dimCount);
  107. // If source don't have dimensions or series don't omit unsed dimensions.
  108. // Generate from seriesDimList directly
  109. var dimHash = '';
  110. var dims = [];
  111. for (var fullDimIdx = 0, seriesDimIdx = 0; fullDimIdx < dimCount; fullDimIdx++) {
  112. var property = void 0;
  113. var type = void 0;
  114. var ordinalMeta = void 0;
  115. var seriesDimDef = this.dimensions[seriesDimIdx];
  116. // The list has been sorted by `storeDimIndex` asc.
  117. if (seriesDimDef && seriesDimDef.storeDimIndex === fullDimIdx) {
  118. property = willRetrieveDataByName ? seriesDimDef.name : null;
  119. type = seriesDimDef.type;
  120. ordinalMeta = seriesDimDef.ordinalMeta;
  121. seriesDimIdx++;
  122. } else {
  123. var sourceDimDef = this.getSourceDimension(fullDimIdx);
  124. if (sourceDimDef) {
  125. property = willRetrieveDataByName ? sourceDimDef.name : null;
  126. type = sourceDimDef.type;
  127. }
  128. }
  129. dims.push({
  130. property: property,
  131. type: type,
  132. ordinalMeta: ordinalMeta
  133. });
  134. // If retrieving data by index,
  135. // use <index, type, ordinalMeta> to determine whether data can be shared.
  136. // (Because in this case there might be no dimension name defined in dataset, but indices always exists).
  137. // (Indices are always 0, 1, 2, ..., so we can ignore them to shorten the hash).
  138. // Otherwise if retrieving data by property name (like `data: [{aa: 123, bb: 765}, ...]`),
  139. // use <property, type, ordinalMeta> in hash.
  140. if (willRetrieveDataByName && property != null
  141. // For data stack, we have make sure each series has its own dim on this store.
  142. // So we do not add property to hash to make sure they can share this store.
  143. && (!seriesDimDef || !seriesDimDef.isCalculationCoord)) {
  144. dimHash += makeHashStrict
  145. // Use escape character '`' in case that property name contains '$'.
  146. ? property.replace(/\`/g, '`1').replace(/\$/g, '`2')
  147. // For better performance, when there are large dimensions, tolerant this defects that hardly meet.
  148. : property;
  149. }
  150. dimHash += '$';
  151. dimHash += dimTypeShort[type] || 'f';
  152. if (ordinalMeta) {
  153. dimHash += ordinalMeta.uid;
  154. }
  155. dimHash += '$';
  156. }
  157. // Source from endpoint(usually series) will be read differently
  158. // when seriesLayoutBy or startIndex(which is affected by sourceHeader) are different.
  159. // So we use this three props as key.
  160. var source = this.source;
  161. var hash = [source.seriesLayoutBy, source.startIndex, dimHash].join('$$');
  162. return {
  163. dimensions: dims,
  164. hash: hash
  165. };
  166. };
  167. SeriesDataSchema.prototype.makeOutputDimensionNames = function () {
  168. var result = [];
  169. for (var fullDimIdx = 0, seriesDimIdx = 0; fullDimIdx < this._fullDimCount; fullDimIdx++) {
  170. var name_1 = void 0;
  171. var seriesDimDef = this.dimensions[seriesDimIdx];
  172. // The list has been sorted by `storeDimIndex` asc.
  173. if (seriesDimDef && seriesDimDef.storeDimIndex === fullDimIdx) {
  174. if (!seriesDimDef.isCalculationCoord) {
  175. name_1 = seriesDimDef.name;
  176. }
  177. seriesDimIdx++;
  178. } else {
  179. var sourceDimDef = this.getSourceDimension(fullDimIdx);
  180. if (sourceDimDef) {
  181. name_1 = sourceDimDef.name;
  182. }
  183. }
  184. result.push(name_1);
  185. }
  186. return result;
  187. };
  188. SeriesDataSchema.prototype.appendCalculationDimension = function (dimDef) {
  189. this.dimensions.push(dimDef);
  190. dimDef.isCalculationCoord = true;
  191. this._fullDimCount++;
  192. // If append dimension on a data store, consider the store
  193. // might be shared by different series, series dimensions not
  194. // really map to store dimensions.
  195. this._updateDimOmitted(true);
  196. };
  197. return SeriesDataSchema;
  198. }();
  199. export { SeriesDataSchema };
  200. export function isSeriesDataSchema(schema) {
  201. return schema instanceof SeriesDataSchema;
  202. }
  203. export function createDimNameMap(dimsDef) {
  204. var dataDimNameMap = createHashMap();
  205. for (var i = 0; i < (dimsDef || []).length; i++) {
  206. var dimDefItemRaw = dimsDef[i];
  207. var userDimName = isObject(dimDefItemRaw) ? dimDefItemRaw.name : dimDefItemRaw;
  208. if (userDimName != null && dataDimNameMap.get(userDimName) == null) {
  209. dataDimNameMap.set(userDimName, i);
  210. }
  211. }
  212. return dataDimNameMap;
  213. }
  214. export function ensureSourceDimNameMap(source) {
  215. var innerSource = inner(source);
  216. return innerSource.dimNameMap || (innerSource.dimNameMap = createDimNameMap(source.dimensionsDefine));
  217. }
  218. export function shouldOmitUnusedDimensions(dimCount) {
  219. return dimCount > 30;
  220. }