score:1

Accepted answer

You just need to test if it's NOT an object. That works like a charm:

jsonData.forEach(function(link) {
        if (typeof(link.source) != "object"){
            link.source = nodeCircles[link.source] || (nodeCircles[link.source] = {name: link.sourceName, ID: link.source, class: link.sourceClass});
        }
        if (typeof(link.target) != "object"){
            link.target = nodeCircles[link.target] || (nodeCircles[link.target] = {name: link.targetName, ID: link.target, class: link.targetClass});
        } 
    });   

score:0

I'm working on the same problem and got a "solution". It is still a prototype, but maybe it helps you.

Every mouse click on a node calls a function and detects new nodes. They are all saved in nodes and all active, it means all nodes on the display are in activeNodes. All nodes belonging to the path from the root node to the clicked one are stored in pathNodes.The result is, that only the active path is displayed inclusivly the children of the clicked one.

Hopefully it is clearly explained. For abetter understanding please look up the source code at: http://github.com/nextlevelshit/d3_nested_nodes

For something to play with check out: http://dailysh.it/github/d3_nested_nodes/

Here the snippet of my code:

/**
 * Triggering mouse click start
 */

function mousedown() {

  if (mousedown_node !== null) {

    var pathNodes = findPathNodesTo(mousedown_node);

    var point = d3.mouse(this),
      node = {
        id: nodes.length,
        parent: mousedown_node.id
      };
    node.x = point[0];
    node.y = point[1];

    var newNodes = findNodesbyParentId(mousedown_node.id),
      startingPoint = {
        x: mousedown_node.x,
        y: mousedown_node.y
      };

    for (var i = 0; i < pathNodes.length; i++) {
      newNodes.push(pathNodes[i]);
      pathNodes[i].path = true;
    }

    var removeNodes = activeNodes.diff(newNodes);
    var addNodes = newNodes.diff(pathNodes).diff(activeNodes);

    for (var i = 0; i < removeNodes.length; i++) {
      removeNode(removeNodes[i].id);
    }

    for (var i = 0; i < addNodes.length; i++) {
        addNodes[i].x = startingPoint.x;
        addNodes[i].y = startingPoint.y;
        activeNodes.push(addNodes[i]);
        activeLinks.push({source: findNode(addNodes[i].parent), target: findNode(addNodes[i].id)});
    }

      // TODO: Find a smoother way do delay popping out new nodes
    });

  }

  restart();
}

The last problem is to find a smooth way for popping out new nodes...


Related Query