-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html~
434 lines (356 loc) · 10.6 KB
/
index.html~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!-- The map and zoom and click functionality was created by Mike Bostock. (http://bl.ocks.org/mbostock/2206590)
Master test
I've simply built off of his great work. Please send suggestions to leishman3@gmail.com
Alexander Leishman
8/29/2013
-->
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.lead {
text-align: center;
margin: 0;
}
.background {
fill: #A0C3FF;
pointer-events: all;
}
#states {
fill: #DCDCDC;
}
#states .active {
fill: #1874CD; /* originally 'orange' */
}
#state-borders {
fill: none;
stroke: #fff;
stroke-width: 1.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
div.tooltip {
position: relative;
text-align: center;
width: 70px;
height: 28px;
padding: 0px;
font: 12px sans-serif;
background: none;
border: 0px;
border-radius: 0px;
pointer-events: none;
}
body .modal {
width: 1000px; /* SET THE WIDTH OF THE MODAL */
margin: 0px 0 0 -450px; /* CHANGE MARGINS TO ACCOMODATE THE NEW WIDTH (original = margin: -250px 0 0 -280px;) */
}
.modal-body{
height: 700px;
margin-bottom: 0;
}
iframe {
height: 600px;
margin-bottom: 0;
}
svg {
margin-left: auto;
margin-right: auto;
margin-top: 0;
display: block;
width: 1100;
}
.footer {
margin-top: 10px;
text-align: center;
}
#map {
margin: 0 auto;
}
</style>
<body>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="js/bootstrap.js"></script>
<script src = "../d3/d3.v3.js"></script>
<!-- <script src="../d3/"></script> -->
<!-- <script src="http://d3js.org/d3.v3.min.js"></script> -->
<script src="../d3/topojson.v1.min.js"></script>
<div class="page-header" style="margin:0">
<h1> Maply v0.1 <small>by <a href="http://www.github.com">Alexander Leishman</a></small></h1>
</div>
<h5 class="lead" style="margin-bottom:3px">Click a state, any state.</h5>
<div id="map">
</div>
<script>
var width = 1100,
height = 620,
centered;
var margin = {top: 10, left: 10, bottom: 10, right: 10}
, width = parseInt(d3.select('#map').style('width'))
, width = width - margin.left - margin.right
, mapRatio = .5
, height = width * mapRatio;
// Define projection type
var projection = d3.geo.albersUsa()
.scale(width)
.translate([width / 2, height / 2]);
// Define tooltip
var div = d3.select("#map").append("div")
.attr("class", "tooltip")
.style("margin", "0 auto")
.style("opacity", 0);
var path = d3.geo.path()
.projection(projection);
// Create svg
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
// Create background for map
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", clicked);
var g = svg.append("g");
d3.select(window).on('resize', resize);
d3.json("us.json", function(error, us) {
g.append("g")
.attr("id", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.attr("class", "state")
// BEGIN ON MOUSEOVER
.on("mouseover", function(d){
if(!centered) {
d3.select(this).style("fill", "aliceblue");
}
// Make opacity zero if map is zoomed in
div.transition()
.duration(0)
.style("opacity", function (d) {
if(centered) {
return 0;
}
else{
return 0.9;
}
});
div .text(stateName(d.id)) // Need to find better way for positioning state names
// .style("left", (path.centroid(d)[0]+25) + "px")
// .style("top", (path.centroid(d)[1]+90) + "px");
})
// END ON MOUSEOVER
// BEGIN ON MOUSOUT
.on("mouseout", function(d){
if(!centered){
d3.select(this).style("fill", "#DCDCDC");
}
div.transition()
.duration(0)
.style("opacity",0);
})
.on("click", function(d) {
d3.select(this).style("fill", "#1874CD");
clicked(d);
});
// END ON MOUSOUT
// Create State Borders
g.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("id", "state-borders")
.attr("d", path);
// CODE TO SHOW ALL STATE LABELS
// g.selectAll("text")
// .data(topojson.feature(us, us.objects.states).features)
// .enter()
// .append("svg:text")
// .text(function(d){return stateName(d.id);})
// .attr("x", function(d){
// return path.centroid(d)[0];
// })
// .attr("y", function(d){
// return path.centroid(d)[1];
// })
// .attr("text-anchor","middle")
// .attr('font-size','6pt')
// .attr("class","state-name");
});
// Function to look up ANSI state ID from JSON and find actual state name (yes I know it's ugly, future refactor)
function stateName(id) {
states = [["Alabama",1],
["Alaska",2],
["Arizona",4],
["Arkansas",5],
["California",6],
["Colorado",8],
["Connecticut",9],
["Delaware",10],
["District of Columbia",11],
["Florida",12],
["Georgia",13],
["Hawaii",15],
["Idaho",16],
["Illinois",17],
["Indiana",18],
["Iowa",19],
["Kansas",20],
["Kentucky",21],
["Louisiana",22],
["Maine",23],
["Maryland",24],
["Massachusetts",25],
["Michigan",26],
["Minnesota",27],
["Mississippi",28],
["Missouri",29],
["Montana",30],
["Nebraska",31],
["Nevada",32],
["New Hampshire",33],
["New Jersey",34],
["New Mexico",35],
["New York",36],
["North Carolina",37],
["North Dakota",38],
["Ohio",39],
["Oklahoma",40],
["Oregon",41],
["Pennsylvania",42],
["Rhode Island",44],
["South Carolina",45],
["South Dakota",46],
["Tennessee",47],
["Texas",48],
["Utah",49],
["Vermont",50],
["Virginia",51],
["Washington",53],
["West Virginia",54],
["Wisconsin",55],
["Wyoming",56]];
for (var i = 0; i < states.length; i++) {
if (id == states[i][1]) {
return states[i][0];
}
}
}
// END stateName()
// Executed when a state is clicked
function clicked(d) {
var x, y, k;
if (d && centered !== d) {
zoomIn(d);
}
else {
zoomOut(d);
}
if (centered){
// Create variable for state name string if not centered
state = stateName(d.id);
$("h4").text("Tryna learn about " + state + "? Check this out:");
if (state == "Georgia") {
pageSource = "http://en.wikipedia.org/wiki/Georgia_(U.S._state)"
}
else{
pageSource = "http://www.wikipedia.org/wiki/" + state;
}
$("iframe").attr("src", pageSource)
$("#myModal").modal();
console.log(state);
}
}
// End clicked
function zoomOut() {
x = width / 2;
y = height / 2;
k = 1;
centered = null;
// g.selectAll("path")
// .classed("active", centered && function(d) { return d === centered; });
g.transition()
.duration(750)
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
.style("stroke-width", 1.5 / k + "px");
}
function zoomIn(d) {
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
g.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
g.transition()
.duration(750)
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
.style("stroke-width", 1.5 / k + "px");
}
function resize() {
// d3.select("#map").append("svg")
// .attr("class", "old");
// adjust things when the window size changes
width = parseInt(d3.select('#map').style('width'));
// widthb = parseInt(d3.select('rect .background').style('width'));
// console.log(widthm + " " + widthb);
width = width - margin.left - margin.right;
height = width * mapRatio;
// update projection
projection
.translate([width / 2, height / 2])
.scale(width);
// resize the map container
d3.select("svg")
.attr("width", width)
.attr("height", height);
// Create background for map
svg.select("rect")
.attr("width", width)
.attr("height", height);
// resize the map
g.select('#state-borders').attr('d', path);
g.selectAll('.state').attr('d', path);
}
</script>
<body>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">State Information</h3>
</div>
<div class="modal-body">
<h4>replace</h4>
<iframe src="" style="zoom:0.60" width="100%" height="400" frameborder="0"></iframe>
</div>
<div class="modal-footer">
<button class="btn btn-primary" id="close" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
<!-- Not crucial, but we put this under a CC By-SA 3.0 license. -->
<!-- http://creativecommons.org/licenses/ -->
<div class="row-fluid footer">
<div class="span12">
<p class="muted">This work is licensed under
the <a href="http://creativecommons.org/licenses/by-sa/3.0/">CC
By-SA 3.0
</a>. Lots of credit goes to <a href="http://www.github.com/mbostock">Mike Bostock</a> for building the original map, zoom functionality and d3.js. You're a baller. </p>
</div>
</div>
</div>
<script type="text/javascript">
$('#myModal').on('hidden', function () {
zoomOut();
});
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-43640478-1', 'streetstand.com');
ga('send', 'pageview');
var padding = parseInt(jQuery(".background").css("margin-left"));
console.log(padding);
</script>
</body>