dataStack.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, each } from 'zrender/lib/core/util.js';
  41. import { addSafe } from '../util/number.js';
  42. // (1) [Caution]: the logic is correct based on the premises:
  43. // data processing stage is blocked in stream.
  44. // See <module:echarts/stream/Scheduler#performDataProcessorTasks>
  45. // (2) Only register once when import repeatedly.
  46. // Should be executed after series is filtered and before stack calculation.
  47. export default function dataStack(ecModel) {
  48. var stackInfoMap = createHashMap();
  49. ecModel.eachSeries(function (seriesModel) {
  50. var stack = seriesModel.get('stack');
  51. // Compatible: when `stack` is set as '', do not stack.
  52. if (stack) {
  53. var stackInfoList = stackInfoMap.get(stack) || stackInfoMap.set(stack, []);
  54. var data = seriesModel.getData();
  55. var stackInfo = {
  56. // Used for calculate axis extent automatically.
  57. // TODO: Type getCalculationInfo return more specific type?
  58. stackResultDimension: data.getCalculationInfo('stackResultDimension'),
  59. stackedOverDimension: data.getCalculationInfo('stackedOverDimension'),
  60. stackedDimension: data.getCalculationInfo('stackedDimension'),
  61. stackedByDimension: data.getCalculationInfo('stackedByDimension'),
  62. isStackedByIndex: data.getCalculationInfo('isStackedByIndex'),
  63. data: data,
  64. seriesModel: seriesModel
  65. };
  66. // If stacked on axis that do not support data stack.
  67. if (!stackInfo.stackedDimension || !(stackInfo.isStackedByIndex || stackInfo.stackedByDimension)) {
  68. return;
  69. }
  70. stackInfoList.push(stackInfo);
  71. }
  72. });
  73. // Process each stack group
  74. stackInfoMap.each(function (stackInfoList) {
  75. if (stackInfoList.length === 0) {
  76. return;
  77. }
  78. // Check if stack order needs to be reversed
  79. var firstSeries = stackInfoList[0].seriesModel;
  80. var stackOrder = firstSeries.get('stackOrder') || 'seriesAsc';
  81. if (stackOrder === 'seriesDesc') {
  82. stackInfoList.reverse();
  83. }
  84. // Set stackedOnSeries for each series in the final order
  85. each(stackInfoList, function (stackInfo, index) {
  86. stackInfo.data.setCalculationInfo('stackedOnSeries', index > 0 ? stackInfoList[index - 1].seriesModel : null);
  87. });
  88. // Calculate stack values
  89. calculateStack(stackInfoList);
  90. });
  91. }
  92. function calculateStack(stackInfoList) {
  93. each(stackInfoList, function (targetStackInfo, idxInStack) {
  94. var resultVal = [];
  95. var resultNaN = [NaN, NaN];
  96. var dims = [targetStackInfo.stackResultDimension, targetStackInfo.stackedOverDimension];
  97. var targetData = targetStackInfo.data;
  98. var isStackedByIndex = targetStackInfo.isStackedByIndex;
  99. var stackStrategy = targetStackInfo.seriesModel.get('stackStrategy') || 'samesign';
  100. // Should not write on raw data, because stack series model list changes
  101. // depending on legend selection.
  102. targetData.modify(dims, function (v0, v1, dataIndex) {
  103. var sum = targetData.get(targetStackInfo.stackedDimension, dataIndex);
  104. // Consider `connectNulls` of line area, if value is NaN, stackedOver
  105. // should also be NaN, to draw a appropriate belt area.
  106. if (isNaN(sum)) {
  107. return resultNaN;
  108. }
  109. var byValue;
  110. var stackedDataRawIndex;
  111. if (isStackedByIndex) {
  112. stackedDataRawIndex = targetData.getRawIndex(dataIndex);
  113. } else {
  114. byValue = targetData.get(targetStackInfo.stackedByDimension, dataIndex);
  115. }
  116. // If stackOver is NaN, chart view will render point on value start.
  117. var stackedOver = NaN;
  118. for (var j = idxInStack - 1; j >= 0; j--) {
  119. var stackInfo = stackInfoList[j];
  120. // Has been optimized by inverted indices on `stackedByDimension`.
  121. if (!isStackedByIndex) {
  122. stackedDataRawIndex = stackInfo.data.rawIndexOf(stackInfo.stackedByDimension, byValue);
  123. }
  124. if (stackedDataRawIndex >= 0) {
  125. var val = stackInfo.data.getByRawIndex(stackInfo.stackResultDimension, stackedDataRawIndex);
  126. // Considering positive stack, negative stack and empty data
  127. if (stackStrategy === 'all' // single stack group
  128. || stackStrategy === 'positive' && val > 0 || stackStrategy === 'negative' && val < 0 || stackStrategy === 'samesign' && sum >= 0 && val > 0 // All positive stack
  129. || stackStrategy === 'samesign' && sum <= 0 && val < 0 // All negative stack
  130. ) {
  131. // The sum has to be very small to be affected by the
  132. // floating arithmetic problem. An incorrect result will probably
  133. // cause axis min/max to be filtered incorrectly.
  134. sum = addSafe(sum, val);
  135. stackedOver = val;
  136. break;
  137. }
  138. }
  139. }
  140. resultVal[0] = sum;
  141. resultVal[1] = stackedOver;
  142. return resultVal;
  143. });
  144. });
  145. }