下拉按钮来过滤d3.js图 [英] Drop down button to filter a d3.js graph
问题描述
我有一个图表工作正常,甚至按钮点击数据过滤。现在我试图使用一个下拉按钮而不是常规的按钮。当用户从下拉菜单中选择金额为1000或2000时,节点和链接应根据属性
I have a graph that works fine and even filters the data on button click. Now I am trying to use a drop down button instead of a regular button. When a user chooses Amount as either 1000 or 2000 from the drop down , nodes and links should get filtered based on the property
d.total_amt> 1000 or d.total_amt > 2000
HTML
<style>
.links line {
stroke: #999;
stroke-opacity: 0.6;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}
div.tooltip {
position: absolute;
background-color: white;
max-width: 200px;
height: auto;
padding: 1px;
border-style: solid;
border-radius: 4px;
border-width: 1px;
box-shadow: 3px 3px 10px rgba(0, 0, 0, .5);
pointer-events: none;
}
</style>
<svg id="Network_graph" width="400" height="350"></svg>
<label for="input ID">Amount</label>
<select id="select_ID" name="select_ID">
<option value="1000">1000</option>
<option value="2000">2000</option>
</select>
<button id="full_data">
full data
</button>
JSON
var IDData = JSON.stringify([
["node/105173", "node/38180995", "Agent", "Customer", "1379644.0", 1, 264, "1374903"],
["node/1061", "node/21373542", "Agent", "Customer", "530848.0", 1, 3000, "529502"],
["node/10750", "node/59648369", "Agent", "Customer", "1454228.0", 1, 120, "1454118"],
["node/10750", "node/78569210", "Agent", "Customer", "1425251.0", 1, 234, "1421416"],
["node/10750", "node/96726118", "Agent", "Customer", "1376239.0", 1, 434, "1376152"],
["node/10946829", "node/11190", "Customer", "Agent", "1409620.0", 20, 3380, "1406665"],
["node/10946829", "node/57774036", "Customer", "Customer", "1460029.0", 3, 960, "1459731"],
["node/109947", "node/97911872", "Agent", "Customer", "1323025.0", 1, 600, "1315582"],..])
下面的代码将以适合渲染图形的格式使这些数据:
Below is the piece of code that will make this data in a format suitable to render the graph:
var galData = JSON.parse(IDData);
var startnodes = [];
var endnodes = [];
var startnodetype = [];
var endnodetype = [];
var PayTime = [];
var TXN_COUNT = [];
var Total_Amt = [];
var SendTime = [];
galData.map(function(e, i) {
startnodes.push(e[0]);
endnodes.push(e[1]);
startnodetype.push(e[2]);
endnodetype.push(e[3]);
PayTime.push(e[4]);
TXN_COUNT.push(e[5]);
Total_Amt.push(e[6]);
SendTime.push(e[7]);
});
var final_data = createNodes(startnodes, endnodes, startnodetype, endnodetype, PayTime, TXN_COUNT, Total_Amt, SendTime);
makeGraph("#Network_graph", final_data);
我已经使用这个数据强制有向图。 Jsfiddle 是工作图的链接。
I have made a force directed graph using this data. Jsfiddle is the link to the working graph.
现在我试图添加下拉菜单来进行数据过滤。这是从下拉列表中选择的代码段。
Now I am trying to add drop down for data filtering. This is the section of code that holds on to the selection from the drop down
d3.select(this) //right way to hold on to the selection or need some jquery?
.selectAll("option")
.filter(function(d, i) {
return this.selected;
});
这段代码使用过滤器函数过滤节点和链接。
This piece of code uses the filter function to filter the nodes and links.
function isUnique(id, nodes) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].id == id) {
return false;
}
}
return true;
}
var filtered_data = [];
var nodes = [];
var links = [];
d3.selectAll("line").filter(function(d, i) {
if (d.total_amt > this.value) {
if (isUnique(d.source.id, nodes)) {
nodes.push(d.source);
}
if (isUnique(d.target.id, nodes)) {
nodes.push(d.target);
}
links.push(d);
}
});
filtered_data.links = links;
filtered_data.nodes = nodes;
filtered_data.nodetype = final_data.nodetype;
d3.select('#Network_graph').selectAll("*").remove();
makeGraph("#Network_graph", filtered_data);
上述方法有意义吗?所以,这远远没有奏效。我不知道缺少的是什么。我也对其他想法开放,可能会更好地下降。仍然找到我的方式d3.js / javascript。期待一些帮助。
Does the above approach make sense? So , far this has not worked. I don't know what is the missing piece. I am open to other ideas too that may work better for a drop down. Still finding my way around d3.js /javascript. Looking forward for some help.
推荐答案
确定
你需要:
var elem = document.getElementById('select_ID');
elem.addEventListener("change", onSelectChange);
function onSelectChange(){
var value = this.value;
var fdata = filteredData(value);
d3.select('#Network_graph').selectAll("*").remove();
makeGraph("#Network_graph", fdata);
}
为了在选择选项更改时执行某些操作。选择元素并附加事件侦听器(在这种情况下更改)。
In order to do something when your select option changes. Select the element and attach an event listener (change in this case) to it.
var elem = document.getElementById('select_ID');
elem.addEventListener("change", onSelectChange);
在此更改事件内部获取值为 this.value
正如您已经说过的那样进行过滤。
And inside this change event get the value with this.value
as you've already said and make the filtering.
这是小提琴: https://jsfiddle.net/tgv6s5cd/14/
这篇关于下拉按钮来过滤d3.js图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!