-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
166 lines (137 loc) · 4.74 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
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<title>Zip Decode</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="queue.min.js"></script>
<style type="text/css">
/* HTML styling */
html { background-color: #333333 }
body {
font-family: Helvetica, Arial, sans-serif; font-size: 16px;
color: #cbcbcb;
}
a { text-decoration: none; color: #999a63 }
#map { width: 1200px; height: 600px; }
/* SVG styling */
svg { background-color: #333333 }
text { fill: #cbcbcb; font-size: 24px; }
#states { fill: none; stroke: #555555; stroke-width: 0.5px; }
rect {
transition: fill 0.4s;
-webkit-transition: fill 0.4s;
-o-transition: fill 0.4s;
}
.selected { fill: #cbcbcb }
.unselected { fill: #999a63 }
</style>
</head><body>
<!-- Static HTML defining the page -->
<center>
<div id="map"></div>
<p style="padding-top: 50px; font-size: 120%">
Please type a zip code
</p>
<p>A reimplementation of
<a href="http://benfry.com/">Ben Fry</a>'s
<a href="http://benfry.com/zipdecode/">zipdecode (2004)</a><br>
In Javascript with <a href="http://d3js.org/">D3</a>
by <a href="http://www.somebits.com/weblog/">Nelson Minar</a>
</p>
</center>
<a href="https://github.com/NelsonMinar/zipdecode-js"><img
style="position:absolute;top:0;right:0;border:0;"
width="149" height="149" src="forkme.png" alt="Fork me on GitHub"
/></a>
<script type="text/javascript">
// Display: configurable parameters (see also HTML)
var width = 1200, height = 600;
var instructions = "";
// Display: geographic projection
var proj = d3.geo.albersUsa().scale(1200)
.translate([width / 2, height / 2]);
var path = d3.geo.path().projection(proj);
// Interaction: stored state
var selectedZip = "";
// Display: AJAX load the data files, then call render()
queue()
.defer(d3.json, "us-states.geojson")
.defer(d3.tsv, "zips.tsv")
.await(render);
// Interaction: handle keyboard input
function key() {
var code = d3.event.keyCode;
if (code == 32) {
// Space: clear code
updateSelection("");
} else if (code == 37 || code == 8) {
// Backspace / left arrow: remove one number
if (selectedZip.length > 0) {
updateSelection(selectedZip.substr(0, selectedZip.length - 1));
}
// Prevent the browser from going back in the URL history
d3.event.preventDefault();
} else if (code >= 48 && code <= 57) {
// number keys
appendToSelection(String.fromCharCode(code))
} else if (code >= 96 && code <= 105) {
// numeric keypad
appendToSelection(String.fromCharCode(code-48));
}
};
// Interaction: add a single digit to the zip if possible
function appendToSelection(digit) {
if (selectedZip.length < 5) {
updateSelection(selectedZip + "" + digit);
}
}
// Data: is the given zip code in the selection?
function zipSelected(zip) {
var l = selectedZip.length;
return l > 0 && zip.substr(0, l) == selectedZip;
}
// Interaction: update the selected zip code
function updateSelection(n) {
selectedZip = n;
var l = selectedZip.length;
// Set the text label
var t = l > 0 ? selectedZip : instructions;
d3.select("#selected").text(t);
var start = Date.now();
// Recolor all the zip dots. Sadly this is slow, a transition won't work
d3.select("#zipdots").selectAll("rect")
.attr("class", function(d) { return zipSelected(d.zip) ? "selected" : "unselected" });
console.log("Update dots: " + (Date.now() - start) + "ms");
}
// Display: create the SVG, draw the map
function render(error, states, zips) {
// Display: the main SVG container
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
// Interaction: text box displaying the selection
svg.append("text").attr("id", "selected")
.text(instructions)
.attr("x", 20).attr("y", 50);
// Display: state outlines
svg.append("g").attr("id", "states");
d3.select("#states").selectAll("path")
.data(states.features)
.enter().append("path")
.attr("d", path);
var start = Date.now();
// Display: all the dots for zip code centroids
svg.append("g").attr("id", "zipdots");
d3.select("#zipdots").selectAll("rect")
.data(zips)
.enter().append("rect")
.attr("x", function(d) { var p = proj([d.lon, d.lat]); return p ? p[0] : null; })
.attr("y", function(d) { var p = proj([d.lon, d.lat]); return p ? p[1] : null; })
.attr("class", "unselected")
.attr("width", 1).attr("height", 1);
console.log("Draw dots: " + (Date.now() - start) + "ms");
// Interaction: handle keyboard events (keydown to capture backspace)
d3.select("body").on("keydown", key);
};
</script>
</body></html>