D3组织结构图 - 如何使未使用的“四肢”崩溃

时间:2016-05-19 18:30:06

标签: javascript d3.js orgchart

我想要破坏未使用的组织结构图的“四肢”。该图表看起来很好,运行良好。当我打开一个新的时候,我会希望其他肢体崩溃。显然,需要将其限制为与点击元素时父项的距离相等。

提前致谢

    <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Underwriting Rule Chart</title>
    <!-- link CSS stylesheets -->
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/style.css"/> 

    <!-- link JS, includes jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
    <script src="js/app.js"></script>
    <!-- creates topbar for mock site using Bootstrap -->
    <div class="topbar">
        <div class="topbar-inner">
            <div>
                <img class="img" src="images/iSite.gif">         
            </div>
        </div>
    </div>
</head>
<body>
</body>
<footer>
    <!-- creates description bar for mock site -->
    <div>
        <div id="draggable-element" class="desc">Description
            <div id="title"></div>
            <div id="desc"></div>
        </div>
    </div>
</footer>
</html>

JavaScript的:

    document.addEventListener("DOMContentLoaded", function() {
    var margin = {
            top: 50,
            right: 300,
            bottom: 50,
            left: 300
        },
        width = 1280 - margin.right - margin.left,
        height = 720 - margin.top - margin.bottom;

    var i = 0,
        duration = 750,
        root;

    var tree = d3.layout.tree()
        .size([height, width]);

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

    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.right + margin.left)
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    //temp JSON data, move to external file on server     

    root = data;
    root.x0 = height / 2;
    root.y0 = 0;

    function collapse(d) {
        if (d.children) {
            d._children = d.children;
            d._children.forEach(collapse);
            d.children = null;
        }
    }

    root.children.forEach(collapse);
    update(root);

    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 * 220; // change to increase/decrease the distance between "limbs"
        });

        // 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.y0 + "," + source.x0 + ")";
            })
            .on("click", click);

        nodeEnter.append("circle")
            .attr("r", 1e-6)
            .style("fill", function(d) {
                return d._children ? "yellow" : "#fdbd44";
            });

        nodeEnter.append("text")
            .attr("x", function(d) {
                return d.children || d._children ? -10 : 10;
            })
            .attr("dy", ".35em")
            .attr("text-anchor", function(d) {
                return d.children || d._children ? "end" : "start";
            })
            .text(function(d) {
                return d.fname;
            })
            // on click of element display the description
            .on("click", function(d) {
                var theDiv = document.getElementById("desc");
                var theDivNodes = document.getElementById("desc").childNodes;
                var content = document.createTextNode(d.desc);
                var title = document.getElementById("title");
                var titleNodes = document.getElementById("title").childNodes;
                var titlecontent = document.createTextNode(d.fname);
                if (theDivNodes[0])
                    theDiv.replaceChild(content, theDivNodes[0]);
                else
                    theDiv.appendChild(content);
                if (titleNodes[0])
                    title.replaceChild(titlecontent, titleNodes[0]);
                else
                    title.appendChild(titlecontent);
            })
            .style("fill-opacity", 1e-6);

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

        nodeUpdate.select("circle")
            .attr("r", 5) //attr changes size of the circle 
            .style("fill", function(d) {
                return d._children ? "#fdbf44" : "white";
            });

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

        // Transition exiting nodes to the parent's new position.
        var nodeExit = node.exit().transition()
            .duration(duration)
            .attr("transform", function(d) {
                return "translate(" + source.y + "," + source.x + ")";
            })
            .remove();

        nodeExit.select("circle")
            .attr("r", 1e-6);

        nodeExit.select("text")
            .style("fill-opacity", 1e-6);

        // 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", "link")
            .attr("d", function(d) {
                var o = {
                    y: source.y0,
                    x: source.x0
                };
                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 = {
                    y: source.y,
                    x: source.x
                };
                return diagonal({
                    source: o,
                    target: o
                });
            })
            .remove();

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

    }
    // Toggle children on click.
    function click(d) {
        if (d.children) {
            d._children = d.children;
            d.children = null;
        } else {
            d.children = d._children;
            d._children = null;
        }
        update(d);
    }
    //JS to create a draggable div
    var selected = null,
        x_pos = 0,
        y_pos = 0,
        x_elem = 0,
        y_elem = 0;

    // Will be called when user starts dragging an element
    function _drag_init(elem) {
        // Store the object of the element which needs to be moved
        selected = elem;
        x_elem = x_pos - selected.offsetLeft;
        y_elem = y_pos - selected.offsetTop;
    }

    // Will be called when user dragging an element
    function _move_elem(e) {
        x_pos = document.all ? window.event.clientX : e.pageX;
        y_pos = document.all ? window.event.clientY : e.pageY;
        if (selected !== null) {
            selected.style.left = (x_pos - x_elem) + 'px';
            selected.style.top = (y_pos - y_elem) + 'px';
        }
    }

    // Destroy the object when we are done
    function _destroy() {
        selected = null;
    }

    // Bind the functions...
    document.getElementById('draggable-element').onmousedown = function() {
        _drag_init(this);
        return false;
    };

    document.onmousemove = _move_elem;
    document.onmouseup = _destroy;
});

1 个答案:

答案 0 :(得分:0)

尝试将点击功能修改为:

function click(d) {
    root.children.forEach(collapse);
    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    }
    update(d);
}

基本上它会使所有&#34;肢体崩溃&#34;之前&#34;扩大&#34;新的那一个。可能会有一些小错误,但我可以帮助您解决,如果您可以在某处托管您的工作代码,那将更容易。