Skip to content

Instantly share code, notes, and snippets.

@pere
Created June 28, 2012 17:10
Geographic d3.js bubbles
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>France bubbling</title>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.7.4"></script>
<style type="text/css">
svg {
width: 900px;
height: 500px;
border: solid 1px #ccc;
background: #eee;
}
line {
stroke: brown;
stroke-dasharray: 4,2;
}
path {
fill: #ccc;
stroke: #fff;
}
div {
width: 900px;
}
</style>
</head>
<body>
<h3>Mercator Projection</h3>
<script type="text/javascript">
width= 900;
height= 500;
//focusing france on SVG
xy=d3.geo.mercator().translate([453,1872]).scale(11000)
path=d3.geo.path().projection(xy);
var svg = d3.select("body")
.append("svg")
.append("g")
.attr("id", "polygons");
d3.json('regions.json', function(collection) {
//console.warn(collection.features)
d3.select("#polygons")
.selectAll("path")
.data(collection.features)
.enter().append("path")
.attr("d", d3.geo.path().projection(xy));
});
d3.json('datapublishers.json', function(data) {
var force = d3.layout.force()
// .gravity(0)
.charge(-0.7)
.nodes(data.features)
.size([0, 0])
.start();
var to_bubble=[]
//this function could be used to filter circles by city parameter
filter_by_city =function (city)
{
data.features.forEach(function(data) {
if (data.properties.city==city)
{
to_bubble.push(data)
}
})
return to_bubble;
}
var node = d3.select("svg").selectAll("g")
.data(data.features) //filter_by_city('Paris') could be used here, assigning later a more negative charge but its not the ideal solution at all
.enter().append("g")
.attr("id", function(d){ return d.properties.city;})
node.append("circle")
.attr("transform", function(d) {
return "translate(" + xy(d.geometry.coordinates) + ")"; })
.attr("id", function(d){ return d.id;})
.attr("class", "node")
.attr('fill','blue')
.attr('opacity',0.5)
.attr('r', function(d) {
return d.properties.num_collections*5})
.call(force.drag);
force.on("tick", function() {
node.attr(
"transform",
function(d) { return "translate(" + d.x + "," + d.y + ")"; }
);
});
});
</script>
</body>
</html>
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment