How to center root node circle in a d3.layout.pack graph?

Mohamed Ali JAMAOUI

I am refactoring the d3.layout.pack graph example here into a reusable module. However, the root node is no longer in the center - I couldn't find the culprit.

Here is a jsfiddle demo of the app:

 d3.visio.clustering = function module(){
var width = 700,//default width 
    height= 500;//default height         
var svg; 
//takes a DOM container or an array of DOM containers 
function exports(_selection){
    console.log(_selection);
    _selection.each(function(_data){
        var r = Math.min(width, height) - 10;

        var x = d3.scale.linear().range([0, r]),
            y = d3.scale.linear().range([0, r]),
            node,
            root;


        var pack = d3.layout.pack()
            .size([r, r])
            .value(function(d) { return d.size; });

        //if the svg element is already defined, don't define it again
        if(!svg){
            svg = d3.select(this).append("svg:svg");
        }

        svg.attr("width", width)
           .attr("height", height)
           .append("svg:g")
           .attr("transform", "translate(" + (width - r) / 2 + "," + (height - r) / 2 + ")");

        node = root = _data;

        var nodes = pack.nodes(root);

        svg.selectAll("circle")
            .data(nodes)
            .enter().append("svg:circle")
            .attr("class", function(d) { return d.children ? "parent" : "child"; })
            .attr("cx", function(d) { return d.x; })
            .attr("cy", function(d) { return d.y; })
            .attr("r", function(d) { return d.r; })
            .on("click", function(d) { return zoom(node == d ? root : d); });

          svg.selectAll("text")
            .data(nodes)
            .enter().append("svg:text")
            .attr("class", function(d) { return d.children ? "parent" : "child"; })
            .attr("x", function(d) { return d.x; })
              .attr("y", function(d) { return d.y; })
              .attr("dy", ".35em")
              .attr("text-anchor", "middle")
              .style("opacity", function(d) { return d.r > 20 ? 1 : 0; })
              .text(function(d) { return d.name; });

          d3.select(window).on("click", function() { zoom(root); });

          function zoom(d, i) {
              var k = r / d.r / 2;
              x.domain([d.x - d.r, d.x + d.r]);
              y.domain([d.y - d.r, d.y + d.r]);

              var t = svg.transition()
                  .duration(d3.event.altKey ? 7500 : 750);

              t.selectAll("circle")
                  .attr("cx", function(d) { return x(d.x); })
                  .attr("cy", function(d) { return y(d.y); })
                  .attr("r", function(d) { return k * d.r; });

              t.selectAll("text")
                  .attr("x", function(d) { return x(d.x); })
                  .attr("y", function(d) { return y(d.y); })
                  .style("opacity", function(d) { return k * d.r > 20 ? 1 : 0; });

              node = d;
              d3.event.stopPropagation();
        }

    });
}

//getter and setter for property width  
exports.width = function(_x){
    if(!arguments.length) return width; 
    width = parseInt(_x); 
    return this;//allow method chaining 
}

//getter and setter for property height 
exports.height = function(_x){
    if(!arguments.length) return height;
    height = parseInt(_x); 
    return this;//allow method chaining 
}

//getter and setter for property radius = r 
exports.radius = function(_x){
    if(!arguments.length) return r; 
    r = parseInt(_x); 
    return this//allow method chaining
}

return exports;
   };

   var cluster = d3.visio.clustering()
       .width(document.getElementById("vis").offsetWidth)
       .height(document.getElementById("vis").offsetHeight);

d3.select('#vis')
  .datum(data)
  .call(cluster);

Could you please check what's wrong?

Lars Kotthoff

The problem in your code is that you're appending a g element that is correctly translated, but you're appending everything else to the top level svg, which is not translated. To make it work, you can simply reassign svg with the top level g, i.e.

svg = svg.append("g").attr("transform", "translate(" + ... + ")");

Complete jsfiddle here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to center root node circle in a d3.layout.pack graph?

