Skip to content

Instantly share code, notes, and snippets.

@emeeks
Last active March 17, 2016 02:27
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 emeeks/386314fe16470c373a4a to your computer and use it in GitHub Desktop.
Save emeeks/386314fe16470c373a4a to your computer and use it in GitHub Desktop.
Ch. 11, Fig. 4 - D3.js in Action

This is the code for Chapter 11, Figure 4 from D3.js in Action showing how to draw geodata using d3.geo.path().context().

<html>
<head>
<title>D3 in Action Chapter 11 - Example 2</title>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
</head>
<style>
body, html {
margin: 0;
}
canvas {
position: absolute;
width: 500px;
height: 500px;
}
svg {
position: absolute;
width:500px;
height:500px;
}
path.country {
fill: gray;
stroke-width: 1;
stroke: black;
opacity: .5;
}
path.sample {
stroke: black;
stroke-width: 1px;
fill: red;
fill-opacity: .5;
}
line.link {
stroke-width: 1px;
stroke: black;
stroke-opacity: .5;
}
circle.node {
fill: red;
stroke: white;
stroke-width: 1px;
}
circle.xy {
fill: pink;
stroke: black;
stroke-width: 1px;
}
</style>
<body>
<canvas height="500" width="500"></canvas>
<div id="viz">
<svg></svg>
</div>
</body>
<footer>
<script>
sampleData = d3.range(2000).map(function(d) {
var datapoint = {};
datapoint.id = "Sample Feature " + d;
datapoint.type = "Feature";
datapoint.properties = {};
datapoint.geometry = {};
datapoint.geometry.type = "Polygon";
datapoint.geometry.coordinates = randomCoords();
return datapoint;
})
d3.json("world.geojson", function(data) {createMap(data)});
function createMap(countries) {
projection = d3.geo.mercator().scale(100).translate([250,250]);
geoPath = d3.geo.path().projection(projection);
mapZoom = d3.behavior.zoom().translate(projection.translate()).scale(projection.scale()).on("zoom", zoomed);
d3.select("svg").call(mapZoom);
zoomed();
function zoomed() {
projection.translate(mapZoom.translate()).scale(mapZoom.scale());
var context = d3.select("canvas").node().getContext("2d");
context.clearRect(0,0,500,500);
context.strokeStyle = "black";
context.fillStyle = "gray";
context.lineWidth = "1px";
for (var x in countries.features) {
context.beginPath();
geoPath.context(context)(countries.features[x]);
context.stroke()
context.fill();
}
context.strokeStyle = "black";
context.fillStyle = "rgba(255,0,0,.2)";
context.lineWidth = 1;
for (var x in sampleData) {
context.beginPath();
geoPath.context(context)(sampleData[x]);
context.stroke()
context.fill();
}
}
}
function randomCoords() {
var randX = (Math.random() * 350) - 175;
var randY = (Math.random() * 170) - 85;
return [[[randX - 5,randY],[randX,randY - 5],[randX - 10,randY - 5],[randX - 5,randY]]];
}
</script>
</footer>
</html>
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