增加D3可折叠树形图中每个节点之间的间距/填充,以避免节点和文本重叠

时间:2018-05-22 10:13:53

标签: javascript d3.js

这里使用D3来构建可折叠树形图,但是由于数据繁重,节点及其相应的文本是重叠的,即在垂直视图上,我尝试了很少的东西但是无法获得所需的结果。如果我能从这里得到一些帮助,那将是很好的,这样我就可以避免图表中文本和节点的重叠。

以下是我所面临的实际问题的截图。 image here

在额外的帮助下,我将包括我的codepen链接。 https://codepen.io/hari2609/pen/YxdYwx

这是代码

D3.js

function demoCallFirst(ele, isRedraw) {
    /*Conversion Part Starts*/
    console.log("perMapData--", isRedraw);
    /*Conversion Part Ends*/
    /*start*/
    var obj = JSON.parse(ele);
    var treeData = [obj];


    // ************** Generate the tree diagram  *****************
    var margin = {
            top: 20,
            right: 120,
            bottom: 20,
            left: 260
        },
        width = 16000 - margin.right - margin.left,
        height = 500 - margin.top - margin.bottom;

    var i = 0,
        duration = 750,
        root;

    var tree = d3.layout.tree()
        .separation(function(a, b) {
            return ((a.parent == root) && (b.parent == root)) ? 3 : 1;
        })
        .size([height, width - 160]);

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

    // Define the div for the tooltip
    var div = d3.select(".tree").append("div")
        .attr("class", "tooltip")
        .attr('id', 'container')
        .style("opacity", 0);

    var svg = d3.select(".tree").append("svg")
        .style("padding-top", "4em")
        .attr("width", width + margin.right + margin.left)
        //.attr("preserveAspectRatio","none")
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        .attr('id', 'sky')
        .attr("transform", "translate(" + margin.left + "," +
            margin.top + ")");

    root = treeData[0];
    root.x0 = height / 2;
    root.y0 = 0;

    // Collapse after the second level
    if (!isRedraw)
        root.children.forEach(collapse);
    update(root);

    // Collapse the node and all it's children
    function collapse(d) {
        console.log('check child------>', d.children);
        if (d.children) {
            d._children = d.children
            d._children.forEach(collapse)
            d.children = null
        }
    }

    d3.select(self.frameElement).style("height", "500px");

    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 * 300
        });

        // 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.nodalColor;
            });

        nodeEnter.append("text")
            .attr("x", function(d) {
                return d.children || d._children ? -13 : 13;
            })
            .attr("dy", ".1em")
            .attr("text-anchor", function(d) {
                return d.children || d._children ? "end" :
                    "start";
            })
            .text(function(d) {
                return d.name;
            })

            .style("fill", function(d) {
                return d.type;
            })
            .call(wrap, 400)
            .style("fill", function(d) {
                return d.color;
            })

            /*if(obj.children.name = 'Capabilities' || 'Offerings' || 'Processes'){
            .style("fill", function(d) { return d._children ? "royalblue" : "#000000";})
            }*/


            /*Logic for displaying no.of children for a parent*/
            /*nodeEnter.append('text')
                .attr("dy", ".35em")
                .attr("x", function(d) {
                    return d.children || d._children ? 4 : -4;
                })
                .attr("text-anchor", function(d) {
                    return d.children || d._children ? "end" :
                        "start";
                })
                .text(function(d) {
                    var children = d.children || d._children;
                    return children ? children.length : null;
                })*/
            /*Logic for displaying no.of children for a parent ends*/
            .on("mouseover", function(d) {
                div.transition()
                    .duration(200)
                    .style("opacity", 0.8);
                div.html(d.name + "</br>")
                    .style("left", (d3.event.pageX) + "px")
                    .style("top", (d3.event.pageY - 38) +
                        "px")
            })
            .on("mouseout", function(d) {
                div.transition()
                    .duration(500)
                    .style("opacity", 0);
            });

        // 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", 8)
            .style("fill", function(d) {
                return d.nodalColor;
            });

        nodeUpdate.select("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 = {
                    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();

        // 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) {
        //console.log('dataa---',d.parentid);
        //var typeId = d.data.parentid;
        console.log('check data--->', d.children);
        console.log('check data--->', d._children);
        if (d.children) {
            d._children = d.children;
            d.children = null;

        } else {
            d.children = d._children;
            d._children = null;

            if (d.childId != null && d.childId != 'undefined' && d.children == null) {
                updateTree(d.childId);
            }

        }
        update(d);

        /*console.log('asapData---',d.aspid);
        passParentId(d.aspid);*/
    }
}

1 个答案:

答案 0 :(得分:0)

尝试增加当前设置为500px(减去边距)的画布的高度,因为它会限制节点之间的垂直间距,无论使用什么其他CSS。