|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <link type="text/css" rel="stylesheet" href="style.css"/> |
| 5 | + <style type="text/css"> |
| 6 | + |
| 7 | +#states path { |
| 8 | + fill: #ccc; |
| 9 | + stroke: #fff; |
| 10 | +} |
| 11 | + |
| 12 | +path.arc { |
| 13 | + pointer-events: none; |
| 14 | + fill: none; |
| 15 | + stroke: #000; |
| 16 | + display: none; |
| 17 | +} |
| 18 | + |
| 19 | +path.cell { |
| 20 | + fill: none; |
| 21 | + pointer-events: all; |
| 22 | +} |
| 23 | + |
| 24 | +circle { |
| 25 | + fill: steelblue; |
| 26 | + fill-opacity: .8; |
| 27 | + stroke: #fff; |
| 28 | +} |
| 29 | + |
| 30 | +#cells.voronoi path.cell { |
| 31 | + stroke: brown; |
| 32 | +} |
| 33 | + |
| 34 | +#cells g:hover path.arc { |
| 35 | + display: inherit; |
| 36 | +} |
| 37 | + |
| 38 | + </style> |
| 39 | + </head> |
| 40 | + <body> |
| 41 | + <h2> |
| 42 | + <span>U.S. commercial airports</span>, 2008<br> |
| 43 | + great arcs and symbol map |
| 44 | + </h2> |
| 45 | + <div style="position:absolute;bottom:0;font-size:18px;"> |
| 46 | + <input type="checkbox" id="voronoi"> <label for="voronoi">show Voronoi</label> |
| 47 | + </div> |
| 48 | + <script type="text/javascript" src="d3/d3.js"></script> |
| 49 | + <script type="text/javascript" src="d3/d3.csv.js"></script> |
| 50 | + <script type="text/javascript" src="d3/d3.geo.js"></script> |
| 51 | + <script type="text/javascript" src="d3/d3.geom.js"></script> |
| 52 | + <script type="text/javascript"> |
| 53 | + |
| 54 | +var w = 1280, |
| 55 | + h = 800; |
| 56 | + |
| 57 | +var projection = d3.geo.azimuthal() |
| 58 | + .mode("equidistant") |
| 59 | + .origin([-98, 38]) |
| 60 | + .scale(1400) |
| 61 | + .translate([640, 360]); |
| 62 | + |
| 63 | +var path = d3.geo.path() |
| 64 | + .projection(projection); |
| 65 | + |
| 66 | +var svg = d3.select("body").insert("svg:svg", "h2") |
| 67 | + .attr("width", w) |
| 68 | + .attr("height", h); |
| 69 | + |
| 70 | +var states = svg.append("svg:g") |
| 71 | + .attr("id", "states"); |
| 72 | + |
| 73 | +var circles = svg.append("svg:g") |
| 74 | + .attr("id", "circles"); |
| 75 | + |
| 76 | +var cells = svg.append("svg:g") |
| 77 | + .attr("id", "cells"); |
| 78 | + |
| 79 | +d3.select("input[type=checkbox]").on("change", function() { |
| 80 | + cells.classed("voronoi", this.checked); |
| 81 | +}); |
| 82 | + |
| 83 | +d3.json("us-states.json", function(collection) { |
| 84 | + states.selectAll("path") |
| 85 | + .data(collection.features) |
| 86 | + .enter().append("svg:path") |
| 87 | + .attr("d", path); |
| 88 | +}); |
| 89 | + |
| 90 | +d3.csv("flights-airport.csv", function(flights) { |
| 91 | + var linksByOrigin = {}, |
| 92 | + countByAirport = {}, |
| 93 | + locationByAirport = {}, |
| 94 | + positions = []; |
| 95 | + |
| 96 | + var arc = d3.geo.greatArc() |
| 97 | + .source(function(d) { return locationByAirport[d.source]; }) |
| 98 | + .target(function(d) { return locationByAirport[d.target]; }); |
| 99 | + |
| 100 | + flights.forEach(function(flight) { |
| 101 | + var origin = flight.origin, |
| 102 | + destination = flight.destination, |
| 103 | + links = linksByOrigin[origin] || (linksByOrigin[origin] = []); |
| 104 | + links.push({source: origin, target: destination}); |
| 105 | + countByAirport[origin] = (countByAirport[origin] || 0) + 1; |
| 106 | + countByAirport[destination] = (countByAirport[destination] || 0) + 1; |
| 107 | + }); |
| 108 | + |
| 109 | + d3.csv("airports.csv", function(airports) { |
| 110 | + |
| 111 | + // Only consider airports with at least one flight. |
| 112 | + airports = airports.filter(function(airport) { |
| 113 | + if (countByAirport[airport.iata]) { |
| 114 | + var location = [+airport.longitude, +airport.latitude]; |
| 115 | + locationByAirport[airport.iata] = location; |
| 116 | + positions.push(projection(location)); |
| 117 | + return true; |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + // Compute the Voronoi diagram of airports' projected positions. |
| 122 | + var polygons = d3.geom.voronoi(positions); |
| 123 | + |
| 124 | + var g = cells.selectAll("g") |
| 125 | + .data(airports) |
| 126 | + .enter().append("svg:g"); |
| 127 | + |
| 128 | + g.append("svg:path") |
| 129 | + .attr("class", "cell") |
| 130 | + .attr("d", function(d, i) { return "M" + polygons[i].join("L") + "Z"; }) |
| 131 | + .on("mouseover", function(d, i) { d3.select("h2 span").text(d.name); }); |
| 132 | + |
| 133 | + g.selectAll("path.arc") |
| 134 | + .data(function(d) { return linksByOrigin[d.iata] || []; }) |
| 135 | + .enter().append("svg:path") |
| 136 | + .attr("class", "arc") |
| 137 | + .attr("d", function(d) { return path(arc(d)); }); |
| 138 | + |
| 139 | + circles.selectAll("circle") |
| 140 | + .data(airports) |
| 141 | + .enter().append("svg:circle") |
| 142 | + .attr("cx", function(d, i) { return positions[i][0]; }) |
| 143 | + .attr("cy", function(d, i) { return positions[i][1]; }) |
| 144 | + .attr("r", function(d, i) { return Math.sqrt(countByAirport[d.iata]); }) |
| 145 | + .sort(function(a, b) { return countByAirport[b.iata] - countByAirport[a.iata]; }); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | + </script> |
| 150 | + </body> |
| 151 | +</html> |
0 commit comments