SeriesDataSchema.js 8.5 KB

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