commonLayout.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var _traversalHelper = require("./traversalHelper");
  2. var eachAfter = _traversalHelper.eachAfter;
  3. var eachBefore = _traversalHelper.eachBefore;
  4. var _layoutHelper = require("./layoutHelper");
  5. var init = _layoutHelper.init;
  6. var firstWalk = _layoutHelper.firstWalk;
  7. var secondWalk = _layoutHelper.secondWalk;
  8. var sep = _layoutHelper.separation;
  9. var radialCoordinate = _layoutHelper.radialCoordinate;
  10. var getViewRect = _layoutHelper.getViewRect;
  11. function _default(seriesModel, api) {
  12. var layoutInfo = getViewRect(seriesModel, api);
  13. seriesModel.layoutInfo = layoutInfo;
  14. var layout = seriesModel.get('layout');
  15. var width = 0;
  16. var height = 0;
  17. var separation = null;
  18. if (layout === 'radial') {
  19. width = 2 * Math.PI;
  20. height = Math.min(layoutInfo.height, layoutInfo.width) / 2;
  21. separation = sep(function (node1, node2) {
  22. return (node1.parentNode === node2.parentNode ? 1 : 2) / node1.depth;
  23. });
  24. } else {
  25. width = layoutInfo.width;
  26. height = layoutInfo.height;
  27. separation = sep();
  28. }
  29. var virtualRoot = seriesModel.getData().tree.root;
  30. var realRoot = virtualRoot.children[0];
  31. init(virtualRoot);
  32. eachAfter(realRoot, firstWalk, separation);
  33. virtualRoot.hierNode.modifier = -realRoot.hierNode.prelim;
  34. eachBefore(realRoot, secondWalk);
  35. var left = realRoot;
  36. var right = realRoot;
  37. var bottom = realRoot;
  38. eachBefore(realRoot, function (node) {
  39. var x = node.getLayout().x;
  40. if (x < left.getLayout().x) {
  41. left = node;
  42. }
  43. if (x > right.getLayout().x) {
  44. right = node;
  45. }
  46. if (node.depth > bottom.depth) {
  47. bottom = node;
  48. }
  49. });
  50. var delta = left === right ? 1 : separation(left, right) / 2;
  51. var tx = delta - left.getLayout().x;
  52. var kx = 0;
  53. var ky = 0;
  54. var coorX = 0;
  55. var coorY = 0;
  56. if (layout === 'radial') {
  57. kx = width / (right.getLayout().x + delta + tx); // here we use (node.depth - 1), bucause the real root's depth is 1
  58. ky = height / (bottom.depth - 1 || 1);
  59. eachBefore(realRoot, function (node) {
  60. coorX = (node.getLayout().x + tx) * kx;
  61. coorY = (node.depth - 1) * ky;
  62. var finalCoor = radialCoordinate(coorX, coorY);
  63. node.setLayout({
  64. x: finalCoor.x,
  65. y: finalCoor.y,
  66. rawX: coorX,
  67. rawY: coorY
  68. }, true);
  69. });
  70. } else {
  71. if (seriesModel.get('orient') === 'horizontal') {
  72. ky = height / (right.getLayout().x + delta + tx);
  73. kx = width / (bottom.depth - 1 || 1);
  74. eachBefore(realRoot, function (node) {
  75. coorY = (node.getLayout().x + tx) * ky;
  76. coorX = (node.depth - 1) * kx;
  77. node.setLayout({
  78. x: coorX,
  79. y: coorY
  80. }, true);
  81. });
  82. } else {
  83. kx = width / (right.getLayout().x + delta + tx);
  84. ky = height / (bottom.depth - 1 || 1);
  85. eachBefore(realRoot, function (node) {
  86. coorX = (node.getLayout().x + tx) * kx;
  87. coorY = (node.depth - 1) * ky;
  88. node.setLayout({
  89. x: coorX,
  90. y: coorY
  91. }, true);
  92. });
  93. }
  94. }
  95. }
  96. module.exports = _default;