-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparallel.html
More file actions
309 lines (283 loc) · 7.82 KB
/
Copy pathparallel.html
File metadata and controls
309 lines (283 loc) · 7.82 KB
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
<!doctype html>
<html>
<head>
<title>Parallel Coordinates</title>
<link type="text/css" rel="stylesheet" href="ex.css?3.2"/>
<script type="text/javascript" src="protovis-r3.2.js"></script>
<script type="text/javascript" src="paralleldata.js"></script>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<style type="text/css">
#fig {
width: 880px;
height: 600px;
}
#title {
position: absolute;
top: 70px;
left: 200px;
padding: 10px;
background: white;
}
large {
font-size: medium;
}
</style>
</head>
<body>
<script type="text/javascript">
var searchCountries = [""]
function search(){
searchCountries = $('#searchBox').val().split(" ")
updateFilter();
}
</script>
Country
<input type="text" id="searchBox" NAME="country" value = "" onkeypress = "search()">
</input>
<br><br>
<div id="center"><div id="fig">
<script type="text/javascript">
/*
* So, the trick is that some of the scales are inverted, and the filters should
* know about this.
*/
// The units and dimensions to visualize, in order.
var units = {
gdp: {name: "GDP Per Capita", unit: " current US$", invert: false, log: true},
net: {name: "Internet Users", unit: " per 100 people", invert: false, log: false},
imr: {name: "Infant Mortality Rate", unit: " per 1000 live births", invert: true, log: true},
death: {name: "Death Rate, Crude", unit: " per 1000 people", invert: true, log: false} //TODO: remove this variable, is not interesting
}
var dims = pv.keys(units);
/* Sizing and scales. */
var w = 820,
h = 600,
fudge = 0.49,
x = pv.Scale.ordinal(dims).splitFlush(0, w),
y = pv.dict(dims,
function(dim) {
if (units[dim].log) {
var domain = pv.Scale.log(cars,
function(country) {return Math.floor(country[dim])-fudge;},
function(country) {return Math.ceil(country[dim]) +fudge;}
);
} else {
var domain = pv.Scale.linear(cars,
function(country) {return Math.floor(country[dim])-fudge;},
function(country) {return Math.ceil(country[dim]) +fudge;}
);
}
return units[dim].invert ? domain.range(h, 0) : domain.range(0, h);
}),
color = function(country) {
var range;
switch (country.cont) {
case 1:
case 2:
case 3:
// Africa
range = ["#FDAE6B", "#A63603"];
break;
case 4:
case 5:
case 6:
case 7:
case 12:
// Asia
range = ["#BCBDDC", "#54278F"];
break;
case 8:
// South america
range = ["#A1D99B","#006D2C"]
break;
case 9:
case 11:
// Europe
range = ["#A63603", "#FDAE6B"]
break;
case 10:
// North america
range = ["#9ECAE1", "#08519C"];
break;
default:
console.log(country);
}
return pv.Scale.linear(0,1).range(pv.color(range[0]).rgb().alpha(0.7), pv.color(range[1]).rgb().alpha(0.7))(1);
}
/* Interaction state.
* filter has a function for each dimension that returns the min and max value currently selected
*/
var filter = pv.dict(dims, function(dim) {
return {min: y[dim].domain()[0], max: y[dim].domain()[1]};
}),
active = "gdp";
/* The root panel. */
var vis = new pv.Panel()
.width(w)
.height(h)
.left(60)
.right(130)
.top(30)
.bottom(20);
// The parallel coordinates display.
// All of the lines are rendered in grey for the background
vis.add(pv.Panel)
.data(cars)
.visible(function(d) {return dims.every(function(t) {return (d[t] >= filter[t].min) && (d[t] <= filter[t].max);});})
.add(pv.Line)
.data(dims)
.left(function(t, d) {return x(t);})
.bottom(function(t, d) {return y[t](d[t]);})
.strokeStyle("#ddd")
.lineWidth(1)
.antialias(false)
;
// Rule per dimension.
rule = vis.add(pv.Rule)
.data(dims)
.left(x);
// Dimension label
rule.anchor("top").add(pv.Label)
.top(-12)
.font("bold 10px sans-serif")
.text(function(dim) {return units[dim].name;});
// The parallel coordinates display.
// This guy is re-rendered every time we change the slider
var change = vis.add(pv.Panel);
var validCountries = 0;
// Pass in the countries we want to render
var line = change.add(pv.Panel)
.data(cars)
.visible(function(country) { return dims.every(function(dim) {
// This is the function that decides if a line is rendered or not
var i;
for (i in searchCountries) {
if (country.name.toLowerCase().indexOf(searchCountries[i].toLowerCase()) === 0) {
return (country[dim] >= filter[dim].min) && (country[dim] <= filter[dim].max);
}
}
return false;
});})
.add(pv.Line)
.data(dims) // Data for the lines are the dimensions
.left(function(t, d) {return x(t);})
.bottom(function(t, d) {return y[t](d[t]);})
// .strokeStyle(function(t, d) {return c[active](d[active]);})
.strokeStyle(function(t, d) {return color(d);})
.lineWidth(function() {return validCountries < 10 ? 4 : 4})
.add(pv.Label)
.visible(function(){ return (validCountries < 10) && this.index == dims.length-1;})
.textBaseline("middle")
.textMargin(function(){
return Math.random() * 40 + 20;
})
.text(function(dontKnow, country) {
return country.name;
});
;
// Updater for slider and resizer.
function update(d){
var t = d.dim;
if (units[t].invert) {
filter[t].max = Math.max(y[t].domain()[0], y[t].invert(h - d.y - d.dy));
filter[t].min = Math.min(y[t].domain()[1], y[t].invert(h - d.y));
} else {
filter[t].min = Math.max(y[t].domain()[0], y[t].invert(h - d.y - d.dy));
filter[t].max = Math.min(y[t].domain()[1], y[t].invert(h - d.y));
}
active = t;
updateFilter();
return false;
}
// Updater for slider and resizer.
function selectAll(d) {
if (d.dy < 3) {
var t = d.dim;
if (units[t].invert) {
filter[t].max = Math.max(y[t].domain()[0], y[t].invert(0));
filter[t].min = Math.min(y[t].domain()[1], y[t].invert(h));
} else {
filter[t].min = Math.max(y[t].domain()[0], y[t].invert(0));
filter[t].max = Math.min(y[t].domain()[1], y[t].invert(h));
}
d.y = 0; d.dy = h;
active = t;
updateFilter()
}
return false;
}
function updateFilter() {
validCountries = 0;
var i;
var country;
var i_country;
var dim, i_dim;
var valid;
for (i_country in cars) {
country = cars[i_country];
for (i in searchCountries) {
if (country.name.toLowerCase().indexOf(searchCountries[i].toLowerCase()) === 0) {
valid = true;
for (i_dim in dims) {
var dim = dims[i_dim];
if (!(country[dim] >= filter[dim].min && country[dim] <= filter[dim].max)) {
valid = false;
break;
}
}
if (valid) {
validCountries++;
}
}
}
}
change.render();
}
/* Handle select and drag */
var handle = change.add(pv.Panel)
.data(dims.map(function(dim) { return {y:0, dy:h, dim:dim}; }))
.left(function(t){
return x(t.dim) - 30;
})
.width(60)
.fillStyle("rgba(0,0,0,.001)")
.cursor("crosshair")
.event("mousedown", pv.Behavior.select())
.event("select", update)
.event("selectend", selectAll)
.add(pv.Bar)
.left(25)
.top(function(d) { return d.y; })
.width(10)
.height(function(d) {return d.dy; })
.fillStyle("rgba(0,0,0,0.1)")
.strokeStyle(function(t) {
return "hsla(0,0,50%,.5)"
})
.cursor("move")
.event("mousedown", pv.Behavior.drag())
.event("dragstart", update)
.event("drag", update);
handle.anchor("bottom").add(pv.Label)
.textBaseline("top")
.text(function(d){
if (units[d.dim].invert) {
return Math.abs(filter[d.dim].max.toFixed(0)) + units[d.dim].unit;
} else {
return Math.abs(filter[d.dim].min.toFixed(0)) + units[d.dim].unit;
}
});
handle.anchor("top").add(pv.Label)
.textBaseline("bottom")
.text(function(d){
if (units[d.dim].invert) {
return filter[d.dim].min.toFixed(0) + units[d.dim].unit;
} else {
return filter[d.dim].max.toFixed(0) + units[d.dim].unit;
}
});
vis.render();
updateFilter();
</script>
</div></div></body>
</html>