From Dev

D3 force layout fix root node at the center

From Dev

D3 Circle Pack Layout with a horizontal arrangement

From Dev

Text along circles in a D3 circle pack layout

From Dev

Tooltips for nested circles in D3 circle pack layout

From Dev

Controlling order of circles in D3 circle pack layout algorithm

From Dev

Controlling order of circles in D3 circle pack layout algorithm

From Dev

How to get the same node positions in d3's force layout graph

From Dev

How to get the same node positions in d3's force layout graph

From Dev

How to center a List inside a D3 circle

From Dev

How to center a List inside a D3 circle

From Dev

Circle Pack Layout D3 - remove add nodes on new data

From Dev

Add circle to pack layout

From Dev

D3 - How to convert circle-pack to ellipse-pack?

From Dev

D3 add zoom to circle pack

From Dev

How to create a d3 force layout graph using React

From Dev

How to change the data in a force layout graph dynamically ? D3

From Dev

How to append images as nodes in D3 force layout graph?

From Dev

how to use circle pack layout in ggraph library in r

From Dev

Different colors for each circle - D3 circle pack

From Dev

How to pan to a node using d3's force layout

From Dev

How to show the tooltip on d3 force directed layout node?

From Dev

How to show the tooltip on d3 force directed layout node?

From Dev

How to Hide Overlapping Text of Labels in D3 Zoomable Pack Layout?

From Dev

D3 bubble chart / pack layout - How to make bubbles radiate out from the largest bubbles to the smallest?

From Dev

How to Hide Overlapping Text of Labels in D3 Zoomable Pack Layout?

From Dev

D3 bubble chart / pack layout - How to make bubbles radiate out from the largest bubbles to the smallest?

From Dev

How does D3 node selection work in D3 force graph?

From Dev

Space out nodes evenly around root node in D3 force layout

Related Related

  1. 1

    How to center root node circle in a d3.layout.pack graph?

  2. 2

    D3 force layout fix root node at the center

  3. 3

    D3 Circle Pack Layout with a horizontal arrangement

  4. 4

    Text along circles in a D3 circle pack layout

  5. 5

    Tooltips for nested circles in D3 circle pack layout

  6. 6

    Controlling order of circles in D3 circle pack layout algorithm

  7. 7

    Controlling order of circles in D3 circle pack layout algorithm

  8. 8

    How to get the same node positions in d3's force layout graph

  9. 9

    How to get the same node positions in d3's force layout graph

  10. 10

    How to center a List inside a D3 circle

  11. 11

    How to center a List inside a D3 circle

  12. 12

    Circle Pack Layout D3 - remove add nodes on new data

  13. 13

    Add circle to pack layout

  14. 14

    D3 - How to convert circle-pack to ellipse-pack?

  15. 15

    D3 add zoom to circle pack

  16. 16

    How to create a d3 force layout graph using React

  17. 17

    How to change the data in a force layout graph dynamically ? D3

  18. 18

    How to append images as nodes in D3 force layout graph?

  19. 19

    how to use circle pack layout in ggraph library in r

  20. 20

    Different colors for each circle - D3 circle pack

  21. 21

    How to pan to a node using d3's force layout

  22. 22

    How to show the tooltip on d3 force directed layout node?

  23. 23

    How to show the tooltip on d3 force directed layout node?

  24. 24

    How to Hide Overlapping Text of Labels in D3 Zoomable Pack Layout?

  25. 25

    D3 bubble chart / pack layout - How to make bubbles radiate out from the largest bubbles to the smallest?

  26. 26

    How to Hide Overlapping Text of Labels in D3 Zoomable Pack Layout?

  27. 27

    D3 bubble chart / pack layout - How to make bubbles radiate out from the largest bubbles to the smallest?

  28. 28

    How does D3 node selection work in D3 force graph?

  29. 29

    Space out nodes evenly around root node in D3 force layout

HotTag

Archive