Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript d3 v4与tree.nodeSize一起出现问题。Root设置为0,0,即使在应用了translate之后也是如此_Javascript_Angularjs_D3.js_Svg_Angularjs Directive - Fatal编程技术网

Javascript d3 v4与tree.nodeSize一起出现问题。Root设置为0,0,即使在应用了translate之后也是如此

Javascript d3 v4与tree.nodeSize一起出现问题。Root设置为0,0,即使在应用了translate之后也是如此,javascript,angularjs,d3.js,svg,angularjs-directive,Javascript,Angularjs,D3.js,Svg,Angularjs Directive,我正在尝试用D3V4创建一个树布局。这是通过以下示例实现的(在v3中)。 我试图在angularjs中实现这一点,因此这段代码在指令中。 除了根节点被定位为0,0之外,一切正常,即使我已经在svg上应用了转换(translate)。 请注意,我在d3.tree()上使用nodeSize,以便在节点之间进行分离 这里出了什么问题 link: function(scope, element, attrs) { var margin = { top: 20, right

我正在尝试用D3V4创建一个树布局。这是通过以下示例实现的(在v3中)。 我试图在angularjs中实现这一点,因此这段代码在指令中。
除了根节点被定位为0,0之外,一切正常,即使我已经在svg上应用了转换(translate)。
请注意,我在d3.tree()上使用nodeSize,以便在节点之间进行分离

这里出了什么问题

link: function(scope, element, attrs) {
                var margin = { top: 20, right: 120, bottom: 20, left: 120},
                    width = 1090- margin.left - margin.right,
                    height = 800 - margin.top - margin.bottom;
                var i = 0,
                    duration = 750,
                    rectW = 100,
                    rectH = 30;


                var svg = d3.select(element[0]).append("svg");
                svg.attr("width", width + margin.right + margin.left)
                            .attr("height", height + margin.top + margin.bottom)
                            .append("g")
                            .attr("transform", "translate(" + (width + margin.left + margin.right) / 2 + "," + 0 + ")");
                scope.$watch('model', function(newVals, oldVals) {
                    if (oldVals !== newVals) {
                        return scope.render(newVals);
                    }
                    return {}
                }, true);

                scope.render = function (data) {
                    root = d3.hierarchy(data, function (d) { return d.children; });
                    root.x0 = 0;
                    root.y0 = height/ 2;
                    var treemap = d3.tree()
                                .nodeSize([rectW, rectH])
                                .separation(function (a, b) {
                                    return a.parent == b.parent ? 1.10 : 2;
                                });// make separation accessor 1;

                    // Assigns the x and y position for the nodes
                    var treeData = treemap(root);

                    svg.selectAll('*').remove();


                    function diagonal(source, d) {
                        return "M" + source.x+ "," + source.y
                            + "C" + source.x + "," + (source.y + d.y) / 2
                            + " " + d.x+ "," + (source.y + d.y) / 2
                            + " " + d.x + "," + d.y;
                    }


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

                    // Collapse the node and all it's children
                    function collapse(d) {
                        if (d.children) {
                            d._children = d.children
                            d._children.forEach(collapse)
                            d.children = null
                        }
                    }


                    //});
                    function update(source) {                           




                        //d3.tree().size([height, width]);




                        // Compute the new tree layout.
                        var nodes = treeData.descendants(),
                            links = treeData.descendants().slice(1);

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

                        // 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);

                        nodeEnter.append("rect")
                            .attr("width", rectW)
                            .attr("height", rectH)
                            .attr("stroke", "black")
                            .attr("stroke-width", 1)
                            .style("fill", function(d) {
                                return d._children ? "lightsteelblue" : "#fff";
                            });

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

                        // Transition nodes to their new position.
                        var nodeUpdate = nodeEnter.merge(node);

                        nodeUpdate.transition()
                            .duration(duration)
                            .attr("transform", function(d) {
                                return "translate(" + d.x + "," + d.y + ")";
                            });

                        nodeUpdate.select("rect")
                            .attr("width", rectW)
                            .attr("height", rectH)
                            .attr("stroke", "black")
                            .attr("stroke-width", 1)
                            .style("fill", function(d) {
                                return d._children ? "lightsteelblue" : "#fff";
                            });

                        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.x + "," + source.y + ")";
                            })
                            .remove();

                        nodeExit.select("rect")
                            .attr("width", rectW)
                            .attr("height", rectH)
                            //.attr("width", bbox.getBBox().width)""
                            //.attr("height", bbox.getBBox().height)
                            .attr("stroke", "black")
                            .attr("stroke-width", 1);

                        nodeExit.select("text");

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

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



                        // UPDATE
                        var linkUpdate = linkEnter.merge(link);

                        // Transition links to their new position.
                        linkUpdate.transition()
                            .duration(duration)
                        .attr("d", function(d) {
                                var s = {
                                    x: d.x + rectW / 2,
                                    y: d.y
                                };
                                var dest = {
                                    x: d.parent.x + rectW / 2,
                                    y: d.parent.y + rectH
                                };

                                return diagonal(s, dest)
                            });;

                            //d3.linkHorizontal()
                            // .x(function (d) { return d.y; })
                            // .y(function (d) { return d.x; }));


                        // Transition exiting nodes to the parent's new position.
                        link.exit().transition()
                            .duration(duration)
                            //.attr("d", d3.linkVertical()
                            //.x(function (d) { return d.y; })
                            //.y(function (d) { return d.x; }))
                            .attr("d", function (d) {
                                var o = {
                                    x: source.x,
                                    y: source.y
                                };
                                 return diagonal(o, 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) {
                        if (d.children) {
                            d._children = d.children;
                            d.children = null;
                        } else {
                            d.children = d._children;
                            d._children = null;
                        }
                        update(d);
                    }

                }


            }
答案如下:

指定节点大小时,根节点始终位于⟨0, 0⟩.

在这里你可以找到答案

答案如下:

指定节点大小时,根节点始终位于⟨0, 0⟩.

在这里你可以找到答案