treeLayout.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { eachAfter, eachBefore } from './traversalHelper.js';
  41. import { init, firstWalk, secondWalk, separation as sep, radialCoordinate, getViewRect } from './layoutHelper.js';
  42. export default function treeLayout(ecModel, api) {
  43. ecModel.eachSeriesByType('tree', function (seriesModel) {
  44. commonLayout(seriesModel, api);
  45. });
  46. }
  47. function commonLayout(seriesModel, api) {
  48. var layoutInfo = getViewRect(seriesModel, api);
  49. seriesModel.layoutInfo = layoutInfo;
  50. var layout = seriesModel.get('layout');
  51. var width = 0;
  52. var height = 0;
  53. var separation = null;
  54. if (layout === 'radial') {
  55. width = 2 * Math.PI;
  56. height = Math.min(layoutInfo.height, layoutInfo.width) / 2;
  57. separation = sep(function (node1, node2) {
  58. return (node1.parentNode === node2.parentNode ? 1 : 2) / node1.depth;
  59. });
  60. } else {
  61. width = layoutInfo.width;
  62. height = layoutInfo.height;
  63. separation = sep();
  64. }
  65. var virtualRoot = seriesModel.getData().tree.root;
  66. var realRoot = virtualRoot.children[0];
  67. if (realRoot) {
  68. init(virtualRoot);
  69. eachAfter(realRoot, firstWalk, separation);
  70. virtualRoot.hierNode.modifier = -realRoot.hierNode.prelim;
  71. eachBefore(realRoot, secondWalk);
  72. var left_1 = realRoot;
  73. var right_1 = realRoot;
  74. var bottom_1 = realRoot;
  75. eachBefore(realRoot, function (node) {
  76. var x = node.getLayout().x;
  77. if (x < left_1.getLayout().x) {
  78. left_1 = node;
  79. }
  80. if (x > right_1.getLayout().x) {
  81. right_1 = node;
  82. }
  83. if (node.depth > bottom_1.depth) {
  84. bottom_1 = node;
  85. }
  86. });
  87. var delta = left_1 === right_1 ? 1 : separation(left_1, right_1) / 2;
  88. var tx_1 = delta - left_1.getLayout().x;
  89. var kx_1 = 0;
  90. var ky_1 = 0;
  91. var coorX_1 = 0;
  92. var coorY_1 = 0;
  93. if (layout === 'radial') {
  94. kx_1 = width / (right_1.getLayout().x + delta + tx_1);
  95. // here we use (node.depth - 1), bucause the real root's depth is 1
  96. ky_1 = height / (bottom_1.depth - 1 || 1);
  97. eachBefore(realRoot, function (node) {
  98. coorX_1 = (node.getLayout().x + tx_1) * kx_1;
  99. coorY_1 = (node.depth - 1) * ky_1;
  100. var finalCoor = radialCoordinate(coorX_1, coorY_1);
  101. node.setLayout({
  102. x: finalCoor.x,
  103. y: finalCoor.y,
  104. rawX: coorX_1,
  105. rawY: coorY_1
  106. }, true);
  107. });
  108. } else {
  109. var orient_1 = seriesModel.getOrient();
  110. if (orient_1 === 'RL' || orient_1 === 'LR') {
  111. ky_1 = height / (right_1.getLayout().x + delta + tx_1);
  112. kx_1 = width / (bottom_1.depth - 1 || 1);
  113. eachBefore(realRoot, function (node) {
  114. coorY_1 = (node.getLayout().x + tx_1) * ky_1;
  115. coorX_1 = orient_1 === 'LR' ? (node.depth - 1) * kx_1 : width - (node.depth - 1) * kx_1;
  116. node.setLayout({
  117. x: coorX_1,
  118. y: coorY_1
  119. }, true);
  120. });
  121. } else if (orient_1 === 'TB' || orient_1 === 'BT') {
  122. kx_1 = width / (right_1.getLayout().x + delta + tx_1);
  123. ky_1 = height / (bottom_1.depth - 1 || 1);
  124. eachBefore(realRoot, function (node) {
  125. coorX_1 = (node.getLayout().x + tx_1) * kx_1;
  126. coorY_1 = orient_1 === 'TB' ? (node.depth - 1) * ky_1 : height - (node.depth - 1) * ky_1;
  127. node.setLayout({
  128. x: coorX_1,
  129. y: coorY_1
  130. }, true);
  131. });
  132. }
  133. }
  134. }
  135. }