1

experts out there, i've got a little probem I seem not to be able to figure out by myself.

I have some data that are displayed as a graph. I get a file with the data and extract the required nodes to an array i called 'reqNodes'. The main-node that is supposed to be 'the center' of all the displayed data is fixed, the others have a force layout applied to them. What I struggle with at the moment: if the user clicks on any other node, this one becomes the center of the graph. he is fixed, the old one unfixed. Working so far. But I would like the new center node to transition to the center of the screen. At the moment all the nodes always stay where I clicked them, and so the graph moves with every new node i click, if i don't push it back to the center manually.

If you have any great idea to help me out, I'd be very glad!

greetz, Dave

function update() {
   group.selectAll(".link").remove();
   group.selectAll(".node").remove();
   for(var count = 0; count < reqNodes.length; count++){
        reqNodes[count].fixed = false;
   }
   var fixedNode = reqNodes[getNodesFinalIndex(mittelpunkt)];
   fixedNode.fixed = true;

   fixedNode.px = width/2;
   fixedNode.py = height/2;

   force
        .nodes(reqNodes)
        .links(reqLinks)
        .start();

    link = group.selectAll(".link")
            .data(reqLinks);

    link.enter().append("line")
        .attr("class", "link")
        .style("stroke", "#000")
        .style("stroke-width", function(d) { return Math.sqrt(d.value)*1.2; });

    node = group.selectAll(".node")
            .data(reqNodes);

    node.enter().append("circle")
        .attr("class", "node")
        .attr("r", 7)
        .style("stroke", "black")
        .style("fill", function(d) { return colorArray[d.group]; })
        .call(force.drag);

    for(var oo = 0; oo < group.selectAll(".node").length; oo++){
                if(group.selectAll(".node")[oo].name = mittelpunkt){
                    console.log("node[0][oo]: ");
                    console.log(node[0][oo]);
                    node[0][oo].style.stroke = "red";
                    node[0][oo].style.strokeWidth = 4;  

                }
    }   

    node.append("title")
        .text(function(d) { return d.name; });

    node.on("click", function(d) {
        mittelpunkt = d.name;
            paintIt();
         });

    node.append("text").attr("dx", 12).attr("dy", ".35em").attr("fill", "#aa0101").text(function(d) {
                return d.name
            });

   force.on("tick", function() {
                link.attr("x1", function(d) { return d.source.x; })
                    .attr("y1", function(d) { return d.source.y; })
                    .attr("x2", function(d) { return d.target.x; })
                    .attr("y2", function(d) { return d.target.y; });

                node.attr("cx", function(d) { return d.x; })
                    .attr("cy", function(d) { return d.y; });
     });

Hope i didn't forget something essential. If you miss something, just let me know!

Note:

var group = d3.select("#chart").append("svg").attr("width", width).attr("height", height).attr("id", 'networkBox').append('g');
VividD
  • 10,456
  • 6
  • 64
  • 111
David
  • 335
  • 3
  • 16
  • 1
    [This question](http://stackoverflow.com/questions/18752006/d3-js-force-layout-showing-only-part-of-a-graph/18763335) should help. You can obviously animate the setting of the attributes. – Lars Kotthoff Sep 16 '13 at 14:41
  • As far as i can see your answer is aiming towards setting new coordinates for the node. I already did that, but now the nodes just jump to the new place. I want them to transition to the given coordinates! – David Sep 18 '13 at 08:35
  • Ok, so after setting the coordinates you transition to the new place -- same code as in your `tick` function, except that you have a transition. – Lars Kotthoff Sep 18 '13 at 11:04
  • So how can I select the node connected with the displayed circle? Right now I just found ways to select the visual element of the circle or the fixedNode element, which claims not to have a transition-method. I can set my coordinates through fixedNode, but a transition is not possible. – David Oct 07 '13 at 08:38
  • 1
    In a click handler you can do `d3.select(this)`. – Lars Kotthoff Oct 07 '13 at 08:40
  • thanks, this really works! Only problem is that I still have to rewrite my data every time, so all nodes get remade. The transition I do before is not kept... :/ but maybe I can discover a way to keep the position. – David Oct 07 '13 at 08:53

0 Answers0