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
D3.js 单击D3DI图上的事件_D3.js - Fatal编程技术网

D3.js 单击D3DI图上的事件

D3.js 单击D3DI图上的事件,d3.js,D3.js,我们使用的是来自以下链接的D3强制有向图: http://bl.ocks.org/jose187/4733747 我无法理解如何启用单击节点。注意到我们有坐标,但不确定如何“附加”click事件处理程序 如果您有任何想法,我们将不胜感激。在D3中,听众可以通过以下方式进行连接: 为指定事件类型名的每个选定元素添加或删除侦听器 因此,对于单击事件,简单如下: node.on("click", function(){ //your code here } 使用您的代码检查演示: .链

我们使用的是来自以下链接的D3强制有向图:

http://bl.ocks.org/jose187/4733747
我无法理解如何启用单击节点。注意到我们有坐标,但不确定如何“附加”click事件处理程序


如果您有任何想法,我们将不胜感激。

在D3中,听众可以通过以下方式进行连接:

为指定事件类型名的每个选定元素添加或删除侦听器

因此,对于单击事件,简单如下:

node.on("click", function(){
    //your code here
}
使用您的代码检查演示:


.链接{
冲程:#aaa;
}
.节点文本{
冲程:#333;
光标:指针;
}
.节点圆{
冲程:#fff;
笔画宽度:3px;
填充:#555;
}
可变宽度=400,
高度=300
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var-force=d3.layout.force()
.重力(.05)
.距离(100)
。收费(-100)
.尺寸([宽度、高度]);
var json={
“节点”:[
{“名称”:“节点1”,“组”:1},
{“name”:“node2”,“group”:2},
{“name”:“node3”,“group”:2},
{“名称”:“节点4”,“组”:3}
],
“链接”:[
{“源”:2,“目标”:1,“权重”:1},
{“源”:0,“目标”:2,“权重”:3}
]
};
力
.nodes(json.nodes)
.links(json.links)
.start();
var link=svg.selectAll(“.link”)
.data(json.links)
.enter().append(“行”)
.attr(“类”、“链接”)
.style(“笔划宽度”,函数(d){return Math.sqrt(d.weight);});
var node=svg.selectAll(“.node”)
.data(json.nodes)
.enter().append(“g”)
.attr(“类”、“节点”)
.呼叫(强制拖动);
node.append(“圆”)
.attr(“r”、“5”);
node.append(“文本”)
.attr(“dx”,12)
.attr(“dy”,“.35em”)
.text(函数(d){返回d.name});
节点上(“单击”,功能(d){
警报(“你好,我是”+d.name)
})
force.on(“勾号”,函数(){
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+”)”);});
});

在D3中,侦听器通过以下方式连接:

为指定事件类型名的每个选定元素添加或删除侦听器

因此,对于单击事件,简单如下:

node.on("click", function(){
    //your code here
}
使用您的代码检查演示:


.链接{
冲程:#aaa;
}
.节点文本{
冲程:#333;
光标:指针;
}
.节点圆{
冲程:#fff;
笔画宽度:3px;
填充:#555;
}
可变宽度=400,
高度=300
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var-force=d3.layout.force()
.重力(.05)
.距离(100)
。收费(-100)
.尺寸([宽度、高度]);
var json={
“节点”:[
{“名称”:“节点1”,“组”:1},
{“name”:“node2”,“group”:2},
{“name”:“node3”,“group”:2},
{“名称”:“节点4”,“组”:3}
],
“链接”:[
{“源”:2,“目标”:1,“权重”:1},
{“源”:0,“目标”:2,“权重”:3}
]
};
力
.nodes(json.nodes)
.links(json.links)
.start();
var link=svg.selectAll(“.link”)
.data(json.links)
.enter().append(“行”)
.attr(“类”、“链接”)
.style(“笔划宽度”,函数(d){return Math.sqrt(d.weight);});
var node=svg.selectAll(“.node”)
.data(json.nodes)
.enter().append(“g”)
.attr(“类”、“节点”)
.呼叫(强制拖动);
node.append(“圆”)
.attr(“r”、“5”);
node.append(“文本”)
.attr(“dx”,12)
.attr(“dy”,“.35em”)
.text(函数(d){返回d.name});
节点上(“单击”,功能(d){
警报(“你好,我是”+d.name)
})
force.on(“勾号”,函数(){
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+”)”);});
});