treeLayout.js 5.0 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. var _traversalHelper = require("./traversalHelper");
  20. var eachAfter = _traversalHelper.eachAfter;
  21. var eachBefore = _traversalHelper.eachBefore;
  22. var _layoutHelper = require("./layoutHelper");
  23. var init = _layoutHelper.init;
  24. var firstWalk = _layoutHelper.firstWalk;
  25. var secondWalk = _layoutHelper.secondWalk;
  26. var sep = _layoutHelper.separation;
  27. var radialCoordinate = _layoutHelper.radialCoordinate;
  28. var getViewRect = _layoutHelper.getViewRect;
  29. /*
  30. * Licensed to the Apache Software Foundation (ASF) under one
  31. * or more contributor license agreements. See the NOTICE file
  32. * distributed with this work for additional information
  33. * regarding copyright ownership. The ASF licenses this file
  34. * to you under the Apache License, Version 2.0 (the
  35. * "License"); you may not use this file except in compliance
  36. * with the License. You may obtain a copy of the License at
  37. *
  38. * http://www.apache.org/licenses/LICENSE-2.0
  39. *
  40. * Unless required by applicable law or agreed to in writing,
  41. * software distributed under the License is distributed on an
  42. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  43. * KIND, either express or implied. See the License for the
  44. * specific language governing permissions and limitations
  45. * under the License.
  46. */
  47. function _default(ecModel, api) {
  48. ecModel.eachSeriesByType('tree', function (seriesModel) {
  49. commonLayout(seriesModel, api);
  50. });
  51. }
  52. function commonLayout(seriesModel, api) {
  53. var layoutInfo = getViewRect(seriesModel, api);
  54. seriesModel.layoutInfo = layoutInfo;
  55. var layout = seriesModel.get('layout');
  56. var width = 0;
  57. var height = 0;
  58. var separation = null;
  59. if (layout === 'radial') {
  60. width = 2 * Math.PI;
  61. height = Math.min(layoutInfo.height, layoutInfo.width) / 2;
  62. separation = sep(function (node1, node2) {
  63. return (node1.parentNode === node2.parentNode ? 1 : 2) / node1.depth;
  64. });
  65. } else {
  66. width = layoutInfo.width;
  67. height = layoutInfo.height;
  68. separation = sep();
  69. }
  70. var virtualRoot = seriesModel.getData().tree.root;
  71. var realRoot = virtualRoot.children[0];
  72. if (realRoot) {
  73. init(virtualRoot);
  74. eachAfter(realRoot, firstWalk, separation);
  75. virtualRoot.hierNode.modifier = -realRoot.hierNode.prelim;
  76. eachBefore(realRoot, secondWalk);
  77. var left = realRoot;
  78. var right = realRoot;
  79. var bottom = realRoot;
  80. eachBefore(realRoot, function (node) {
  81. var x = node.getLayout().x;
  82. if (x < left.getLayout().x) {
  83. left = node;
  84. }
  85. if (x > right.getLayout().x) {
  86. right = node;
  87. }
  88. if (node.depth > bottom.depth) {
  89. bottom = node;
  90. }
  91. });
  92. var delta = left === right ? 1 : separation(left, right) / 2;
  93. var tx = delta - left.getLayout().x;
  94. var kx = 0;
  95. var ky = 0;
  96. var coorX = 0;
  97. var coorY = 0;
  98. if (layout === 'radial') {
  99. kx = width / (right.getLayout().x + delta + tx); // here we use (node.depth - 1), bucause the real root's depth is 1
  100. ky = height / (bottom.depth - 1 || 1);
  101. eachBefore(realRoot, function (node) {
  102. coorX = (node.getLayout().x + tx) * kx;
  103. coorY = (node.depth - 1) * ky;
  104. var finalCoor = radialCoordinate(coorX, coorY);
  105. node.setLayout({
  106. x: finalCoor.x,
  107. y: finalCoor.y,
  108. rawX: coorX,
  109. rawY: coorY
  110. }, true);
  111. });
  112. } else {
  113. var orient = seriesModel.getOrient();
  114. if (orient === 'RL' || orient === 'LR') {
  115. ky = height / (right.getLayout().x + delta + tx);
  116. kx = width / (bottom.depth - 1 || 1);
  117. eachBefore(realRoot, function (node) {
  118. coorY = (node.getLayout().x + tx) * ky;
  119. coorX = orient === 'LR' ? (node.depth - 1) * kx : width - (node.depth - 1) * kx;
  120. node.setLayout({
  121. x: coorX,
  122. y: coorY
  123. }, true);
  124. });
  125. } else if (orient === 'TB' || orient === 'BT') {
  126. kx = width / (right.getLayout().x + delta + tx);
  127. ky = height / (bottom.depth - 1 || 1);
  128. eachBefore(realRoot, function (node) {
  129. coorX = (node.getLayout().x + tx) * kx;
  130. coorY = orient === 'TB' ? (node.depth - 1) * ky : height - (node.depth - 1) * ky;
  131. node.setLayout({
  132. x: coorX,
  133. y: coorY
  134. }, true);
  135. });
  136. }
  137. }
  138. }
  139. }
  140. module.exports = _default;