JavaScript D3 Tree Layout - Distance between child nodes that have children

Trying to get the tree layout in D3 to render the child nodes with children closer together. Here is the code:

var margin = {
            top: 20,
            right: 120,
            bottom: 20,
            left: 120
    },
    width = 960 - margin.right - margin.left,
    height = 800 - margin.top - margin.bottom;

    var i = 0,
            duration = 750,
            rectW = 185,
            rectH = 45;

    var tree = d3.layout.tree()
        .nodeSize([200, 40]);

    var diagonal = d3.svg.diagonal()
        .projection(function (d) {
        return [d.x + rectW / 2, d.y + rectH / 2];
    });

    var svg = d3.select("#body").append("svg").attr("width", 1000).attr("height", 1000)
        .call(zm = d3.behavior.zoom().scaleExtent([0,1]).on("zoom", redraw))
        .append("g")
        .attr("transform", "translate(" + 30 + "," + 20 + ")");

    //necessary so that zoom knows where to zoom and unzoom from
    zm.translate([350, 20]);

    root.x0 = width / 2;
    root.y0 = 0;

    update(root);

    d3.select("#body").style("height", "800px");

    function update(source) {

        // Compute the new tree layout.
        var nodes = tree.nodes(root).reverse(),
            links = tree.links(nodes);


        // Normalize for fixed-depth.
        nodes.forEach(function (d) {
            d.y = (d.depth * 120);
        });

        // Update the nodes…
        var node = svg.selectAll("g.node")
            .data(nodes, function (d) {
            return d.id || (d.id = ++i);
        });

        // Enter any new nodes at the parent's previous position.
        var nodeEnter = node.enter().append("g")
            .attr("class", "node")
            .attr("transform", function (d) {
                return "translate(" + source.x0 + "," + source.y0 + ")";
            })
            .on("click", click);

        // Add rectangles to nodes
        nodeEnter.append("rect")
            .attr("width", function (d) {                   
                return rectW;
                //return d._children ? "lightsteelblue" : "#fff";
            })
            .attr("height", rectH)
            .attr("class", function (d) {
                return "rect-" + d.state;
            });

        // Add text to nodes
        nodeEnter.append("text")
            .attr("x", rectW / 2)
            .attr("y", rectH / 2)
            .attr("dy", ".35em")
            .attr("text-anchor", "middle")
            .text(function (d) {
                return d.name;
            });

        // Transition nodes to their new position.
        var nodeUpdate = node.transition()
            .duration(duration)
            .attr("transform", function (d) {
                return "translate(" + d.x + "," + d.y + ")";
            });

        nodeUpdate.select("rect")
            .attr("width", rectW)
            .attr("height", rectH)
            .attr("class", function (d) {
                return "rect-" + d.state;
            });

        nodeUpdate.select("text")
            .style("fill-opacity", 1);

        // Update the links…
        var link = svg.selectAll("path.link")
            .data(links, function (d) {
                return d.target.id;
            });

        // Enter any new links at the parent's previous position.
        link.enter().insert("path", "g")
            .attr("class", function (d) {
                return "link " + d.target.dest;
            })
            .attr("x", rectW / 2)
            .attr("d", function (d) {
                var o = {
                    x: source.x0,
                    y: source.y0
                };
                return diagonal({
                    source: o,
                    target: o
                });
            });

        // Transition links to their new position.
        link.transition()
                .duration(duration)
                .attr("d", diagonal);

        // Transition exiting nodes to the parent's new position.
        link.exit().transition()
            .duration(duration)
            .attr("d", function (d) {
                var o = {
                    x: source.x,
                    y: source.y
                };
                return diagonal({
                    source: o,
                    target: o
                });
            }).remove();


        // Update the link labels…
        var linkLabel = svg.selectAll("text.link-label")
            .data(links, function (d) {
                return d.target.id;
            });

        // Enter any new links at the parent's previous position.
        linkLabel.enter().insert("text", "path")
            .text(function (d) {
                return (d.target.state !== "open") ? null : "If " + d.target.dest;
            })
            .attr("class", function (d) {
                return "link-label " + d.target.dest;
            })
            .attr("x",  function (d) {
                return d.target.x + rectW / 2;
            })
            .attr("y", function (d) {
                return d.target.y + rectH * 2 - 30;
            })              
            .attr('text-anchor', 'middle')
            .style("fill-opacity", 0);;

        // Transition link labels
        linkLabel.transition()
            .delay(duration)
            .style("fill-opacity", 1);


        // Stash the old positions for transition.
        nodes.forEach(function (d) {
            d.x0 = d.x;
            d.y0 = d.y;
        });
    }

    // Toggle children on click.
    function click(d) {
        return false;
        if (d.children) {
            d._children = d.children;
            d.children = null;
        } else {
            d.children = d._children;
            d._children = null;
        }
        update(d);
    }

    //Redraw for zoom
    function redraw() {
        //console.log("here", d3.event.translate, d3.event.scale);
        svg.attr("transform",
            "translate(" + d3.event.translate + ")"
            + " scale(" + d3.event.scale + ")");
    }

