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. var zrUtil = require("zrender/lib/core/util");
  20. var numberUtil = require("../../util/number");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. function _default(ecModel, api) {
  40. ecModel.eachSeriesByType('themeRiver', function (seriesModel) {
  41. var data = seriesModel.getData();
  42. var single = seriesModel.coordinateSystem;
  43. var layoutInfo = {}; // use the axis boundingRect for view
  44. var rect = single.getRect();
  45. layoutInfo.rect = rect;
  46. var boundaryGap = seriesModel.get('boundaryGap');
  47. var axis = single.getAxis();
  48. layoutInfo.boundaryGap = boundaryGap;
  49. if (axis.orient === 'horizontal') {
  50. boundaryGap[0] = numberUtil.parsePercent(boundaryGap[0], rect.height);
  51. boundaryGap[1] = numberUtil.parsePercent(boundaryGap[1], rect.height);
  52. var height = rect.height - boundaryGap[0] - boundaryGap[1];
  53. themeRiverLayout(data, seriesModel, height);
  54. } else {
  55. boundaryGap[0] = numberUtil.parsePercent(boundaryGap[0], rect.width);
  56. boundaryGap[1] = numberUtil.parsePercent(boundaryGap[1], rect.width);
  57. var width = rect.width - boundaryGap[0] - boundaryGap[1];
  58. themeRiverLayout(data, seriesModel, width);
  59. }
  60. data.setLayout('layoutInfo', layoutInfo);
  61. });
  62. }
  63. /**
  64. * The layout information about themeriver
  65. *
  66. * @param {module:echarts/data/List} data data in the series
  67. * @param {module:echarts/model/Series} seriesModel the model object of themeRiver series
  68. * @param {number} height value used to compute every series height
  69. */
  70. function themeRiverLayout(data, seriesModel, height) {
  71. if (!data.count()) {
  72. return;
  73. }
  74. var coordSys = seriesModel.coordinateSystem; // the data in each layer are organized into a series.
  75. var layerSeries = seriesModel.getLayerSeries(); // the points in each layer.
  76. var timeDim = data.mapDimension('single');
  77. var valueDim = data.mapDimension('value');
  78. var layerPoints = zrUtil.map(layerSeries, function (singleLayer) {
  79. return zrUtil.map(singleLayer.indices, function (idx) {
  80. var pt = coordSys.dataToPoint(data.get(timeDim, idx));
  81. pt[1] = data.get(valueDim, idx);
  82. return pt;
  83. });
  84. });
  85. var base = computeBaseline(layerPoints);
  86. var baseLine = base.y0;
  87. var ky = height / base.max; // set layout information for each item.
  88. var n = layerSeries.length;
  89. var m = layerSeries[0].indices.length;
  90. var baseY0;
  91. for (var j = 0; j < m; ++j) {
  92. baseY0 = baseLine[j] * ky;
  93. data.setItemLayout(layerSeries[0].indices[j], {
  94. layerIndex: 0,
  95. x: layerPoints[0][j][0],
  96. y0: baseY0,
  97. y: layerPoints[0][j][1] * ky
  98. });
  99. for (var i = 1; i < n; ++i) {
  100. baseY0 += layerPoints[i - 1][j][1] * ky;
  101. data.setItemLayout(layerSeries[i].indices[j], {
  102. layerIndex: i,
  103. x: layerPoints[i][j][0],
  104. y0: baseY0,
  105. y: layerPoints[i][j][1] * ky
  106. });
  107. }
  108. }
  109. }
  110. /**
  111. * Compute the baseLine of the rawdata
  112. * Inspired by Lee Byron's paper Stacked Graphs - Geometry & Aesthetics
  113. *
  114. * @param {Array.<Array>} data the points in each layer
  115. * @return {Object}
  116. */
  117. function computeBaseline(data) {
  118. var layerNum = data.length;
  119. var pointNum = data[0].length;
  120. var sums = [];
  121. var y0 = [];
  122. var max = 0;
  123. var temp;
  124. var base = {};
  125. for (var i = 0; i < pointNum; ++i) {
  126. for (var j = 0, temp = 0; j < layerNum; ++j) {
  127. temp += data[j][i][1];
  128. }
  129. if (temp > max) {
  130. max = temp;
  131. }
  132. sums.push(temp);
  133. }
  134. for (var k = 0; k < pointNum; ++k) {
  135. y0[k] = (max - sums[k]) / 2;
  136. }
  137. max = 0;
  138. for (var l = 0; l < pointNum; ++l) {
  139. var sum = sums[l] + y0[l];
  140. if (sum > max) {
  141. max = sum;
  142. }
  143. }
  144. base.y0 = y0;
  145. base.max = max;
  146. return base;
  147. }
  148. module.exports = _default;