I have been using the genogramLayout for a while now. I was trying to build a collection of nodes that are in the same family tree. So of a certain node, I want all the nodes connected to it (including all children, grandchildren, … parents, grandparents, …). I was trying to walk the tree using node.findNodesConnected(), but that does not jump over the label nodes which the children are connected to.
Yes, the graph is not tree-structured, so you cannot use the various methods of Node to walk the graph.
Here’s some code that I haven’t tried in a long time for looking at ancestors:
function showAncestors() {
var node = myDiagram.selection.first();
if (node === null) return;
var coll = new go.Set();
addAncestors(node, coll);
coll.remove(node); // don't include node itself
myDiagram.selectCollection(coll);
}
function addAncestors(node, coll) {
if (!node) return;
coll.add(node);
var parentmarriagenode = null;
node.findNodesInto().each(function(n) {
if (n.isLinkLabel) parentmarriagenode = n;
});
if (!parentmarriagenode) return;
var marriagelink = parentmarriagenode.labeledLink;
if (!marriagelink) return;
addAncestors(marriagelink.fromNode, coll);
addAncestors(marriagelink.toNode, coll);
}
function showMostRecentCommonAncestors() {
var node1 = null;
var node2 = null;
var it = myDiagram.selection.iterator;
while (it.next()) {
var n = it.value;
if (n instanceof go.Node) {
if (!node1) {
node1 = n;
} else if (!node2) {
node2 = n;
break;
}
}
}
var node1ancestors = new go.Set();
addAncestors(node1, node1ancestors);
var node2ancestors = new go.Set();
addAncestors(node2, node2ancestors);
var recents = new go.Set();
addRecentCommonAncestors(node1ancestors, node2ancestors, recents);
myDiagram.selectCollection(recents);
}
function addRecentCommonAncestors(node1ancestors, node2ancestors, recents) {
var it1 = node1ancestors.iterator;
while (it1.next()) {
var n = it1.value;
if (node2ancestors.contains(n)) {
recents.add(n);
recents.add(findSpouse(n));
break;
}
}
var it2 = node2ancestors.iterator;
while (it2.next()) {
var n = it2.value;
if (node1ancestors.contains(n)) {
recents.add(n);
recents.add(findSpouse(n));
break;
}
}
}
function findSpouse(n) {
var it = n.findLinksConnected().iterator;
while (it.next()) {
var l = it.value;
if (l.category === "Marriage") {
return l.getOtherNode(n);
}
}
return null;
}