dataStack.js 6.4 KB

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