Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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应用程序?_Javascript_Svg_D3.js - Fatal编程技术网

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

Javascript 如何在一个页面中获得两个D3应用程序?,javascript,svg,d3.js,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> </

我有一个显示父子关系的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页面
.节点圆{
光标:指针;
行程:#3182bd;
笔划宽度:1.5px;
}
.节点文本{
字体:10px无衬线;
指针事件:无;
文本锚定:中间;
}
line.link{
填充:无;
行程:#9ecae1;
笔划宽度:1.5px;
}
可变宽度=800,
高度=400,
根;
var-force=d3.layout.force()
.linkDistance(80)
。收费(-120)
重力(.04)
.尺寸([宽度、高度])
.在(“滴答”,滴答)上;
//添加为svg元素
var svg=d3.选择(“#上”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var link=svg.selectAll(“.link”),
node=svg.selectAll(“.node”);
d3.json(“test.json”,函数(错误,json){
root=json;
update();//负责创建布局
});
函数更新(){
变量节点=展平(根),
/*
*d3.layout.tree()是起点
*对于D3中的树布局。
*对该函数的调用返回一个对象
*它包含一系列要配置的方法
*布局,还提供了
*计算布局
**/           
links=d3.layout.tree().links(节点);//附加节点
//重新启动强制布局。
力
.节点(节点)
.链接(links)
.start();
//更新链接。
link=link.data(links,函数(d){返回d.target.id;});
link.exit().remove();
link.enter().insert(“行”,“节点”)
.attr(“类”、“链接”);
//更新节点。
node=node.data(节点,函数(d){return d.id;});
node.exit().remove();
var nodeEnter=node.enter().append(“g”)
.attr(“类”、“节点”)
.on(“单击”,单击)
.呼叫(强制拖动);
nodeEnter.append(“圆”)
.attr(“r”,函数(d){return Math.sqrt(d.size)/10 | | 7.5;});
nodeEnter.append(“文本”)
.attr(“dy”,“.35em”)
.text(函数(d){返回d.name;});
节点。选择(“圆”)
.样式(“填充”,颜色);
}
/*在单击时提供元素*/
函数tick(){
attr(“x1”,函数(d){返回d.source.x;})
.attr(“y1”,函数(d){返回d.source.y;})
.attr(“x2”,函数(d){返回d.target.x;})
.attr(“y2”,函数(d){返回d.target.y;});
attr(“transform”,函数(d){return“translate”(“+d.x+”,“+d.y+”)”);});
}
/*调整每个节点的颜色*/
功能色(d){
退回d.#儿童?#3182bd//折叠包裹
:d.儿童?#c6dbef”//扩展包
:“#fd8d3c”;//叶节点
}
//在单击时切换子项。
功能点击(d){
if(d3.event.defaultPrevented)返回;//忽略拖动
如果(d.儿童){
d、 _children=d.children;
d、 children=null;
}否则{
d、 儿童=d.\U儿童;
d、 _children=null;
}
更新();
}
//返回根目录下所有节点的列表。
函数展平(根){
var节点=[],i=0;
函数递归(节点){
if(node.children)node.children.forEach(recurse);
如果(!node.id)node.id=++i;
nodes.push(节点);
}
递归(根);
返回节点;
}
显示
的最大值 1. 2. 3. 4. 5. 6. 7. 8. 9 可变宽度=800, 高度=400, 根; var-force=d3.layout.force() .linkDistance(80) 。收费(-120) 重力(.04) .尺寸([宽度、高度]) .在(“滴答”,滴答)上; //添加为svg元素 var svg=d3。选择(#下”)。追加(“svg”) .attr(“宽度”,宽度) .attr(“高度”,高度); var link=svg.selectAll(“.link”), node=svg.selectAll(“.node”); d3.json(“test.json”,函数(错误,json){ root=json; update();//负责创建布局 }); 函数更新(){ 变量节点=展平(根), /* *d3.layout.tree()是起点 *对于D3中的树布局。 *对该函数的调用返回一个对象 *