我创建了一个函数,当我单击一个节点时,它会使所有非相邻节点透明*。现在我想让相同的节点对鼠标事件不响应,同时保持可见节点响应。
一种选择是将 css 属性 pointer-events:none
分配给透明节点。我可以用 sigma 做到这一点吗?
*为此,我指定了不透明度为 0 的 rgba 颜色。因此我必须使用 Canvas 渲染器,因为 WebGL 不支持透明度。
我的代码:
function highlight () {
var s = sigma.instances()[0];
var nodes = s.graph.nodes();
var edges = s.graph.edges();
var maxCollab = d3.max(edges, function(d) { return d.collaborations; });
// We first need to save the original colors of our
// nodes and edges, like this:
nodes.forEach(function(n) {
n.originalColor = n.color;
});
edges.forEach(function(e) {
e.originalColor = e.color;
});
// When a node is clicked, we check for each node
// if it is a neighbor of the clicked one. If not,
// we set its color as grey, and else, it takes its
// original color.
// We do the same for the edges, and we only keep
// edges that have both extremities colored.
s.bind('clickNode', function(e) {
var nodeId = e.data.node.id,
toKeep = s.graph.neighbors(nodeId);
toKeep[nodeId] = e.data.node;
nodes.forEach(function(n) {
if (toKeep[n.id])
n.color = n.originalColor;
else
n.color = 'rgba(0,0,0,0)';
});
edges.forEach(function(e) {
if (toKeep[e.source] && toKeep[e.target]) {
e.color = e.originalColor;
}
else
e.color = 'rgba(0,0,0,0)';
});
// Since the data has been modified, we need to
// call the refresh method to make the colors
// update effective.
s.refresh();
});
// When the stage is clicked, we just color each
// node and edge with its original color.
s.bind('clickStage', function(e) {
nodes.forEach(function(n) {
n.color = n.originalColor;
});
edges.forEach(function(e) {
e.color = e.originalColor;
});
s.refresh();
});
}
最佳答案
您只想隐藏节点吗?如果是这样,您可以将节点的隐藏属性设置为 true。这样它们就不再可见,并且 sigma 不会为它们触发任何事件。
关于javascript - 如何将 CSS 属性添加到 sigma.js( Canvas 渲染器)上的选定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39577636/