计算每个树节点的水平距离

时间:2016-06-23 20:41:26

标签: javascript jquery math formula family-tree

我正在为繁殖大鼠创建一个谱系树图,我无法计算每个树节点所需的水平距离,因为后代数量变化或是动态的。

我找到了一个用于绘制树的脚本,但看起来它没有任何文档。

我在这部分需要帮助

var node = new Node({
  title: 'M: ' + pair.male + '<br />F: ' + pair.female,
  stage: stage,
  w: NODE_DIMENSIONS.w,
  h: NODE_DIMENSIONS.h,
  x: COORDINATES.x + (INCREMENTS.x * pair.column), // Formula should adjust based on descendants - HELP
  y: COORDINATES.y + (INCREMENTS.y * pair.generation)
}).attach();

如果您想要使用它并在下面提供完整的代码,那么这里是Fiddle

HTML

<div id="stage"></div>

CSS

.node h4 {
  position: static;
  left: auto;
  bottom: auto;
  width: 80px;
  height: 80px;
  text-align: center;
  vertical-align: middle;
  display: table-cell;
  text-shadow: none;
  color: #ffffff;
  font-weight: bold;
}

脚本

var stage = $('#stage');

var NODE_DIMENSIONS = { w: 80, h: 80 };
var SEGMENT_DIMENSIONS = { h: 5 };
var COORDINATES = { x: 50, y: 50 };
var INCREMENTS = { x: 200, y: 150 };

// Sample JSON array resulting from an AJAX call.
var mating = [{
  "name": "one",
  "male": 1234,
  "female": 5643,
  "male_lineage": null,
  "female_parent": null,
  "generation": 0,
  "column": 0
}, {
  "name": "two",
  "male": 6737,
  "female": 1627,
  "male_lineage": ["four"],
  "female_parent": null,
  "generation": 0,
  "column": 2
}, {
  "name": "three",
  "male": 9332,
  "female": 6227,
  "male_lineage": ["five", "six"],
  "female_parent": null,
  "generation": 0,
  "column": 3
}, {
    "name": "four",
  "male": 1111,
  "female": 6537,
  "male_lineage": null,
  "female_parent": "one",
  "generation": 1,
  "column": 1
}, {
    "name": "five",
  "male": 8853,
  "female": 3189,
  "male_lineage": null,
  "female_parent": "two",
  "generation": 1,
  "column": 2
}, {
    "name": "six",
  "male": 8853,
  "female": 3189,
  "male_lineage": null,
  "female_parent": "three",
  "generation": 1,
  "column": 3
}];

var m = new Map();

for (var i = 0; i < mating.length; i++) {
  var pair = mating[i];
  var node = new Node({
    title: 'M: ' + pair.male + '<br />F: ' + pair.female,
    stage: stage,
    w: NODE_DIMENSIONS.w,
    h: NODE_DIMENSIONS.h,
    x: COORDINATES.x + (INCREMENTS.x * pair.column), // Formula should adjust based on descendants - HELP
    y: COORDINATES.y + (INCREMENTS.y * pair.generation)
  }).attach();

  var element = {
    "pair": pair,
    "node": node
  };

  m.set(pair.name, element);
}

m.forEach(function(element, key, m) {
  // We are going to create 2 segments

  // First is for the male lineage
  if (element.pair.male_lineage != null) {
    for(var i = 0; i < element.pair.male_lineage.length; i++) {
        new Segment({
        h: SEGMENT_DIMENSIONS.h,
        stage: stage,
        origin: element.node,
        destination: m.get(element.pair.male_lineage[i]).node
      }).attach();
    }
  }

  // Last is the female parent
  if (element.pair.female_parent != null) {
    new Segment({
      h: SEGMENT_DIMENSIONS.h,
      stage: stage,
      origin: element.node,
      destination: m.get(element.pair.female_parent).node
    }).attach();
  }
});

1 个答案:

答案 0 :(得分:2)

甚至没有尝试(请原谅我......)深入研究“你的”特定问题的原因,我可以明确地说“树木绘图本身就是递归问题。“

此外,直接映射到HTML“DOM”的问题。(您必须“之后”处理DOM 之前,你决定“树需要看起来像什么。”)

一般来说,树木绘画问题需要从下到上递归地工作:最低的非叶节点平均分配它们的叶子,然后在它们的孩子的最左边和最右边之间等距离定位,然后报告他们的宽度等于他们孩子宽度的范围。 (这个问题的递归描述然后解决自己,或多或少令人满意,包含整个树。)

一旦你确定了整个树想要如何出现,你的下一个(但是,完全不相关的......)问题是:如何操纵 DOM -tree,“或多或少地驱动浏览器”,实际产生你想要的视觉结果。

(“呃......除了Internet Explorer 8。”)