dataStack.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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';
  41. import { addSafe } from '../util/number'; // (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 repeatly.
  45. // Should be executed after series 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'); // Compatibal: 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; // Should not write on raw data, because stack series model list changes
  80. // depending on legend selection.
  81. targetData.modify(dims, function (v0, v1, dataIndex) {
  82. var sum = targetData.get(targetStackInfo.stackedDimension, dataIndex); // Consider `connectNulls` of line area, if value is NaN, stackedOver
  83. // should also be NaN, to draw a appropriate belt area.
  84. if (isNaN(sum)) {
  85. return resultNaN;
  86. }
  87. var byValue;
  88. var stackedDataRawIndex;
  89. if (isStackedByIndex) {
  90. stackedDataRawIndex = targetData.getRawIndex(dataIndex);
  91. } else {
  92. byValue = targetData.get(targetStackInfo.stackedByDimension, dataIndex);
  93. } // If stackOver is NaN, chart view will render point on value start.
  94. var stackedOver = NaN;
  95. for (var j = idxInStack - 1; j >= 0; j--) {
  96. var stackInfo = stackInfoList[j]; // Has been optimized by inverted indices on `stackedByDimension`.
  97. if (!isStackedByIndex) {
  98. stackedDataRawIndex = stackInfo.data.rawIndexOf(stackInfo.stackedByDimension, byValue);
  99. }
  100. if (stackedDataRawIndex >= 0) {
  101. var val = stackInfo.data.getByRawIndex(stackInfo.stackResultDimension, stackedDataRawIndex); // Considering positive stack, negative stack and empty data
  102. if (sum >= 0 && val > 0 || // Positive stack
  103. sum <= 0 && val < 0 // Negative stack
  104. ) {
  105. // The sum should be as less as possible to be effected
  106. // by floating arithmetic problem. A wrong result probably
  107. // filtered incorrectly by axis min/max.
  108. sum = addSafe(sum, val);
  109. stackedOver = val;
  110. break;
  111. }
  112. }
  113. }
  114. resultVal[0] = sum;
  115. resultVal[1] = stackedOver;
  116. return resultVal;
  117. });
  118. });
  119. }