treeLayout.js 5.1 KB

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