如何在一个页面中获得两个D3应用程序?

时间:2013-12-05 08:38:00

标签: javascript svg d3.js

我有一个显示父子关系的D3应用程序

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<style>

    .node circle {
        cursor: pointer;
        stroke: #3182bd;
        stroke-width: 1.5px;
    }

    .node text {
        font: 10px sans-serif;
        pointer-events: none;
        text-anchor: middle;
    }

    line.link {
        fill: none;
        stroke: #9ecae1;
        stroke-width: 1.5px;
    }

</style>
<script type="text/javascript" src="d3/d3.v3.min.js"></script>
<body>
    <div id="container" class="container">
        <div id="upper" style="width:80%; height:50%; float:left; border:1px; border-color:#000000">

            <script>
                var width = 800,
                height = 400,
                root;

                var force = d3.layout.force()
                .linkDistance(80)
                .charge(-120)
                .gravity(.04)
                .size([width, height])
                .on("tick", tick);

                //adding as svg element
                var svg = d3.select("#upper").append("svg")
                .attr("width", width)
                .attr("height", height);

                var link = svg.selectAll(".link"),
                node = svg.selectAll(".node");

                d3.json("test.json", function(error, json) {
                    root = json;
                    update(); //responsible for creating the layout
                });

                function update() {
                    var nodes = flatten(root),

                    /*
                     *d3.layout.tree() is the starting point 
                     *for tree layouts in D3. 
                     *The call to this function returns an object
                     * that contains a bunch of methods to configure 
                     * the layout and also provides methods to 
                     * compute the layout
                     **/           

                    links = d3.layout.tree().links(nodes);//attach the nodes

                    // Restart the force layout.
                    force
                    .nodes(nodes)
                    .links(links)
                    .start();

                    // Update links.
                    link = link.data(links, function(d) { return d.target.id; });

                    link.exit().remove();

                    link.enter().insert("line", ".node")
                    .attr("class", "link");

                    // Update nodes.
                    node = node.data(nodes, function(d) { return d.id; });

                    node.exit().remove();

                    var nodeEnter = node.enter().append("g")
                    .attr("class", "node")
                    .on("click", click)
                    .call(force.drag);

                    nodeEnter.append("circle")
                    .attr("r", function(d) { return Math.sqrt(d.size) / 10 || 7.5; });

                    nodeEnter.append("text")
                    .attr("dy", ".35em")
                    .text(function(d) { return d.name; });

                    node.select("circle")
                    .style("fill", color);
                }


                /*Giving elements on click*/
                function tick() {
                    link.attr("x1", function(d) { return d.source.x; })
                    .attr("y1", function(d) { return d.source.y; })
                    .attr("x2", function(d) { return d.target.x; })
                    .attr("y2", function(d) { return d.target.y; });

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


                /*Adjusting the color of each node*/
                function color(d) {
                    return d._children ? "#3182bd" // collapsed package
                    : d.children ? "#c6dbef" // expanded package
                    : "#fd8d3c"; // leaf node
                }

                // Toggle children on click.
                function click(d) {
                    if (d3.event.defaultPrevented) return; // ignore drag
                    if (d.children) {
                        d._children = d.children;
                        d.children = null;
                    } else {
                        d.children = d._children;
                        d._children = null;
                    }
                    update();
                }

                // Returns a list of all nodes under the root.
                function flatten(root) {
                    var nodes = [], i = 0;

                    function recurse(node) {
                        if (node.children) node.children.forEach(recurse);
                        if (!node.id) node.id = ++i;
                        nodes.push(node);
                    }

                    recurse(root);
                    return nodes;
                }

            </script>

        </div>
        <div style="width:18%; float:right; border:1px; border-color:#000000">Show The top Values of <br/> <select name="nodes" size="4">
          <option value="1">1</option>
          <option value="2">  2  </option>
          <option value="3">  3  </option>
          <option value="4">  4  </option>
          <option value="5">  5  </option>
          <option value="6">  6  </option>
          <option value="7">  7  </option>
          <option value="8">  8  </option>
          <option value="9">  9  </option>

        </select>
            <input type="submit" value="submit"/>
        </div>

        <div id="lower" style="width:80%; height:50%; float:left; border:1px; border-color:#000000">
            <script>
                var width = 800,
                height = 400,
                root;

                var force = d3.layout.force()
                .linkDistance(80)
                .charge(-120)
                .gravity(.04)
                .size([width, height])
                .on("tick", tick);

                //adding as svg element
                var svg = d3.select("#lower").append("svg")
                .attr("width", width)
                .attr("height", height);

                var link = svg.selectAll(".link"),
                node = svg.selectAll(".node");

                d3.json("test.json", function(error, json) {
                    root = json;
                    update(); //responsible for creating the layout
                });

                function update() {
                    var nodes = flatten(root),

                    /*
                     *d3.layout.tree() is the starting point 
                     *for tree layouts in D3. 
                     *The call to this function returns an object
                     * that contains a bunch of methods to configure 
                     * the layout and also provides methods to 
                     * compute the layout
                     **/           

                    links = d3.layout.tree().links(nodes);//attach the nodes

                    // Restart the force layout.
                    force
                    .nodes(nodes)
                    .links(links)
                    .start();

                    // Update links.
                    link = link.data(links, function(d) { return d.target.id; });

                    link.exit().remove();

                    link.enter().insert("line", ".node")
                    .attr("class", "link");

                    // Update nodes.
                    node = node.data(nodes, function(d) { return d.id; });

                    node.exit().remove();

                    var nodeEnter = node.enter().append("g")
                    .attr("class", "node")
                    .on("click", click)
                    .call(force.drag);

                    nodeEnter.append("circle")
                    .attr("r", function(d) { return Math.sqrt(d.size) / 10 || 7.5; });

                    nodeEnter.append("text")
                    .attr("dy", ".35em")
                    .text(function(d) { return d.name; });

                    node.select("circle")
                    .style("fill", color);
                }


                /*Giving elements on click*/
                function tick() {
                    link.attr("x1", function(d) { return d.source.x; })
                    .attr("y1", function(d) { return d.source.y; })
                    .attr("x2", function(d) { return d.target.x; })
                    .attr("y2", function(d) { return d.target.y; });

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


                /*Adjusting the color of each node*/
                function color(d) {
                    return d._children ? "#3182bd" // collapsed package
                    : d.children ? "#c6dbef" // expanded package
                    : "#fd8d3c"; // leaf node
                }

                // Toggle children on click.
                function click(d) {
                    if (d3.event.defaultPrevented) return; // ignore drag
                    if (d.children) {
                        d._children = d.children;
                        d.children = null;
                    } else {
                        d.children = d._children;
                        d._children = null;
                    }
                    update();
                }

                // Returns a list of all nodes under the root.
                function flatten(root) {
                    var nodes = [], i = 0;

                    function recurse(node) {
                        if (node.children) node.children.forEach(recurse);
                        if (!node.id) node.id = ++i;
                        nodes.push(node);
                    }

                    recurse(root);
                    return nodes;
                }

            </script>
        </div>
        </div>
    </body>
</html>

我有一个jsp页面,我必须并排显示两个D3应用程序。要做到这一点我在svg体中使用两个div但总是第二个即将到来。任何人都可以帮我这个?

0 个答案:

没有答案