-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-20interactivemap.html
executable file
·160 lines (140 loc) · 4.78 KB
/
11-20interactivemap.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>D3 Interactions & Transitions</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-legend/2.25.6/d3-legend.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<style>
.zipcode:hover {
z-index: 1000;
fill: #ef476f;
transition: .3s;
}
#tooltip.hidden {
display: none;
}
#tooltip {
position: absolute;
padding: 5px;
background-color: white;
border: solid gray 1px;
z-index: 10;
pointer-events: none;
}
#map-zoomer {
position: absolute;
writing-mode: bt-lr;
-webkit-appearance: slider-vertical;
width: 8px;
height: 100px;
padding: 0 5px;
position: absolute;
top: 110px;
left: 22px;
}
#float-button-group {
position: fixed;
left: 10px;
opacity: 0.5;
}
#float-button-group:hover {
opacity: 1;
}
</style>
</head>
<body>
<div id="tooltip" class="hidden">
<p><span id="value">100</span></p>
</div>
<div id="main">
<div class="btn-group-vertical" role="group" aria-label="..." id="float-button-group">
<button type="button" class="btn btn-default" id="zoom-in"><span class="glyphicon glyphicon-zoom-in"
aria-hidden="true"></span></button>
<button type="button" class="btn btn-default" id="zoom-out"><span class="glyphicon glyphicon-zoom-out"
aria-hidden="true"></span></button>
<button type="button" class="btn btn-default" id="reset"><span class="glyphicon glyphicon-screenshot"
aria-hidden="true"></span></button>
</div>
<input type="range" value="1" min="1" max="8" orient="vertical" id="map-zoomer" />
</div>
<script type="text/javascript">
var nycgeourl = 'datas/00-tractTOzcta/Modified Zip Code Tabulation Areas (MODZCTA).geojson';
var interneturl = 'datas/2-InternetMerged.csv';
var height = 800, width = 800;
var svg = d3.select('#main').append('svg').attr('height', height).attr('width', width).attr('style', 'border: solid 1px black');
var projection = d3.geoMercator()
.center([-73.94, 40.70])
.scale(70000)
.translate([width / 2, height / 2]);
var path = d3.geoPath().projection(projection);
// var zoom = d3.zoom().scaleExtent([1, 8]).on('zoom', zoomed);
// svg.call(zoom);
var nycgrp = svg.append('g');
var internet = d3.map();
var promises = [d3.json(nycgeourl), d3.csv(interneturl, function (d) {
internet.set(+d.modzcta, +d.perc);
})];
Promise.all(promises).then(ready);
function ready([nyc]) {
nyc.features.pop();
var colors = d3.scaleQuantize().domain(d3.extent(internet.values())).range(d3.schemeBlues[5]);
nycgrp.selectAll('path').data(nyc.features).enter()
.append('path').attr('class', 'zipcode')
.attr('fill', d => colors(internet.get(d.properties.modzcta))).attr('stroke', 'gray').attr('d', path)
.on('mouseover', function (d) {
d3.select('#tooltip')
.style('left', d3.event.pageX + 'px')
.style('top', d3.event.pageY + 'px')
.select('#value')
.html('<p>No internet percentage:<br>' + String((100 * internet.get(d.properties.modzcta)).toFixed(2)) + '%</p>');
d3.select('#tooltip')
.classed('hidden', false);
})
.on('mousemove', function (d) {
d3.select('#tooltip')
.style('left', d3.event.pageX + 'px')
.style('top', d3.event.pageY + 'px')
.select('#value')
.html('<p>No internet percentage:<br>' + String((100 * internet.get(d.properties.modzcta)).toFixed(2)) + '%</p>');
d3.select('#tooltip')
.classed('hidden', false);
})
.on('mouseout', function () {
d3.select('#tooltip').classed('hidden', true);
});;
// legend
var cellNum = 9;
var colorLegend = d3.legendColor()
.shapeWidth(50)
.shapeHeight(10)
.cells(cellNum)
.shapePadding(0)
.labelFormat(d3.format('.2f'))
.labels(['0'])
.title('Percentage of the households with no Internet access')
.orient('horizontal')
.scale(colors);
svg.append('g')
.attr('transform', 'translate(100, 100)')
// .append(() => legend({color, title: 'Tweets per neighborhood (log scale)', width: 500}));
.call(colorLegend);
// legend title location and size
d3.select('.legendTitle')
.attr('transform', 'translate(0, 5)')
.attr('font-size', '12px');
// labels
d3.selectAll('.label')
.style('display', 'none');
};
// function zoomed() {
// svg.selectAll('path').attr('transform', d3.event.transform);
// };
</script>
</body>
</html>