dataStack.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. var _util = require("zrender/lib/core/util");
  20. var createHashMap = _util.createHashMap;
  21. var each = _util.each;
  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. // (1) [Caution]: the logic is correct based on the premises:
  41. // data processing stage is blocked in stream.
  42. // See <module:echarts/stream/Scheduler#performDataProcessorTasks>
  43. // (2) Only register once when import repeatly.
  44. // Should be executed after series filtered and before stack calculation.
  45. function _default(ecModel) {
  46. var stackInfoMap = createHashMap();
  47. ecModel.eachSeries(function (seriesModel) {
  48. var stack = seriesModel.get('stack'); // Compatibal: when `stack` is set as '', do not stack.
  49. if (stack) {
  50. var stackInfoList = stackInfoMap.get(stack) || stackInfoMap.set(stack, []);
  51. var data = seriesModel.getData();
  52. var stackInfo = {
  53. // Used for calculate axis extent automatically.
  54. stackResultDimension: data.getCalculationInfo('stackResultDimension'),
  55. stackedOverDimension: data.getCalculationInfo('stackedOverDimension'),
  56. stackedDimension: data.getCalculationInfo('stackedDimension'),
  57. stackedByDimension: data.getCalculationInfo('stackedByDimension'),
  58. isStackedByIndex: data.getCalculationInfo('isStackedByIndex'),
  59. data: data,
  60. seriesModel: seriesModel
  61. }; // If stacked on axis that do not support data stack.
  62. if (!stackInfo.stackedDimension || !(stackInfo.isStackedByIndex || stackInfo.stackedByDimension)) {
  63. return;
  64. }
  65. stackInfoList.length && data.setCalculationInfo('stackedOnSeries', stackInfoList[stackInfoList.length - 1].seriesModel);
  66. stackInfoList.push(stackInfo);
  67. }
  68. });
  69. stackInfoMap.each(calculateStack);
  70. }
  71. function calculateStack(stackInfoList) {
  72. each(stackInfoList, function (targetStackInfo, idxInStack) {
  73. var resultVal = [];
  74. var resultNaN = [NaN, NaN];
  75. var dims = [targetStackInfo.stackResultDimension, targetStackInfo.stackedOverDimension];
  76. var targetData = targetStackInfo.data;
  77. var isStackedByIndex = targetStackInfo.isStackedByIndex; // Should not write on raw data, because stack series model list changes
  78. // depending on legend selection.
  79. var newData = targetData.map(dims, function (v0, v1, dataIndex) {
  80. var sum = targetData.get(targetStackInfo.stackedDimension, dataIndex); // Consider `connectNulls` of line area, if value is NaN, stackedOver
  81. // should also be NaN, to draw a appropriate belt area.
  82. if (isNaN(sum)) {
  83. return resultNaN;
  84. }
  85. var byValue;
  86. var stackedDataRawIndex;
  87. if (isStackedByIndex) {
  88. stackedDataRawIndex = targetData.getRawIndex(dataIndex);
  89. } else {
  90. byValue = targetData.get(targetStackInfo.stackedByDimension, dataIndex);
  91. } // If stackOver is NaN, chart view will render point on value start.
  92. var stackedOver = NaN;
  93. for (var j = idxInStack - 1; j >= 0; j--) {
  94. var stackInfo = stackInfoList[j]; // Has been optimized by inverted indices on `stackedByDimension`.
  95. if (!isStackedByIndex) {
  96. stackedDataRawIndex = stackInfo.data.rawIndexOf(stackInfo.stackedByDimension, byValue);
  97. }
  98. if (stackedDataRawIndex >= 0) {
  99. var val = stackInfo.data.getByRawIndex(stackInfo.stackResultDimension, stackedDataRawIndex); // Considering positive stack, negative stack and empty data
  100. if (sum >= 0 && val > 0 || // Positive stack
  101. sum <= 0 && val < 0 // Negative stack
  102. ) {
  103. sum += val;
  104. stackedOver = val;
  105. break;
  106. }
  107. }
  108. }
  109. resultVal[0] = sum;
  110. resultVal[1] = stackedOver;
  111. return resultVal;
  112. });
  113. targetData.hostModel.setData(newData); // Update for consequent calculation
  114. targetStackInfo.data = newData;
  115. });
  116. }
  117. module.exports = _default;