fperucic / treant-js

Treant.js - javascript library for drawing tree diagrams
MIT License
864 stars 314 forks source link

Does not work for than one level of nodes #147

Closed CodHeK closed 5 years ago

CodHeK commented 5 years ago

I am trying to plot a tree but it is only working for cases with depth=1 it fails in the case of greater depths.

This is my code:

const genTree = (nodes, word) => {
           let tree = {}, treeArray = [];
           for(const node of nodes) {
               if(node.Word === word) {
                   tree[node.Word] = {
                       text: { name: node.Word }
                   };
               } else {
                   tree[node.Word] = {
                       parent: tree[node.Parent],
                       childrenDropLevel: node.Level,
                       text: { name: node.Word }
                   };
               }
           }
           let config = {
               container: '#tree',
               connectors: {
                   type: 'step'
               }
           };
           for(const [_, v] of Object.entries(tree)) {
               treeArray.push(v);
           }
           treeArray = [ config, ...treeArray ]
           console.log(treeArray);
           return treeArray;
        };

       let chart = new Treant(genTree({{ .Nodes }}, {{ .Word }}), function() {  console.log( 'Tree Loaded' ) }, $ );