dataStackHelper.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 { each, isString } from 'zrender/lib/core/util.js';
  41. import { isSeriesDataSchema } from './SeriesDataSchema.js';
  42. /**
  43. * Note that it is too complicated to support 3d stack by value
  44. * (have to create two-dimension inverted index), so in 3d case
  45. * we just support that stacked by index.
  46. *
  47. * @param seriesModel
  48. * @param dimensionsInput The same as the input of <module:echarts/data/SeriesData>.
  49. * The input will be modified.
  50. * @param opt
  51. * @param opt.stackedCoordDimension Specify a coord dimension if needed.
  52. * @param opt.byIndex=false
  53. * @return calculationInfo
  54. * {
  55. * stackedDimension: string
  56. * stackedByDimension: string
  57. * isStackedByIndex: boolean
  58. * stackedOverDimension: string
  59. * stackResultDimension: string
  60. * }
  61. */
  62. export function enableDataStack(seriesModel, dimensionsInput, opt) {
  63. opt = opt || {};
  64. var byIndex = opt.byIndex;
  65. var stackedCoordDimension = opt.stackedCoordDimension;
  66. var dimensionDefineList;
  67. var schema;
  68. var store;
  69. if (isLegacyDimensionsInput(dimensionsInput)) {
  70. dimensionDefineList = dimensionsInput;
  71. } else {
  72. schema = dimensionsInput.schema;
  73. dimensionDefineList = schema.dimensions;
  74. store = dimensionsInput.store;
  75. }
  76. // Compatibal: when `stack` is set as '', do not stack.
  77. var mayStack = !!(seriesModel && seriesModel.get('stack'));
  78. var stackedByDimInfo;
  79. var stackedDimInfo;
  80. var stackResultDimension;
  81. var stackedOverDimension;
  82. each(dimensionDefineList, function (dimensionInfo, index) {
  83. if (isString(dimensionInfo)) {
  84. dimensionDefineList[index] = dimensionInfo = {
  85. name: dimensionInfo
  86. };
  87. }
  88. if (mayStack && !dimensionInfo.isExtraCoord) {
  89. // Find the first ordinal dimension as the stackedByDimInfo.
  90. if (!byIndex && !stackedByDimInfo && dimensionInfo.ordinalMeta) {
  91. stackedByDimInfo = dimensionInfo;
  92. }
  93. // Find the first stackable dimension as the stackedDimInfo.
  94. if (!stackedDimInfo && dimensionInfo.type !== 'ordinal' && dimensionInfo.type !== 'time' && (!stackedCoordDimension || stackedCoordDimension === dimensionInfo.coordDim)) {
  95. stackedDimInfo = dimensionInfo;
  96. }
  97. }
  98. });
  99. if (stackedDimInfo && !byIndex && !stackedByDimInfo) {
  100. // Compatible with previous design, value axis (time axis) only stack by index.
  101. // It may make sense if the user provides elaborately constructed data.
  102. byIndex = true;
  103. }
  104. // Add stack dimension, they can be both calculated by coordinate system in `unionExtent`.
  105. // That put stack logic in List is for using conveniently in echarts extensions, but it
  106. // might not be a good way.
  107. if (stackedDimInfo) {
  108. // Use a weird name that not duplicated with other names.
  109. // Also need to use seriesModel.id as postfix because different
  110. // series may share same data store. The stack dimension needs to be distinguished.
  111. stackResultDimension = '__\0ecstackresult_' + seriesModel.id;
  112. stackedOverDimension = '__\0ecstackedover_' + seriesModel.id;
  113. // Create inverted index to fast query index by value.
  114. if (stackedByDimInfo) {
  115. stackedByDimInfo.createInvertedIndices = true;
  116. }
  117. var stackedDimCoordDim_1 = stackedDimInfo.coordDim;
  118. var stackedDimType = stackedDimInfo.type;
  119. var stackedDimCoordIndex_1 = 0;
  120. each(dimensionDefineList, function (dimensionInfo) {
  121. if (dimensionInfo.coordDim === stackedDimCoordDim_1) {
  122. stackedDimCoordIndex_1++;
  123. }
  124. });
  125. var stackedOverDimensionDefine = {
  126. name: stackResultDimension,
  127. coordDim: stackedDimCoordDim_1,
  128. coordDimIndex: stackedDimCoordIndex_1,
  129. type: stackedDimType,
  130. isExtraCoord: true,
  131. isCalculationCoord: true,
  132. storeDimIndex: dimensionDefineList.length
  133. };
  134. var stackResultDimensionDefine = {
  135. name: stackedOverDimension,
  136. // This dimension contains stack base (generally, 0), so do not set it as
  137. // `stackedDimCoordDim` to avoid extent calculation, consider log scale.
  138. coordDim: stackedOverDimension,
  139. coordDimIndex: stackedDimCoordIndex_1 + 1,
  140. type: stackedDimType,
  141. isExtraCoord: true,
  142. isCalculationCoord: true,
  143. storeDimIndex: dimensionDefineList.length + 1
  144. };
  145. if (schema) {
  146. if (store) {
  147. stackedOverDimensionDefine.storeDimIndex = store.ensureCalculationDimension(stackedOverDimension, stackedDimType);
  148. stackResultDimensionDefine.storeDimIndex = store.ensureCalculationDimension(stackResultDimension, stackedDimType);
  149. }
  150. schema.appendCalculationDimension(stackedOverDimensionDefine);
  151. schema.appendCalculationDimension(stackResultDimensionDefine);
  152. } else {
  153. dimensionDefineList.push(stackedOverDimensionDefine);
  154. dimensionDefineList.push(stackResultDimensionDefine);
  155. }
  156. }
  157. return {
  158. stackedDimension: stackedDimInfo && stackedDimInfo.name,
  159. stackedByDimension: stackedByDimInfo && stackedByDimInfo.name,
  160. isStackedByIndex: byIndex,
  161. stackedOverDimension: stackedOverDimension,
  162. stackResultDimension: stackResultDimension
  163. };
  164. }
  165. function isLegacyDimensionsInput(dimensionsInput) {
  166. return !isSeriesDataSchema(dimensionsInput.schema);
  167. }
  168. export function isDimensionStacked(data, stackedDim) {
  169. // Each single series only maps to one pair of axis. So we do not need to
  170. // check stackByDim, whatever stacked by a dimension or stacked by index.
  171. return !!stackedDim && stackedDim === data.getCalculationInfo('stackedDimension');
  172. }
  173. export function getStackedDimension(data, targetDim) {
  174. return isDimensionStacked(data, targetDim) ? data.getCalculationInfo('stackResultDimension') : targetDim;
  175. }