forked from emeeks's block: Ch. 7 Fig. 12 - D3.js in Action
xxxxxxxxxx
<html>
<head>
<title>D3 in Action Chapter 7 - Example 5</title>
<meta charset="utf-8" />
<script src="https://d3js.org/d3.v3.min.js" type="text/JavaScript"></script>
<script src="https://d3js.org/d3.geo.projection.v0.min.js" type="text/JavaScript"></script>
<script src="https://d3js.org/queue.v1.min.js" type="text/JavaScript"></script>
<script src="https://d3js.org/colorbrewer.v1.min.js" type="text/JavaScript"></script>
</head>
<style>
svg {
border: 1px solid gray;
}
.countries {
fill: red;
fill-opacity: .5;
stroke: black;
stroke-width: 1px;
}
</style>
<body>
<div id="viz">
</div>
<div id="controls" />
</body>
<footer>
<script>
var width = 800;
var height = 500;
d3.select("#viz").append("svg").attr("width", width).attr("height", height);
queue()
.defer(d3.json, "world.geojson")
.defer(d3.csv, "cities.csv")
.await(function(error, file1, file2) { createMap(file1, file2); });
function createMap(countries, cities) {
expData = countries;
projection = d3.geo.orthographic()
.scale((Math.min(width,height) / 2))
.translate([width / 2, height / 2])
.clipAngle(90);
geoPath = d3.geo.path().projection(projection);
var mapZoom = d3.behavior.zoom().translate(projection.translate()).scale(projection.scale()).on("zoom", zoomed);
d3.select("svg").call(mapZoom);
var rotateScale = d3.scale.linear()
.domain([0, width])
.range([0, 180]);
var currentRotation = projection.rotate();
d3.select("svg").on("mousedown", startRotating).on("mouseup", stopRotating);
function startRotating() {
var pStart = d3.mouse(this);
var currentLat = (currentRotation[1] + 360) % 360;
var xFactor = (currentLat > 90 && currentLat < 270) ? -1.0 : 1.0;
d3.select("svg").on("mousemove", function() {
var p = d3.mouse(this);
projection.rotate([
currentRotation[0] + xFactor * (rotateScale(p[0] - pStart[0])),
currentRotation[1] + rotateScale(pStart[1] - p[1])]);
zoomed();
});
}
function stopRotating() {
currentRotation = projection.rotate();
console.log(currentRotation);
d3.select("svg").on("mousemove", null);
}
function zoomed() {
var currentRotate = projection.rotate()[0];
projection.scale(mapZoom.scale());
d3.selectAll("path.graticule").attr("d", geoPath);
d3.selectAll("path.countries").attr("d", geoPath);
}
function zoomButton(zoomDirection) {
if (zoomDirection == "in") {
var newZoom = mapZoom.scale() * 1.5;
var newX = ((mapZoom.translate()[0] - (width / 2)) * 1.5) + width / 2;
var newY = ((mapZoom.translate()[1] - (height / 2)) * 1.5) + height / 2;
}
else if (zoomDirection == "out") {
var newZoom = mapZoom.scale() * .75;
var newX = ((mapZoom.translate()[0] - (width / 2)) * .75) + width / 2;
var newY = ((mapZoom.translate()[1] - (height / 2)) * .75) + height / 2;
}
mapZoom.scale(newZoom).translate([newX,newY])
zoomed();
}
}
</script>
</footer>
</html>
Modified http://d3js.org/d3.v3.min.js to a secure url
Modified http://d3js.org/d3.geo.projection.v0.min.js to a secure url
Modified http://d3js.org/queue.v1.min.js to a secure url
Modified http://d3js.org/colorbrewer.v1.min.js to a secure url
https://d3js.org/d3.v3.min.js
https://d3js.org/d3.geo.projection.v0.min.js
https://d3js.org/queue.v1.min.js
https://d3js.org/colorbrewer.v1.min.js