dataStackHelper.js 6.5 KB

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