From e3ea5cd4790c05166168a4bf3bc9ab0e8b96c20b Mon Sep 17 00:00:00 2001 From: Azgaar Date: Sun, 12 Jun 2022 17:14:51 +0300 Subject: [PATCH] feat(hierarchy tree): sort by descendants --- modules/dynamic/hierarchy-tree.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/dynamic/hierarchy-tree.js b/modules/dynamic/hierarchy-tree.js index 28a2f10f..1dc94f3f 100644 --- a/modules/dynamic/hierarchy-tree.js +++ b/modules/dynamic/hierarchy-tree.js @@ -200,8 +200,16 @@ const shapesMap = { pentagon: "M0,-14l14,11l-6,14h-16l-6,-14Z" }; +const getSortIndex = node => { + const descendants = node.descendants(); + const secondaryOrigins = descendants.map(({data}) => data.origins.slice(1)).flat(); + + if (secondaryOrigins.length === 0) return node.data.i; + return d3.mean(secondaryOrigins); +}; + function renderTree(root, treeLayout) { - treeLayout(root); + treeLayout(root.sort((a, b) => getSortIndex(a) - getSortIndex(b))); primaryLinks.selectAll("path").data(root.links()).enter().append("path").attr("d", getLinkPath); secondaryLinks.selectAll("path").data(getSecondaryLinks(root)).enter().append("path").attr("d", getLinkPath);