-
Notifications
You must be signed in to change notification settings - Fork 0
/
khooshe.html
executable file
·205 lines (172 loc) · 6.35 KB
/
khooshe.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
<!DOCTYPE html>
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8"><![endif]-->
<!--[if IE 8]><html class="no-js lt-ie9"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
<html>
<head>
<link rel="stylesheet" href="static/css/bootstrap.css" rel="stylesheet" />
<link rel="stylesheet" href="static/css/main.css" rel="stylesheet" />
<script src="static/js/jquery-1.11.1.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<script src="static/js/d3.v3.min.js"></script>
<script src="static/js/queue.v1.min.js"></script>
<script src="static/js/topojson.v1.min.js"></script>
</head>
<body>
<div class="container" style="text-align: center;">
<nav class="navbar navbar-custom">
<h1 style="text-align:center">Khooshe Visualizer</h1>
<p>double click on points</p>
</nav>
<div id='container'></div>
</div>
<script type="text/javascript">
var width = 1000,
height = 1000;
var layer = 0;
var past;
var name;
var mean;
var k = 0;
var projection = d3.geo.mercator()
.precision(.1);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#container").insert("svg:svg")
.attr("width", width)
.attr("height", height);
var all = svg.append("svg:g")
.attr("id", "all");
var countries = all.append("svg:g")
.attr("id", "countries");
var cells = all.append("svg:g")
.attr("id", "cells");
queue()
.defer(d3.json, 'static/json/readme-world.json')
.defer(d3.csv, 'static/tiles/0/0.csv')
.await(ready);
function ready(error, world, start_loc) {
countries.append("path")
.datum(topojson.feature(world, world.objects.countries))
.attr("class", "countries")
.attr("d", path)
.attr("fill", "#C0C0C0")
.attr("stroke-width", "1")
.attr("stroke", "#DAA520");
show_first_points(start_loc);
}
function show_first_points(start_loc) {
var start_positions = [];
var points_label = [];
start_loc.forEach(function(points) {
start_positions.push(projection([+points.longitude, +points.latitude]));
points_label.push(parseFloat(points.label));
});
var min_label = Math.min.apply(Math, points_label);
var max_label = Math.max.apply(Math, points_label);
var g = cells.selectAll("g")
.data(start_loc)
.enter().append("svg:g");
g.append("svg:circle")
.style("fill", "#40E0D0")
.attr("cx", function(d, i) { return start_positions[i][0]; })
.attr("cy", function(d, i) { return start_positions[i][1]; })
.attr("r", function(d, i) { return (((points_label[i]-min_label)/(max_label - min_label))+1)*10; })
.style("fill-opacity", 0.5)
.attr("stroke-width", "1")
.attr("stroke", "#993300")
.on("click", function(d,i) { click(i); });
}
function click() {
d3.selectAll("circle").on("click", function(d, i){
layer = layer + 1
name, layer = get_next_name(i, layer);
add_next_pooints(name, layer);
});
}
function get_next_name(i, layer) {
if (layer == 1) {
name = layer + "/" + i + ".csv"
past = i
}else{
past = past + "_" + i
name = layer + "/" + past + ".csv"
};
return name, layer;
}
function add_next_pooints(name, layer) {
svg.selectAll("#cells").remove();
d3.csv('static/tiles/' + name, function(points) {
var positions = [];
var points_label = [];
var lat_lon = [];
var cell = all.append("svg:g")
.attr("id", "cells");
var g = cell.selectAll("g")
.data(points)
.enter().append("svg:g");
var lat_lon = [];
points.forEach(function(point) {
lat_lon.push([+point.longitude, +point.latitude]);
positions.push(projection([+point.longitude, +point.latitude]));
points_label.push(point.label);
});
if (layer==1) {
var rsize = 2
}else{
var rsize = layer/2
}
g.append("svg:circle")
.attr("cx", function(d, i) { return positions[i][0]; })
.attr("cy", function(d, i) { return positions[i][1]; })
.attr("r", rsize)
.style("fill", "#CC33FF")
.on("click", function(d,i) { click(i); });
if (points_label[0]=='p') {
d3.selectAll("circle")
.style("fill", "#FF0000")
.attr("r", 0.1);
}
zoom(positions);
});
}
function zoom(positions) {
var all_lats = [];
var all_lons = [];
positions.forEach(function(point){
all_lats.push(point[0]);
all_lons.push(point[1]);
});
var min_lat = d3.min(all_lats);
var min_lon = d3.min(all_lons);
var max_lat = d3.max(all_lats);
var max_lon = d3.max(all_lons);
var cell = all.append("svg:g")
.attr("id", "cells");
var g = cell.selectAll("g")
.data(positions)
.enter().append("svg:g");
mean_point = [(max_lat+min_lat)/2, (max_lon+min_lon)/2];
var area = (max_lon-min_lon)*(max_lat-min_lat);
if (area==0) {
good_scale=20;
}else{
var canvas_area = width*height;
var box_area = canvas_area/area;
var good_scale = Math.sqrt(box_area,2);
good_scale = good_scale * 60/100;
}
var far = [good_scale*mean_point[0], good_scale*mean_point[1]];
var dist = [far[0]-width, far[1]-height];
var t = [-1*(dist[0]+width/2), -1*(dist[1]+height/2)];
t= [t[0],t[1]];
d3.selectAll("#all")
.transition()
.duration(1000).attr("transform", "translate(" + t + ")scale(" + good_scale + ")");
d3.selectAll("circle").attr("r",(1/good_scale)*5);
d3.selectAll("path").attr("stroke-width", (1/good_scale)*2);
}
</script>
</body>
</html>