Skip to content

Instantly share code, notes, and snippets.

@mapsense-examples
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapsense-examples/386443157dd5e0a278a1 to your computer and use it in GitHub Desktop.
Save mapsense-examples/386443157dd5e0a278a1 to your computer and use it in GitHub Desktop.
Display roads in paris from a topojson file using D3
<!DOCTYPE html>
<meta charset="utf-8">
<style>
#map {
width: 960px;
height: 500px;
}
.roads {
stroke: black;
fill: none;
}
.overlay {
fill: none;
pointer-events: all;
}
</style>
<body>
<div id="map"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
<script>
var width = 960,
height = 500;
var initialZoom = 295;
var initialPos = [-205.49548429098172, -14197.978760832331];
var path = d3.geo.path().projection(null);
var zoom = d3.behavior.zoom()
.translate(initialPos)
.scale(initialZoom)
.on("zoom", zoomed);
var svg = d3.select("#map").append("svg").attr("height", height).attr("width", width);
var features = svg.append("g").attr("transform","translate("+ initialPos[0] + "," + initialPos[1] + ")scale(" + initialZoom + ")");
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.call(zoom);
d3.json("paris.topojson", function(error, data) {
features.append("path")
.datum(topojson.feature(data, data.objects.roads))
.attr("class", "roads")
.attr("d", path)
.style("stroke-width", .5/initialZoom + "px");
});
function zoomed() {
console.log(d3.event.scale);
console.log(d3.event.translate);
features.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
features.select(".roads").style("stroke-width", .5 / d3.event.scale + "px");
}
d3.select(self.frameElement).style("height", height + "px");
</script>
Display the source blob
Display the rendered blob
Raw
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