dataStack.js 5.8 KB

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