- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个力导向图,其中有(大量)节点和它们之间的许多链接。我想以交互方式应用过滤器,以便仅保留一部分节点(以及它们之间的任何链接)。但是,由于图形很大,我想从模拟中删除任何被过滤掉的节点,而不是仅仅隐藏它们(这样生成的图形具有更好的性能)。因此,我希望创建一个过滤后的节点列表作为一个新数组,并仅使用这些节点重新初始化模拟。同样适用于边缘——我还没有这样做,但我可能需要以编程方式确定要保留哪些边缘,并在使用新节点 和 边缘重新初始化图形之前进行相同类型的过滤.我想保留原始节点/边缘数组以允许“重置”回到起始状态。
我已经整理了一个简单的例子,此时它只是在做一些硬编码过滤,但我正在努力使用过滤后的数组重新初始化模拟。它似乎正在从模拟中删除过滤后的节点(圆圈“三”变得不再可拖动)但它仍然显示在渲染图中。
不过,到目前为止,我对“重置”逻辑的尝试似乎确实奏效了。
我做错了什么?有没有更好的方法来实现这一目标? (d3.js v3)
我的示例代码如下:
var links = [{
source: 0,
target: 1,
type: "c"
},
{
source: 1,
target: 2,
type: "d"
},
{
source: 2,
target: 0,
type: "d"
}
];
var nodes = [{
name: "one",
type: "a"
},
{
name: "two",
type: "a"
},
{
name: "three",
type: "b"
}
];
var width = 300;
var height = 300;
var force = d3.layout.force()
.nodes(nodes)
.links(links)
.size([width, height])
.linkDistance(200)
.charge(-400)
.on("tick", tick)
.start();
var svg = d3.select("#graph").append("svg")
.attr("width", width)
.attr("height", height);
function colours(n) {
var colours = ["#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477", "#66aa00", "#b82e2e", "#316395",
"#994499", "#22aa99", "#aaaa11", "#6633cc", "#e67300", "#8b0707", "#651067", "#329262", "#5574a6", "#3b3eac"
];
return colours[n % colours.length];
}
var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("line")
.attr('class', 'link')
.attr('stroke', function(d, i) {
return colours(i);
})
var circles = svg.append("g");
var circle = circles.selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.attr("r", 8)
.attr('class', 'circle')
.attr('fill', function(d, i) {
return colours(i + 3);
})
.call(force.drag);
var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter().append("text")
.attr("x", 14)
.attr("y", ".31em")
.text(function(d) {
return d.name;
});
function tick() {
path.attr({
x1: function(d) {
return d.source.x;
},
y1: function(d) {
return d.source.y;
},
x2: function(d) {
return d.target.x;
},
y2: function(d) {
return d.target.y;
}
});
circle.attr("transform", transform);
text.attr("transform", transform);
}
function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}
var nodeText = "";
function nodeTypeA(node) {
return (node.type == "a");
}
function linkTypeC(link) {
return (link.type == "c");
}
function applyFilter() {
force.nodes(nodes.filter(nodeTypeA));
force.links(links.filter(linkTypeC));
circle.data(force.nodes());
text.data(force.nodes());
path.data(force.links());
d3.selectAll("circle").each(
function(d) {
console.log(d.name);
}
);
console.log("");
}
function resetFilter() {
force.nodes(nodes);
force.links(links);
circle.data(force.nodes());
text.data(force.nodes());
path.data(force.links());
d3.selectAll("circle").each(
function(d) {
console.log(d.name);
}
)
console.log("");
}
#buttons {
position: absolute;
top: 10px;
left: 20px;
height: 100px;
width: 400px;
z-index: 99;
}
#graph {
position: absolute;
top: 50px;
left: 20px;
height: 300px;
width: 300px;
z-index: 98;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<div id="root">
<div id="buttons">
<button id="filter" onclick="applyFilter()">Apply</button>
<button id="reset" onclick="resetFilter()">Reset</button>
</div>
<div id="graph">
</div>
</div>
</body>
<div id="node_details">
</div>
</body>
最佳答案
对于您的过滤器和重置功能,您更新选择的数据但不使用进入或退出选择来添加/删除新元素。
您在最初附加元素时确实使用了输入选择,例如:
var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("line")
...
但是更新时你只需使用:
path.data(force.links());
您仍然需要指定要添加的内容以及添加方式,就像最初添加节点时所做的那样。
要退出节点非常简单,我们只需在应用过滤器时将 .exit().remove()
添加到上面的行。 .exit()
选择选择中不再具有数据数组中对应项的元素。 .remove()
只是将它们从 DOM 中移除:
var links = [
{source: 0, target: 1, type: "c"},
{source: 1, target: 2, type: "d"},
{source: 2, target: 0, type: "d"}
];
var nodes = [
{name: "one", type: "a"},
{name: "two", type: "a"},
{name: "three", type: "b"}
];
var width = 300;
var height = 300;
var force = d3.layout.force()
.nodes(nodes)
.links(links)
.size([width, height])
.linkDistance(200)
.charge(-400)
.on("tick", tick)
.start();
var svg = d3.select("#graph").append("svg")
.attr("width", width)
.attr("height", height);
function colours(n) {
var colours = ["#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477", "#66aa00", "#b82e2e", "#316395",
"#994499", "#22aa99", "#aaaa11", "#6633cc", "#e67300", "#8b0707", "#651067", "#329262", "#5574a6", "#3b3eac"];
return colours[n % colours.length];
}
var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("line")
.attr('class', 'link')
.attr('stroke', function(d, i) {
return colours(i);
})
var circles = svg.append("g");
var circle = circles.selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.attr("r", 8)
.attr('class', 'circle')
.attr('fill', function(d, i) {
return colours(i + 3);
})
.call(force.drag);
var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter().append("text")
.attr("x", 14)
.attr("y", ".31em")
.text(function(d) {
return d.name;
});
function tick() {
path.attr({
x1: function(d) {
return d.source.x;
},
y1: function(d) {
return d.source.y;
},
x2: function(d) {
return d.target.x;
},
y2: function(d) {
return d.target.y;
}
});
circle.attr("transform", transform);
text.attr("transform", transform);
}
function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}
var nodeText = "";
function nodeTypeA(node) {
return (node.type == "a");
}
function linkTypeC(link) {
return (link.type == "c");
}
function applyFilter() {
force.nodes(nodes.filter(nodeTypeA));
force.links(links.filter(linkTypeC));
circle.data(force.nodes()).exit().remove();
text.data(force.nodes()).exit().remove();
path.data(force.links()).exit().remove();
d3.selectAll("circle").each(
function(d) {
console.log(d.name);
}
);
console.log("");
}
function resetFilter() {
force.nodes(nodes);
force.links(links);
circle.data(force.nodes());
text.data(force.nodes());
path.data(force.links());
d3.selectAll("circle").each(
function(d) {
console.log(d.name);
}
)
console.log("");
}
#buttons {
position: absolute;
top: 10px;
left: 20px;
height: 100px;
width: 400px;
z-index: 99;
}
#graph {
position: absolute;
top: 50px;
left: 20px;
height: 300px;
width: 300px;
z-index: 98;
}
<div id="root">
<div id="buttons">
<button id="filter" onclick="applyFilter()">Apply</button>
<button id="reset" onclick="resetFilter()">Reset</button>
</div>
<div id="graph">
</div>
</div>
</body>
<script src="https://d3js.org/d3.v3.min.js"></script>
我们可以复制您在重置函数中用于初始输入以输入元素的代码(以及一些小的修改),但这有点重复 - 我们将有两部分代码执行相同的操作。
相反,让我们将进入和退出放入一个更新函数中。更新函数将从力布局中获取节点和链接,并根据需要进入/退出:
var links = [
{source: 0, target: 1, type: "c"},
{source: 1, target: 2, type: "d"},
{source: 2, target: 0, type: "d"}
];
var nodes = [
{name: "one", type: "a"},
{name: "two", type: "a"},
{name: "three", type: "b"}
];
var width = 300;
var height = 300;
var force = d3.layout.force()
.nodes(nodes)
.links(links)
.size([width, height])
.linkDistance(200)
.charge(-400)
.on("tick", tick)
.start();
var svg = d3.select("#graph").append("svg")
.attr("width", width)
.attr("height", height);
function colours(n) {
var colours = ["#3366cc", "#dc3912", "#ff9900", "#109618", "#990099", "#0099c6", "#dd4477", "#66aa00", "#b82e2e", "#316395",
"#994499", "#22aa99", "#aaaa11", "#6633cc", "#e67300", "#8b0707", "#651067", "#329262", "#5574a6", "#3b3eac"];
return colours[n % colours.length];
}
var paths = svg.append("g");
var circles = svg.append("g");
var texts = svg.append("g");
update();
function tick() {
paths.selectAll("line").attr({
x1: function(d) {
return d.source.x;
},
y1: function(d) {
return d.source.y;
},
x2: function(d) {
return d.target.x;
},
y2: function(d) {
return d.target.y;
}
});
circles.selectAll("circle").attr("transform", transform);
texts.selectAll("text").attr("transform", transform);
}
function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}
var nodeText = "";
function nodeTypeA(node) {
return (node.type == "a");
}
function linkTypeC(link) {
return (link.type == "c");
}
function applyFilter() {
force.nodes(nodes.filter(nodeTypeA));
force.links(links.filter(linkTypeC));
update();
}
function resetFilter() {
force.nodes(nodes);
force.links(links);
update();
force.start(); // start the force layout again.
}
function update() {
// update the data for the lines:
var path = paths.selectAll("line")
.data(force.links());
// enter new lines:
path.enter().append("line")
.attr('class', 'link')
.attr('stroke', function(d, i) {
return colours(i);
})
// exit unneeded lines:
path.exit().transition().style("opacity",0).remove();
// update the data for the circles:
var circle = circles.selectAll("circle")
.data(force.nodes());
// enter new circles:
circle.enter().append("circle")
.attr("r", 8)
.attr('class', 'circle')
.attr('fill', function(d, i) {
return colours(i + 3);
})
.call(force.drag);
// remove unneeded circles:
circle.exit().transition().style("opacity",0).remove();
// update the text data:
var text = texts.selectAll("text")
.data(force.nodes());
// enter new text
text.enter().append("text")
.attr("x", 14)
.attr("y", ".31em")
.text(function(d) {
return d.name;
});
// exit old text:
text.exit().transition().style("opacity",0).remove();
}
#buttons {
position: absolute;
top: 10px;
left: 20px;
height: 100px;
width: 400px;
z-index: 99;
}
#graph {
position: absolute;
top: 50px;
left: 20px;
height: 300px;
width: 300px;
z-index: 98;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<div id="root">
<div id="buttons">
<button id="filter" onclick="applyFilter()">Apply</button>
<button id="reset" onclick="resetFilter()">Reset</button>
</div>
<div id="graph">
</div>
</div>
</body>
<div id="node_details">
</div>
与原来相比的变化:过滤器和重置函数在设置力的节点和链接后调用更新函数(并且节点最初是用更新函数绘制的)。添加节点时会重置力(重新激活模拟,就好像它已经冷却一样,不会调用刻度,节点也不会正确定位)。
最后,文本、圆圈和线条在名为 texts
circles
和 lines< 的父
分别。 tick 函数已被修改为在每次 tick 时重新选择每个父 g
选择中g
的子级 - 尽管您可以以不同的方式优化它。
作为最后一点,可能值得查看为数据指定标识符或在数据中指定节点/链接属性 - 当删除/添加链接/节点着色和/或按索引设置属性时可能会出现问题。
关于javascript - "Hard"d3,js力向图中的节点过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51200038/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!