themeRiverLayout.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import * as numberUtil from '../../util/number.js';
  42. import { SERIES_TYPE_THEME_RIVER } from './ThemeRiverSeries.js';
  43. import { createSimpleOverallStageHandler } from '../../util/model.js';
  44. export var themeRiverLayoutStageHandler = createSimpleOverallStageHandler(SERIES_TYPE_THEME_RIVER, themeRiverLayout);
  45. function themeRiverLayout(ecModel, api) {
  46. ecModel.eachSeriesByType(SERIES_TYPE_THEME_RIVER, function (seriesModel) {
  47. var data = seriesModel.getData();
  48. var single = seriesModel.coordinateSystem;
  49. var layoutInfo = {};
  50. // use the axis boundingRect for view
  51. var rect = single.getRect();
  52. layoutInfo.rect = rect;
  53. var boundaryGap = seriesModel.get('boundaryGap');
  54. var axis = single.getAxis();
  55. layoutInfo.boundaryGap = boundaryGap;
  56. if (axis.orient === 'horizontal') {
  57. boundaryGap[0] = numberUtil.parsePercent(boundaryGap[0], rect.height);
  58. boundaryGap[1] = numberUtil.parsePercent(boundaryGap[1], rect.height);
  59. var height = rect.height - boundaryGap[0] - boundaryGap[1];
  60. doThemeRiverLayout(data, seriesModel, height);
  61. } else {
  62. boundaryGap[0] = numberUtil.parsePercent(boundaryGap[0], rect.width);
  63. boundaryGap[1] = numberUtil.parsePercent(boundaryGap[1], rect.width);
  64. var width = rect.width - boundaryGap[0] - boundaryGap[1];
  65. doThemeRiverLayout(data, seriesModel, width);
  66. }
  67. data.setLayout('layoutInfo', layoutInfo);
  68. });
  69. }
  70. /**
  71. * The layout information about themeriver
  72. *
  73. * @param data data in the series
  74. * @param seriesModel the model object of themeRiver series
  75. * @param height value used to compute every series height
  76. */
  77. function doThemeRiverLayout(data, seriesModel, height) {
  78. if (!data.count()) {
  79. return;
  80. }
  81. var coordSys = seriesModel.coordinateSystem;
  82. // the data in each layer are organized into a series.
  83. var layerSeries = seriesModel.getLayerSeries();
  84. // the points in each layer.
  85. var timeDim = data.mapDimension('single');
  86. var valueDim = data.mapDimension('value');
  87. var layerPoints = zrUtil.map(layerSeries, function (singleLayer) {
  88. return zrUtil.map(singleLayer.indices, function (idx) {
  89. var pt = coordSys.dataToPoint(data.get(timeDim, idx));
  90. pt[1] = data.get(valueDim, idx);
  91. return pt;
  92. });
  93. });
  94. var base = computeBaseline(layerPoints);
  95. var baseLine = base.y0;
  96. var ky = height / base.max;
  97. // set layout information for each item.
  98. var n = layerSeries.length;
  99. var m = layerSeries[0].indices.length;
  100. var baseY0;
  101. for (var j = 0; j < m; ++j) {
  102. baseY0 = baseLine[j] * ky;
  103. data.setItemLayout(layerSeries[0].indices[j], {
  104. layerIndex: 0,
  105. x: layerPoints[0][j][0],
  106. y0: baseY0,
  107. y: layerPoints[0][j][1] * ky
  108. });
  109. for (var i = 1; i < n; ++i) {
  110. baseY0 += layerPoints[i - 1][j][1] * ky;
  111. data.setItemLayout(layerSeries[i].indices[j], {
  112. layerIndex: i,
  113. x: layerPoints[i][j][0],
  114. y0: baseY0,
  115. y: layerPoints[i][j][1] * ky
  116. });
  117. }
  118. }
  119. }
  120. /**
  121. * Compute the baseLine of the rawdata
  122. * Inspired by Lee Byron's paper Stacked Graphs - Geometry & Aesthetics
  123. *
  124. * @param data the points in each layer
  125. */
  126. function computeBaseline(data) {
  127. var layerNum = data.length;
  128. var pointNum = data[0].length;
  129. var sums = [];
  130. var y0 = [];
  131. var max = 0;
  132. for (var i = 0; i < pointNum; ++i) {
  133. var temp = 0;
  134. for (var j = 0; j < layerNum; ++j) {
  135. temp += data[j][i][1];
  136. }
  137. if (temp > max) {
  138. max = temp;
  139. }
  140. sums.push(temp);
  141. }
  142. for (var k = 0; k < pointNum; ++k) {
  143. y0[k] = (max - sums[k]) / 2;
  144. }
  145. max = 0;
  146. for (var l = 0; l < pointNum; ++l) {
  147. var sum = sums[l] + y0[l];
  148. if (sum > max) {
  149. max = sum;
  150. }
  151. }
  152. return {
  153. y0: y0,
  154. max: max
  155. };
  156. }