@antv/hierarchy 0.6.8 → 0.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
- function positionNode(node, previousNode, indent, dropCap) {
1
+ const util = require('../util');
2
+
3
+ function positionNode(node, previousNode, indent, dropCap, align) {
2
4
  // caculate the node's horizontal offset DX, dx's type might be number or function
3
5
  const displacementX = typeof indent === 'function' ? indent(node) : indent * node.depth;
4
6
 
@@ -15,13 +17,26 @@ function positionNode(node, previousNode, indent, dropCap) {
15
17
  }
16
18
 
17
19
  node.x += displacementX;
18
- node.y = previousNode ? previousNode.y + previousNode.height : 0;
20
+ if (previousNode) {
21
+ node.y = previousNode.y + util.getHeight(previousNode, node, align);
22
+ if (node.parent.id !== previousNode.parent?.id) {
23
+ // previous node has different parent
24
+ const index = node.parent.children.findIndex(n => n.id === node.id);
25
+ const preNode = node.parent.children[index - 1];
26
+ if (preNode) {
27
+ const preY = preNode.y + util.getHeight(preNode, node, align);
28
+ node.y = preY > node.y ? preY : node.y;
29
+ }
30
+ }
31
+ } else {
32
+ node.y = 0;
33
+ }
19
34
  return;
20
35
  }
21
- module.exports = (root, indent, dropCap) => {
36
+ module.exports = (root, indent, dropCap, align) => {
22
37
  let previousNode = null;
23
38
  root.eachNode(node => {
24
- positionNode(node, previousNode, indent, dropCap);
39
+ positionNode(node, previousNode, indent, dropCap, align);
25
40
  previousNode = node;
26
41
  });
27
42
  };
package/src/util.js CHANGED
@@ -1,4 +1,18 @@
1
1
  const { mix } = require('@antv/util');
2
+
3
+ /**
4
+ * Get average height or height for node's position calculation, according to align.
5
+ * @param {*} preNode previous node
6
+ * @param {*} node current node, whose position is going to be calculated
7
+ * @param {'center' | undefined} align 'center' means nodes align at the center, other value means align at the left-top
8
+ * @param {string} heightField field name for height value on preNode and node
9
+ * @return {number} the height for calculation
10
+ */
11
+ function getHeight(preNode, node, align, heightField = 'height') {
12
+ return align === 'center' ? (preNode[heightField] + node[heightField]) / 2 : preNode.height;
13
+ }
14
+
2
15
  module.exports = {
3
- assign: mix
16
+ assign: mix,
17
+ getHeight
4
18
  };