D3 v4 forcelayout example with dropdown filters.
forked from mbostock's block: Force Dragging I
xxxxxxxxxx
<meta charset="utf-8">
<style>
.links line {
stroke: #aaa;
}
.nodes circle {
pointer-events: all;
stroke: none;
stroke-width: 40px;
}
#parent_div{
width: 400px;
height: 100px;
margin-right: 10px;
float: left;
}
.child_div_1, .child_div_2 {
float: left;
margin-right: 5px;
}
</style>
<body>
Select Category:
<div id = 'catdropdown'></div>
</body>
<svg width="960" height="400"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
var color = d3.scaleOrdinal(d3.schemeCategory10);
d3.json("miserables.json", function(error, graph) {
if (error) throw error;
var currNodes = graph.nodes
var currLinks = graph.links
var nodesByGroup = d3.nest()
.key(function(d) { return d.group; })
.entries(graph.nodes);
// Create a dropdown
var catMenu = d3.select("#catdropdown")
catMenu
.append("select")
.selectAll("option")
.data(nodesByGroup)
.enter()
.append("option")
.attr("value", function(d){
return d.key;
})
.text(function(d){
return d.key;
})
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
//.data(graph.links)
.data(currLinks)
.enter().append("line");
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
//.data(graph.nodes)
.data(currNodes)
.enter().append("circle")
.attr("r", 2.5)
.attr("fill", function(d) { return color(d.group);})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("title")
.text(function(d) { return d.id; });
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) {return d.id;})
simulation
//.nodes(graph.nodes)
.nodes(currNodes)
.on("tick", ticked);
simulation.force("link")
.links(graph.links);
function ticked() {
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; });
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
// Run update function when dropdown selection changes
catMenu.on('change', function(){
// Find which fruit was selected from the dropdown
var selectedGroup = d3.select(this)
.select("select")
.property("value");
// Run update function with the selected fruit
currNodes = filterNodes(selectedGroup);
});
function filterNodes(group) {
//console.log(nodesByGroup);
var filteredNodes = nodesByGroup.filter(function(d){
return d.key == group;
});
console.log(filteredNodes);
console.log(filteredNodes.values);
return filteredNodes.values;
//alert("Submit Button Clicked!");
//console.log(document.getElementById('Group'))
//var order = document.getElementById('group').value;
//alert(order)
}
});
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
</script>
https://d3js.org/d3.v4.min.js