D3Js图不适用于Angular JS

时间:2016-03-03 02:13:31

标签: javascript angularjs d3.js

我想在我的角度Web应用程序中使用d3.js实现图形。最初,我使用了此site中的代码。我复制了脚本标签之间的所有副本并粘贴到我的控制器文件中,将css代码复制到我的css文件中,将html从网站复制到我的html文件中。如果这令人困惑,我将在下面展示一个例子。当我使用本网站的代码时,一切正常。 但是,我试图从这个site实现一个不同的d3.js图,并按照我之前所做的相同方法,这一次,它不起作用。

我试图在这里粘贴我的代码,但它看起来并不整洁。所以这是jsfiddle BuildVerticaLTree(treeData, "#tree");

此外,如果在html页面中的脚本标记下实现,则js代码正在工作。示例如下:

<script>
$(document).ready(function () {
        //build tree
        function BuildVerticaLTree(treeData, treeContainerDom) {
            var margin = { top: 40, right: 120, bottom: 20, left: 120 };
            var width = 960 - margin.right - margin.left;
            var height = 500 - margin.top - margin.bottom;

            var i = 0, duration = 750;
            var tree = d3.layout.tree()
                .size([height, width]);
            var diagonal = d3.svg.diagonal()
                .projection(function (d) { return [d.x, d.y]; });
            var svg = d3.select(treeContainerDom).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 + ")");
            root = treeData;

            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 * 100; });
                // Declare the nodes…
                var node = svg.selectAll("g.node")
                    .data(nodes, function (d) { return d.id || (d.id = ++i); });
                // Enter the nodes.
                var nodeEnter = node.enter().append("g")
                    .attr("class", "node")
                    .attr("transform", function (d) {
                        return "translate(" + source.x0 + "," + source.y0 + ")";
                    }).on("click", nodeclick);
                nodeEnter.append("circle")
                 .attr("r", 10)
                    .attr("stroke", function (d) { return d.children || d._children ? "steelblue" : "#00c13f"; })
                    .style("fill", function (d) { return d.children || d._children ? "lightsteelblue" : "#fff"; });
                //.attr("r", 10)
                //.style("fill", "#fff");
                nodeEnter.append("text")
                    .attr("y", function (d) {
                        return d.children || d._children ? -18 : 18;
                    })
                    .attr("dy", ".35em")
                    .attr("text-anchor", "middle")
                    .text(function (d) { return d.name; })
                    .style("fill-opacity", 1e-6);
                // Transition nodes to their new position.
                //horizontal tree
                var nodeUpdate = node.transition()
                    .duration(duration)
                    .attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; });
                nodeUpdate.select("circle")
                    .attr("r", 10)
                    .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("circle")
                    .attr("r", 1e-6);
                nodeExit.select("text")
                    .style("fill-opacity", 1e-6);
                // Update the links…
                // Declare the links…
                var link = svg.selectAll("path.link")
                    .data(links, function (d) { return d.target.id; });
                // Enter the links.
                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 nodeclick(d) {
                if (d.children) {
                    d._children = d.children;
                    d.children = null;
                } else {
                    d.children = d._children;
                    d._children = null;
                }
                update(d);
            }
        }

        var treeData =
    {
        "name": "BU Head",
        "children": [
          {
              "name": "Manager",
              "children": [
                {
                    "name": "Team Lead",
                     "children": []
                },
                {
                    "name": "Team Lead",
                    "children": []
                }
              ]
          },
          {
              "name": "Manager",
              "children": []
          }
        ]
    };
        BuildVerticaLTree(treeData, "#tree");
    });
</script>
<div class="container">
    <div id = "tree"></div>  
</div>

0 个答案:

没有答案