dataStack.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.length && data.setCalculationInfo('stackedOnSeries', stackInfoList[stackInfoList.length - 1].seriesModel);
  71. stackInfoList.push(stackInfo);
  72. }
  73. });
  74. stackInfoMap.each(calculateStack);
  75. }
  76. function calculateStack(stackInfoList) {
  77. each(stackInfoList, function (targetStackInfo, idxInStack) {
  78. var resultVal = [];
  79. var resultNaN = [NaN, NaN];
  80. var dims = [targetStackInfo.stackResultDimension, targetStackInfo.stackedOverDimension];
  81. var targetData = targetStackInfo.data;
  82. var isStackedByIndex = targetStackInfo.isStackedByIndex;
  83. var stackStrategy = targetStackInfo.seriesModel.get('stackStrategy') || 'samesign';
  84. // Should not write on raw data, because stack series model list changes
  85. // depending on legend selection.
  86. targetData.modify(dims, function (v0, v1, dataIndex) {
  87. var sum = targetData.get(targetStackInfo.stackedDimension, dataIndex);
  88. // Consider `connectNulls` of line area, if value is NaN, stackedOver
  89. // should also be NaN, to draw a appropriate belt area.
  90. if (isNaN(sum)) {
  91. return resultNaN;
  92. }
  93. var byValue;
  94. var stackedDataRawIndex;
  95. if (isStackedByIndex) {
  96. stackedDataRawIndex = targetData.getRawIndex(dataIndex);
  97. } else {
  98. byValue = targetData.get(targetStackInfo.stackedByDimension, dataIndex);
  99. }
  100. // If stackOver is NaN, chart view will render point on value start.
  101. var stackedOver = NaN;
  102. for (var j = idxInStack - 1; j >= 0; j--) {
  103. var stackInfo = stackInfoList[j];
  104. // Has been optimized by inverted indices on `stackedByDimension`.
  105. if (!isStackedByIndex) {
  106. stackedDataRawIndex = stackInfo.data.rawIndexOf(stackInfo.stackedByDimension, byValue);
  107. }
  108. if (stackedDataRawIndex >= 0) {
  109. var val = stackInfo.data.getByRawIndex(stackInfo.stackResultDimension, stackedDataRawIndex);
  110. // Considering positive stack, negative stack and empty data
  111. if (stackStrategy === 'all' // single stack group
  112. || stackStrategy === 'positive' && val > 0 || stackStrategy === 'negative' && val < 0 || stackStrategy === 'samesign' && sum >= 0 && val > 0 // All positive stack
  113. || stackStrategy === 'samesign' && sum <= 0 && val < 0 // All negative stack
  114. ) {
  115. // The sum has to be very small to be affected by the
  116. // floating arithmetic problem. An incorrect result will probably
  117. // cause axis min/max to be filtered incorrectly.
  118. sum = addSafe(sum, val);
  119. stackedOver = val;
  120. break;
  121. }
  122. }
  123. }
  124. resultVal[0] = sum;
  125. resultVal[1] = stackedOver;
  126. return resultVal;
  127. });
  128. });
  129. }