themeRiverLayout.js 5.1 KB

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