-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHB.html
245 lines (219 loc) · 9.3 KB
/
HB.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
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Hexbins and D3 Maps</title>
<!--
<script src='https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v1.6.2/mapbox.css' rel='stylesheet' />
-->
<script src='js/mapbox.js'></script>
<link href='css/mapbox.css' rel='stylesheet' />
<style>
body {
background-image:url('css/dark.png');
font-size: 12px;
}
text {
fill: grey;
}
#mapContainer {
margin: 0px;
background-color: #333;
width: 100%;
height: 600px;
}
#tooltip {
opacity: 1;
background: #333;
padding: 5px;
border: 1px solid lightgrey;
border-radius: 5px;
position: absolute;
z-index: 10;
visibility: hidden;
pointer-events: none;
}
</style>
</head>
<body>
<div id="mapContainer"></div>
<div id="tooltip">
<svg width="100px" height="100px"></svg>
</div>
<script src="js/d3.min.js"></script>
<script src="js/hexbin.js"></script>
<script src="js/simple_statistics.js"></script>
<script>
//**********************************************************************************
//******** LEAFLET HEXBIN LAYER CLASS *********************************************
//**********************************************************************************
L.HexbinLayer = L.Class.extend({
includes: L.Mixin.Events,
initialize: function (rawData, options) {
this.levels = {};
this.layout = d3.hexbin().radius(10);
this.rscale = d3.scale.sqrt().range([0, 10]).clamp(false);
this.rwData = rawData;
this.config = options;
},
project: function(x) {
var point = this.map.latLngToLayerPoint([x[1], x[0]]);
return [point.x, point.y];
},
getBounds: function(d) {
var b = d3.geo.bounds(d)
return L.bounds(this.project([b[0][0], b[1][1]]), this.project([b[1][0], b[0][1]]));
},
update: function () {
var pad = 100, xy = this.getBounds(this.rwData), zoom = this.map.getZoom();
this.container
.attr("width", xy.getSize().x + (2 * pad))
.attr("height", xy.getSize().y + (2 * pad))
.style("margin-left", (xy.min.x - pad) + "px")
.style("margin-top", (xy.min.y - pad) + "px");
if (!(zoom in this.levels)) {
this.levels[zoom] = this.container.append("g").attr("class", "zoom-" + zoom);
this.genHexagons(this.levels[zoom]);
this.levels[zoom].attr("transform", "translate(" + -(xy.min.x - pad) + "," + -(xy.min.y - pad) + ")");
}
if (this.curLevel) {
this.curLevel.style("display", "none");
}
this.curLevel = this.levels[zoom];
this.curLevel.style("display", "inline");
},
genHexagons: function (container) {
var data = this.rwData.features.map(function (d) {
var coords = this.project(d.geometry.coordinates)
return [coords[0],coords[1], d.properties];
}, this);
var bins = this.layout(data);
var hexagons = container.selectAll(".hexagon").data(bins);
var counts = [];
bins.map(function (elem) { counts.push(elem.length) });
this.rscale.domain([0, (ss.mean(counts) + (ss.standard_deviation(counts) * 10))]);
var path = hexagons.enter().append("path").attr("class", "hexagon");
this.config.style.call(this, path);
that = this;
hexagons
.attr("d", function(d) { return that.layout.hexagon(that.rscale(d.length)); })
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.on("mouseover", function (d) {
var s=0, k=0;
d.map(function(e){
if (e.length === 3) e[2].group === 1 ? ++k : ++s;
});
that.config.mouse.call(this, [s,k]);
d3.select("#tooltip")
.style("visibility", "visible")
.style("top", function () { return (d3.event.pageY - 130)+"px"})
.style("left", function () { return (d3.event.pageX - 130)+"px";})
})
.on("mouseout", function (d) { d3.select("#tooltip").style("visibility", "hidden") });
},
addTo: function (map) {
map.addLayer(this);
return this;
},
onAdd: function (map) {
this.map = map;
var overlayPane = this.map.getPanes().overlayPane;
if (!this.container || overlayPane.empty) {
this.container = d3.select(overlayPane)
.append('svg')
.attr("id", "hex-svg")
.attr('class', 'leaflet-layer leaflet-zoom-hide');
}
map.on({ 'moveend': this.update }, this);
this.update();
}
});
L.hexbinLayer = function (data, styleFunction) {
return new L.HexbinLayer(data, styleFunction);
};
//**********************************************************************************
//******** IMPORT DATA AND REFORMAT ***********************************************
//**********************************************************************************
d3.csv('data/vaf.csv', function (error, coffee) {
function reformat (array) {
var data = [];
array.map(function (d){
data.push({
properties: {
group: +d.WelfareScore,
city: d.Thresholds,
state: d.Governorate,
store: d.HouseholdInformationFamilySize
},
type: "Feature",
geometry: {
coordinates:[+d.longitudee,+d.latitudee],
type:"Point"
}
});
});
return data;
}
var geoData = { type: "FeatureCollection", features: reformat(coffee) };
//**********************************************************************************
//******** CREATE LEAFLET MAP *****************************************************
//**********************************************************************************
var cscale = d3.scale.linear().domain([0,1]).range(["#00FF00","#FFA500"]);
// PLEASE DO NOT USE MY MAP ID :) YOU CAN GET YOUR OWN FOR FREE AT MAPBOX.COM
// Jordan @ 7/31.048/36.519
var leafletMap = L.mapbox.map('mapContainer', 'unhcr.kie3eda0')
.setView([31.048, 36.519], 7);
// var leafletMap = L.tileLayer.map('mapContainer','http://otile2.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png')
// .setView([31.048, 36.519], 7);
// PLEASE DO NOT USE MY MAP ID :) YOU CAN GET YOUR OWN FOR FREE AT MAPBOX.COM
// var leafletMap = L.mapbox.map('mapContainer', 'delimited.ge9h4ffl')
// .setView([40.7, -73.8], 11);
//**********************************************************************************
//******** ADD HEXBIN LAYER TO MAP AND DEFINE HEXBIN STYLE FUNCTION ***************
//**********************************************************************************
var hexLayer = L.hexbinLayer(geoData, {
style: hexbinStyle,
mouse: makePie
}).addTo(leafletMap);
function hexbinStyle(hexagons) {
hexagons
.attr("stroke", "black")
.attr("fill", function (d) {
var values = d.map(function (elem) {
return elem[2].group;
})
var avg = d3.mean(d, function(d) { return +d[2].group; })
return cscale(avg);
});
}
//**********************************************************************************
//******** PIE CHART ROLL-OVER ****************************************************
//**********************************************************************************
function makePie (data) {
d3.select("#tooltip").selectAll(".arc").remove()
d3.select("#tooltip").selectAll(".pie").remove()
var arc = d3.svg.arc()
.outerRadius(45)
.innerRadius(10);
var pie = d3.layout.pie()
.value(function(d) { return d; });
var svg = d3.select("#tooltip").select("svg")
.append("g")
.attr("class", "pie")
.attr("transform", "translate(50,50)");
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d, i) { return i === 1 ? "#FFA500":"#00FF00"; });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.style("text-anchor", "middle")
.text(function (d) { return d.value === 0 ? "" : d.value; });
}
});
</script>
</body>
</html>