Here is a jsbin of the above code. What I would like to see is the navy-colored "Node 1" and "Node 2" be closer together while preserving the distance between the nodes without children (grey nodes).

Is this possible and how would I do so?

Answer:1

I have nav tabs within a modal as shown. I am using the meteor framework. Here is the modal link from sign up button (there is another button called login that links to same modal): <button class=...

I have nav tabs within a modal as shown. I am using the meteor framework. Here is the modal link from sign up button (there is another button called login that links to same modal): <button class=...

I have the following javascript: $('#edit_category').on('click','#btn_save_category_name',function(){ currently_edit.text($('#txt_edit_category').val()); edit_category_name(currently_edit,...

I have the following javascript: $('#edit_category').on('click','#btn_save_category_name',function(){ currently_edit.text($('#txt_edit_category').val()); edit_category_name(currently_edit,...

  1. jquery ajax called twice
  2. jquery ajax called multiple times
  3. jquery ajax error called on 200
  4. jquery ajax fail called on 200
  5. jquery ajax get called twice
  6. jquery ajax success called multiple times
  7. jquery ajax success called too early
  8. jquery ajax not called
  9. jquery ajax done called twice
  10. jquery ajax success called twice
  11. jquery datatable ajax called twice
  12. jquery ajax success not called
  13. jquery ajax done not called
  14. jquery ajax always not called
  15. jquery ajax success not called 200
  16. jquery ajax error not called
  17. jquery ajax fail not called
  18. jquery ajax complete not called
  19. jquery ajax callback not called
  20. jquery ajax fail always called

Let say globalX is a global object variable. Lets define a function that takes that variable and inside another function takes same variable and changes the value inside it. var globalX = []; ...

Let say globalX is a global object variable. Lets define a function that takes that variable and inside another function takes same variable and changes the value inside it. var globalX = []; ...

  1. reference global variable python
  2. reference global variable in function python
  3. reference global variable javascript
  4. reference global variable in class python
  5. reference global variable c++
  6. reference global variable in typescript
  7. reference global variable
  8. reference global variable in method python
  9. reference global variable java
  10. reference global variable r
  11. reference global variables php
  12. python reference global variable inside function
  13. reference to global variable is ambiguous
  14. unresolved reference global variable python
  15. javascript reference global variable in function
  16. postman reference global variable
  17. python reference global variable in module
  18. react reference global variable
  19. angular reference global variable
  20. js reference global variable

I'd like to create a parse.com cloud function that will either login or signup a user depending on if the credentials are recognized. I think I'm getting tripped up on promises, particularly on ...

I'd like to create a parse.com cloud function that will either login or signup a user depending on if the credentials are recognized. I think I'm getting tripped up on promises, particularly on ...

  1. parse cloud sign up user