The force directed map allows users to rearrange the states by mouse click and drag. The VisDock toolkit has been integrated into the force directed map example built with D3.js (found here) by M. Bostock. Using selection tools, users can query path elements even in a force-layout example.
Last active
August 29, 2015 13:56
-
-
Save VisDockHub/8972904 to your computer and use it in GitHub Desktop.
The Force Directed Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <style> | |
| path { | |
| fill: #ddd; | |
| fill-opacity: .8; | |
| stroke: #fff; | |
| stroke-width: 1.5px; | |
| } | |
| line { | |
| stroke: #999; | |
| } | |
| </style> | |
| <body> | |
| <link href="http://rawgithub.com/VisDockHub/NewVisDock/master/master/visdock.css" rel="stylesheet" type="text/css"/> | |
| <script src="http://d3js.org/d3.v3.min.js"></script> | |
| <script src="http://d3js.org/topojson.v1.min.js"></script> | |
| <script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/visdock.js"></script> | |
| <script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/2D.js"></script> | |
| <script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/IntersectionUtilities.js"></script> | |
| <script src="http://rawgithub.com/VisDockHub/NewVisDock/master/master/visdock.utils.js"></script> | |
| <script> | |
| var width = 960, | |
| height = 500; | |
| var click = 0; | |
| VisDock.init("body", {width: 960, height: 700}); | |
| var viewport = VisDock.getViewport(); | |
| var path = d3.geo.path(), | |
| force = d3.layout.force().size([width, height]); | |
| var svg = viewport; | |
| d3.json("/VisDockHub/raw/8971488/us.json", function(error, us) { | |
| var states = topojson.feature(us, us.objects.states), | |
| nodes = [], | |
| links = []; | |
| states.features.forEach(function(d, i) { | |
| if (d.id === 2 || d.id === 15 || d.id === 72) return; // lower 48 | |
| var centroid = path.centroid(d); | |
| if (centroid.some(isNaN)) return; | |
| centroid.x = centroid[0]; | |
| centroid.y = centroid[1]; | |
| centroid.feature = d; | |
| nodes.push(centroid); | |
| }); | |
| d3.geom.voronoi().links(nodes).forEach(function(link) { | |
| var dx = link.source.x - link.target.x, | |
| dy = link.source.y - link.target.y; | |
| link.distance = Math.sqrt(dx * dx + dy * dy); | |
| links.push(link); | |
| }); | |
| force | |
| .gravity(0) | |
| .nodes(nodes) | |
| .links(links) | |
| .linkDistance(function(d) { return d.distance; }) | |
| .start(); | |
| var link = svg.selectAll("line") | |
| .data(links) | |
| .enter().append("line") | |
| .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; }); | |
| var node = svg.selectAll("g") | |
| .data(nodes) | |
| .enter().append("g") | |
| .attr("class", "states") | |
| .attr("transform", function(d) { return "translate(" + -d.x + "," + -d.y + ")"; }) | |
| .call(force.drag) | |
| .append("path") | |
| .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) | |
| .attr("d", function(d) { return path(d.feature); }) | |
| force.on("tick", function(e) { | |
| VisDock.startChrome(); | |
| 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; }); | |
| for (var u = 0; u < num; u++){ | |
| for (var v = 0; v < QueryManager.layers[u].length; v++){ | |
| var str = QueryManager.layers[u][v][0][0].parentNode.childNodes[0].getAttributeNS(null, "transform") | |
| QueryManager.layers[u][v][0][0].setAttributeNS(null, "transform", str) | |
| } | |
| } | |
| node.attr("transform", function(d) { | |
| return "translate(" + d.x + "," + d.y + ")"; | |
| }); | |
| VisDock.finishChrome(); | |
| }); | |
| }); | |
| VisDock.eventHandler = { | |
| getHitsPolygon : function(points, inclusive) { | |
| var shapebound = new createPolygon(points); | |
| return shapebound.intersectPath(d3.selectAll("path")[0], inclusive) | |
| }, | |
| getHitsLine : function(points, inclusive) { | |
| var shapebound = new createLine(points); | |
| return shapebound.intersectPath(d3.selectAll("path")[0], inclusive) | |
| }, | |
| getHitsEllipse : function(points, inclusive) { | |
| var shapebound = new createEllipse(points); | |
| return shapebound.intersectPath(d3.selectAll("path")[0], inclusive) | |
| }, | |
| setColor : function(hits) { | |
| QueryManager.layers[num - 1] = []; | |
| for (var i = 0; i < hits.length; i++) { | |
| var str = hits[i].getAttributeNS(null, "transform"); | |
| var d = hits[i].getAttributeNS(null, "d") | |
| QueryManager.layers[num - 1][i] = d3.select(hits[i].parentNode).append("path").attr("transform", str) | |
| .attr("d", d) | |
| .attr("style", "fill: " + VisDock.color[num - 1] + "; opacity: 0.5; pointer-events: none") | |
| } | |
| }, | |
| changeColor : function(color, query, index) { | |
| var vis = VisDock.utils.getQueryVisibility(index); | |
| for (var i = 0; i < query.length; i++) { | |
| query[i][0][0].setAttributeNS(null, "style", "fill: " + color + "; opacity: " + vis) | |
| } | |
| }, | |
| changeVisibility : function(vis, query, index) { | |
| var color = VisDock.utils.getQueryColor(index); | |
| for (var i = 0; i < query.length; i++) { | |
| query[i][0][0].setAttributeNS(null, "style", "fill: " + color + "; opacity: " + vis) | |
| } | |
| }, | |
| removeColor : function(hits, index) { | |
| for (var i = 0; i < hits.length; i++) { | |
| hits[i].remove(); | |
| } | |
| }, | |
| QueryClick : function(query, index) { | |
| } | |
| } | |
| BirdView.init(viewport, 960, 700) | |
| d3.select(self.frameElement).style("width", "960px") | |
| d3.select(self.frameElement).style("height", "700px") | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment