d3.js - How to add links dynamically to a D3 Tree -


i'm trying add links dynamically tree in order create new relationships. intent develop flow diagram, more tree.

the code seems work fine @ first then, after showing bootstrap popover on 1 of nodes, new links disappear. i'm thinking tree being "re-painted" , makeshift links don't included.

here how looks "by default": enter image description here

here how looks new links: enter image description here

after looping ids of nodes should connected, reference nodes , concat pairs output of tree.links. here code

//create links var newlinkpairs = getnodereferencesforlinks(nodes, newlinksids); //add new pair links based on removed duplicates var linkpairs = tree.links(nodes).concat(newlinkpairs);  var linkenter = g.selectall(".node-link") .data(linkpairs) .enter();  linkenter .insert("path", "g") .attr("class", "node-link") .attr("d", (function(d, i){return createconnector(nodeheight, nodewidth)})()) .attr("marker-end", "url(#source-target-marker)"); 

these auxiliary functions:

function createconnector(nodeheight, nodewidth) {     return d3.svg.diagonal()     .source(function(d) {         return {             y: d.source.y + nodewidth,             x: ((d.source.height - nodeheight) /2) + d.source.x}; })             .target(function(d) {                 return {                     y: d.target.y,                     x: ((d.target.height - nodeheight) /2) + d.target.x}; })                     .projection(function(d) { return [d.y, d.x]; });     }  function getnodereferencesforlinks(nodes, newlinksids){     var newlinkpairs =[];      _.each(newlinksids, function(p){         var s = _.findwhere(nodes, {id: p.source});         var t = _.findwhere(nodes, {id: p.target});          newlinkpairs.push({source:s,target:t});         // console.log(s.tasks.join(',') + ' -> ' + t.tasks.join(','));     });      return newlinkpairs; } 

is there better way this?

the code above works supposed to. links disappearing because function created graph being called whenever popover displayed.

the underlying problem here mutation. function created graph modifying original structure consecutive calls failed create new links.

bottom line: mutation evil.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -