themeRiverLayout.js 5.1 KB

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