|
74 | 74 | </article> |
75 | 75 | {% endblock %} |
76 | 76 | {% block scripts %} |
77 | | - <script src="/assets/js/d3.v3.min.js"></script> |
78 | | - <script src="/assets/js/topojson.v1.min.js"></script> |
| 77 | + <script src="http://d3js.org/d3.v3.min.js"></script> |
| 78 | + <script src="http://d3js.org/topojson.v1.min.js"></script> |
79 | 79 | <script src="/assets/js/d3.tip.js"></script> |
80 | 80 | <script> |
81 | | -
|
82 | | - // Location |
83 | | - var LocationTools = { |
84 | | - degToRad: function(deg){ |
85 | | - return deg * Math.PI / 180; |
86 | | - }, |
87 | | - pythagorasEquirectangular: function(lat, long, lat2, long2){ |
88 | | - lat = LocationTools.degToRad(lat); |
89 | | - lat2 = LocationTools.degToRad(lat2); |
90 | | - long = LocationTools.degToRad(long); |
91 | | - long2 = LocationTools.degToRad(long2); |
92 | | - var R = 6371; // km |
93 | | - var x = (long2-long) * Math.cos((lat+lat2)/2); |
94 | | - var y = (lat2-lat); |
95 | | - var d = Math.sqrt(x*x + y*y) * R; |
96 | | - return d; |
97 | | - }, |
98 | | - nearestRegion: function(cities, lat, long){ |
99 | | - var mindif = 99999; |
100 | | - var closest; |
101 | | -
|
102 | | - for (index = 0; index < cities.length; ++index) { |
103 | | - var dif = LocationTools.pythagorasEquirectangular(lat, long, cities[index].lat, cities[index].lng); |
104 | | - if (dif < mindif){ |
105 | | - closest = index; |
106 | | - mindif = dif; |
107 | | - } |
108 | | - } |
109 | | - return cities[closest]; |
110 | | - } |
111 | | - } |
112 | | -
|
113 | 81 | batch_regions = [ |
114 | 82 | {% set is_first = true %} |
115 | 83 | {% for region in batch_regions %} |
|
124 | 92 | {% endfor %} |
125 | 93 | ]; |
126 | 94 |
|
127 | | - usCached = null; |
128 | | -
|
129 | | - var updateMap = function(us){ |
130 | | - var path = d3.geo.path(); |
131 | | -
|
132 | | - var map = $('#map').html('<svg></svg>'); |
133 | | - var width = map.width(); |
134 | | - var height = map.width() * 0.5; |
135 | | -
|
136 | | - var projection = d3.geo.albersUsa() |
137 | | - .scale((width)) |
138 | | - .translate([width / 2, height / 2]); |
139 | | -
|
140 | | - var path = d3.geo.path() |
141 | | - .projection(projection); |
142 | | -
|
143 | | - var svg = d3.select("#map svg") |
144 | | - .attr("width", width) |
145 | | - .attr("height", height); |
146 | | -
|
147 | | - var tip = d3.tip() |
148 | | - .attr('class', 'd3-tip') |
149 | | - .offset([-10, 0]) |
150 | | - .html(function(d) { |
151 | | - return d.name; |
152 | | - }); |
153 | | - svg.call(tip); |
154 | | -
|
155 | | - svg.insert("path", ".graticule") |
156 | | - .datum(topojson.feature(us, us.objects.land)) |
157 | | - .attr("class", "land") |
158 | | - .attr("d", path); |
159 | | -
|
160 | | - svg.insert("clipPath") |
161 | | - .attr("id", "landclip") |
162 | | - .append("path", ".graticule") |
163 | | - .datum(topojson.feature(us, us.objects.land)) |
164 | | - .attr("d", path); |
165 | | -
|
166 | | - svg.insert("path", ".graticule") |
167 | | - .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })) |
168 | | - .attr("class", "state-boundary") |
169 | | - .attr("d", path); |
| 95 | + var redraw = function(geodata) { |
| 96 | + var map = $('#map').html('<svg></svg>'); |
| 97 | + var width = map.width(); |
| 98 | + var height = map.width() * 0.55 / (width/630); |
| 99 | +
|
| 100 | + //Map projection |
| 101 | + var projection = d3.geo.mercator() |
| 102 | + .scale(530 * (width/630)) |
| 103 | + .center([-95,40]) //projection center |
| 104 | + .translate([width/2,height/2]) //translate to center the map in view |
| 105 | +
|
| 106 | + //Generate paths based on projection |
| 107 | + var path = d3.geo.path() |
| 108 | + .projection(projection); |
| 109 | +
|
| 110 | + //Create an SVG |
| 111 | + var svg = d3.select("#map svg") |
| 112 | + .attr("width", width) |
| 113 | + .attr("height", height); |
| 114 | +
|
| 115 | + //Group for the map features |
| 116 | + var features = svg.append("g") |
| 117 | + .attr("class","features"); |
| 118 | +
|
| 119 | +
|
| 120 | + var tip = d3.tip() |
| 121 | + .attr('class', 'd3-tip') |
| 122 | + .offset([-10, 0]) |
| 123 | + .html(function(d) { |
| 124 | + return d.name; |
| 125 | + }); |
| 126 | + svg.call(tip); |
| 127 | +
|
| 128 | + |
| 129 | + features.selectAll("path") |
| 130 | + .data(topojson.feature(geodata,geodata.objects.collection).features) |
| 131 | + .enter() |
| 132 | + .append("path") |
| 133 | + .attr("class", "land") |
| 134 | + .attr("d",path); |
170 | 135 |
|
171 | 136 | svg.selectAll("circle.footprint") |
172 | 137 | .data(batch_regions) |
|
184 | 149 | return "city footprint"; |
185 | 150 | } |
186 | 151 | }); |
187 | | -
|
188 | | - svg.selectAll("circle.impact") |
| 152 | + |
| 153 | + svg.selectAll("circle.impact") |
189 | 154 | .data(batch_regions) |
190 | 155 | .enter() |
191 | 156 | .append("circle") |
|
201 | 166 | return "city impact"; |
202 | 167 | } |
203 | 168 | }); |
204 | | -
|
205 | 169 | svg.selectAll("circle.outline") |
206 | 170 | .data(batch_regions) |
207 | 171 | .enter() |
|
218 | 182 | return "city outline"; |
219 | 183 | } |
220 | 184 | }); |
221 | | -
|
222 | 185 | svg.selectAll("circle.handle") |
223 | 186 | .data(batch_regions) |
224 | 187 | .enter() |
|
238 | 201 | .on('click', function(d){ |
239 | 202 | window.location.href = '/'+ d.id; |
240 | 203 | }); |
241 | | -
|
242 | 204 | d3.select(self.frameElement).style("height", height + "px"); |
243 | 205 | map.css('margin-top', '-'+(height/1.5)+'px'); |
244 | | - }; |
| 206 | + } |
245 | 207 |
|
| 208 | + var _geodata = null; |
246 | 209 | var update = function() { |
247 | | - // Caching the map really makes the load time quicker. |
248 | | - if(!usCached){ |
249 | | - d3.json("/assets/topo/us.json", function(error, us) { |
250 | | - usCached = us; |
251 | | - updateMap(us); |
252 | | - }); |
253 | | - }else{ |
254 | | - updateMap(usCached); |
| 210 | + if (_geodata == null) { |
| 211 | + d3.json("/assets/topo/us-ca.json",function(error,geodata) { |
| 212 | + if (error) return console.log(error); |
| 213 | + _geodata = geodata; |
| 214 | + redraw(_geodata); |
| 215 | + }); |
| 216 | + } else { |
| 217 | + redraw(_geodata); |
255 | 218 | } |
256 | | - }; |
| 219 | + } |
257 | 220 |
|
258 | 221 | update(); |
259 | 222 | $(window).on('resize', update); |
260 | | -
|
261 | | - // $(".wrap-inner").on("mouseenter", function(){ |
262 | | - // $("#lead-text").animate({ |
263 | | - // "margin-bottom": "220px" |
264 | | - // }); |
265 | | - // |
266 | | - // $(".lead").animate({ |
267 | | - // "margin-top": "0px" |
268 | | - // }); |
269 | | - // }); |
270 | | - // |
271 | | - // $(".wrap-inner").on("mouseleave", function(){ |
272 | | - // $("#lead-text").animate({ |
273 | | - // "margin-bottom": "0px" |
274 | | - // }); |
275 | | - // |
276 | | - // $(".lead").animate({ |
277 | | - // "margin-top": "5rem" |
278 | | - // }); |
279 | | - // }); |
280 | 223 | </script> |
281 | 224 | {% endblock %} |
0 commit comments