javascript - adding nodes to tree force layout - almost working -
I am working with force layout and tree hierarchy.
It almost works and its add nodes and links, but they are not visible, maybe I'm missing something trivial, but I have spent many hours on this ... any help greatly appreciated is.
In this ridicule, after clicking, the third node should be added to the root.
var width = 960, height = 500, root; Var strength = D3 Layout. Force () Link (80). Charge (-320). Guruvita (0.05). Size ([width, height]). On ("Tic", Tic); Var svg = d3.select ("# myviz"). Attached ("svg") .attr ("width", width) .attr ("height", height); Var link = svg.selectAll (".link"), node = svg.selectAll (".node"); Var mydata = '{"name": "start_bubble", "children": [{"name": "child_O1", "size": 31812}, {"name": "child_O2", "size": 31812}] } '; Var myadd = '{"name": "new_child", "size": 100}'; Root = JSON.parse (mydata); Mydadars = JSON Pars (maid); Updates(); force start(); Function Update () {var nodes = flatten (root), link = D3 Layout .ree () Links (nodes); // Restart the Force Layout. Force Node (Nodes) Link (link). Start (); // Update Link Link = link.data (link, function (d) {return d.target.id;}); . Link.exit () removal (); Link () ".Update nodes node = node data (nodes, function (d) {return daid;}); .display node.exit (); var node enter = node.enter () .app ("G") .attr ("class", "node") .on (click "click"). Call (force.drag); NodeEnter.append ("circle") .attr ("r", Function (d) {return math.Sqrt (d.size) / 5 || 14.5;}); NodeEnter.append ("text") .attr ("dy", "2.3em") .text (function (d) {Return d.name;}); node selection ("circle") style ("fill", color); loud start ();} function tick () {link.attr ("x1", function (d ) {Return d.source.x;}) .attr ("y1", function (d) {return d.source.y;}). Etter ("x2", function (d) { Sign d.target.x;}) .attr ("y2", function (d) {returns d.target.y;}); Node.attr ("transform", function (d) {return "translation (" + + Dx + "," + dy + ")";});} function color (d) {returns d._children? "# 3182bd" // compressed package: d.children? "# C6dbef" // Extended package: " # FD8D3C "; // Lead node} // Click Toggle to Function of Children Click on Function (D) {if (d3.event.defaultPrevented) Return; // Avoid Drag If (d.children) {// d._children = d.children; Remove for testing // d.children = null; Root removed for testing. Children [2] = myaddparsed; // add third node} and {// d.children = d._children; Removed for Tesing // d._children = null; Root removed for testing. Children [2] = myaddparsed; // third node} update () add; } // Returns the list of all nodes under the root. Function flatten (root) {var nodes = [], i = 0; Function recurs (node) {if (node. Children) node. children. Ex (recurs); If (! Node.id) node.id = ++ i; Nodes.push (node); } Rikers (Route); Return nodes; }
You come on stackoverflow, And you say: "I index my data with the value of
iinRickers." But you do not maintain the value between the call offlatten. You do not provide the specificity. You can not tell D3 that you have new data instead, youi to 0, increment it with++ i, set it to your new element, and ask D3 to find out that your new element is actually New When you have second element with 1 code equal toid
By dragging it can prevent it from resetting to zero. Here is a .
Comments
Post a Comment