D3.js 双击节点以淡出除其近邻以外的所有节点。双击以再次将它们带回来
我使用下面的代码来显示图像和鼠标悬停文本。现在我已经添加了双击一个节点以淡出所有的代码,但它不工作,链接颜色也没有改变。有人能帮忙吗D3.js 双击节点以淡出除其近邻以外的所有节点。双击以再次将它们带回来,d3.js,D3.js,我使用下面的代码来显示图像和鼠标悬停文本。现在我已经添加了双击一个节点以淡出所有的代码,但它不工作,链接颜色也没有改变。有人能帮忙吗 var width = 960, height = 500 var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); var force = d3.layout.force
var width = 960,
height = 500
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(0.1)
.charge(-120)
.linkDistance(30)
.size([width, height]);
var voronoi = d3.geom.voronoi()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.clipExtent([[0, 0], [width, height]]);
d3.json("miserables1.json", function(error, json) {
if (error) throw error;
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link", "fill:red; stroke:red;");
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("svg:image")
.attr("xlink:href", function(d) { return d.imagen })
.on('dblclick', connectedNodes);
var circle = node.append("circle")
.attr("r", 4.5);
var label = node.append("text")
.attr("dy", ".35em")
.text(function(d) { return d.name; });
var cell = node.append("path")
.attr("class", "cell");
force.on("tick", function() {
cell
.data(voronoi(json.nodes))
.attr("d", function(d) { return d.length ? "M" + d.join("L") : null; });
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; });
circle
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
label
.attr("x", function(d) { return d.x + 8; })
.attr("y", function(d) { return d.y; });
});
});
var toggle = 0;
var linkedByIndex = {};
for (i = 0; i < graph.nodes.length; i++) {
linkedByIndex[i + "," + i] = 1;
};
graph.links.forEach(function (d) {
linkedByIndex[d.source.index + "," + d.target.index] = 1;
});
function neighboring(a, b) {
return linkedByIndex[a.index + "," + b.index];
}
function connectedNodes() {
if (toggle == 0) {
//Reduce the opacity of all but the neighbouring nodes
d = d3.select(this).node().__data__;
node.style("opacity", function (o) {
return neighboring(d, o) | neighboring(o, d) ? 1 : 0.1;
});
link.style("opacity", function (o) {
return d.index==o.source.index | d.index==o.target.index ? 1 : 0.1;
});
toggle = 1;
} else {
node.style("opacity", 1);
link.style("opacity", 1);
toggle = 0;
}
}
var-width=960,
高度=500
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度)
.attr(“高度”,高度);
var-force=d3.layout.force()
.重力(0.1)
。收费(-120)
.linkDistance(30)
.尺寸([宽度、高度]);
var voronoi=d3.geom.voronoi()
.x(函数(d){返回d.x;})
.y(函数(d){返回d.y;})
.clipExtent([[0,0],[width,height]]);
json(“miserables1.json”),函数(错误,json){
如果(错误)抛出错误;
力
.nodes(json.nodes)
.links(json.links)
.start();
var link=svg.selectAll(“.link”)
.data(json.links)
.enter().append(“行”)
.attr(“类”、“链接”、“填充:红色;笔划:红色;”);
var node=svg.selectAll(“.node”)
.data(json.nodes)
.enter().append(“g”)
.attr(“类”、“节点”)
.呼叫(强制拖动);
node.append(“svg:image”)
.attr(“xlink:href”,函数(d){return d.imagen})
.on('dblclick',connectedNodes);
var circle=node.append(“圆”)
.attr(“r”,4.5);
var label=node.append(“文本”)
.attr(“dy”,“.35em”)
.text(函数(d){返回d.name;});
var cell=node.append(“路径”)
.attr(“类”、“单元”);
force.on(“勾号”,函数(){
细胞
.data(voronoi(json.nodes))
.attr(“d”,函数(d){返回d.length?“M”+d.join(“L”):null;});
链接
.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(“cx”,函数(d){return d.x;})
.attr(“cy”,函数(d){返回d.y;});
标签
.attr(“x”,函数(d){返回d.x+8;})
.attr(“y”,函数(d){返回d.y;});
});
});
var=0;
var linkedByIndex={};
对于(i=0;i
请详细说明它不起作用
:需要什么行为,您观察到了什么?当我双击节点时,只应显示链接的节点。剩下的需要消失。再次双击时,它们必须再次出现在屏幕上。当[I]双击[…]链接节点时,任何人都可以在此上帮助我。剩余的[…]消失
-看起来像是必需的行为:请编辑到您的问题中-不要忘记也描述观察到的行为。(只需尝试,无需寻求答案/帮助。)这是关于淡出未连接到双击节点的节点和链接。观察到的行为是当我双击节点时,行为没有改变。