-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
277 lines (238 loc) · 7.83 KB
/
index.html
File metadata and controls
277 lines (238 loc) · 7.83 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.background path {
fill: none;
stroke: #ddd;
shape-rendering: crispEdges;
}
.foreground path {
fill: none;
/*stroke: steelblue;*/
}
.brush .extent {
fill-opacity: .3;
stroke: #fff;
shape-rendering: crispEdges;
}
.axis line,
.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
cursor: move;
}
</style>
<body>
<h1> Parallel Coordinates - Michelle and Shrestha </h1>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
// main structure of code from https://bl.ocks.org/jasondavies/1341281
var margin = {top: 30, right: 100, bottom: 10, left: 70},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangePoints([0, width], 1),
y = {},
dragging = {};
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
background,
foreground;
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var color = d3.scale.ordinal()
.domain(['Australia','Austria','Belgium','Canada','China','Denmark','Egypt','Finland','France','Germany','Hong Kong','Japan','Netherlands','New Zealand','Norway','Republic of Ireland','Singapore','South Africa','South Korea','Spain','Sweden','Switzerland','Taiwan','Turkey','United Kingdom','United States of America'])
.range(['green','black','black','blue', 'red','black','brown','black','black','black','black','red','red','black','black','black','red','brown','red','black','black','black','red','black','black','blue'])
d3.csv("timesData.csv", function(error, cars) {
// Extract the list of dimensions and create a scale for each.
// x.domain(dimensions = d3.keys(cars[0]).filter(function(d) {
// return (y[d] = d3.scale.linear()
// .domain(d3.extent(cars, function(p) { return +p[d]; }))
// .range([height, 0]))
// }));
x.domain(dimensions = d3.keys(cars[0]).filter(function(d) {
if(d === "country") {
y[d] = d3.scale.ordinal()
.domain(cars.map(function(p) { return p[d]; }).sort().reverse())
.rangePoints([height, 0]);
}
else if (d === "international"){
return false;
}
else if (d === "female_male_ratio"){
return false;
}
else if (d === "num_students"){
return false;
}
else if (d === "university_name"){
return false;
}
else if (d === "total_score"){
return false;
}
else if (d === "international_students"){
return false;
}
else if (d === "student_staff_ratio"){
return false;
}
else if (d === "world_rank"){
y[d] = d3.scale.linear()
.domain(d3.extent(cars, function(p) { return +p[d]; }).sort().reverse())
.range([height, 0]);
}
else if (d === "year"){
return false;
}
else{
y[d] = d3.scale.linear()
.domain(d3.extent(cars, function(p) { return +p[d]; }))
.range([height, 0]);
}
return true;
}));
// Add grey background lines for context.
background = svg.append("g")
.attr("class", "background")
.selectAll("path")
.data(cars)
.enter().append("path")
.attr("d", path);
// Add blue foreground lines for focus.
foreground = svg.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(cars)
.enter().append("path")
.attr("d", path)
.attr("stroke", function(d) {
var continent = d.country;
return color(continent);
})
// .attr('stroke', function(d) { return color(d.label); });
// Add a group element for each dimension.
var g = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; })
.call(d3.behavior.drag()
.origin(function(d) { return {x: x(d)}; })
.on("dragstart", function(d) {
dragging[d] = x(d);
background.attr("visibility", "hidden");
})
.on("drag", function(d) {
dragging[d] = Math.min(width, Math.max(0, d3.event.x));
foreground.attr("d", path);
dimensions.sort(function(a, b) { return position(a) - position(b); });
x.domain(dimensions);
g.attr("transform", function(d) { return "translate(" + position(d) + ")"; })
})
.on("dragend", function(d) {
delete dragging[d];
transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
transition(foreground).attr("d", path);
background
.attr("d", path)
.transition()
.delay(500)
.duration(0)
.attr("visibility", null);
}));
// Add an axis and title.
g.append("g")
.attr("class", "axis")
.each(function(d) { d3.select(this).call(axis.scale(y[d])); })
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) { return d; });
// Add and store a brush for each axis.
g.append("g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(y[d].brush = d3.svg.brush().y(y[d]).on("brushstart", brushstart).on("brush", brush));
})
.selectAll("rect")
.attr("x", -8)
.attr("width", 50);
var legendRectSize = 10;
var legendSpacing = 10;
var legend = d3.select('svg')
.append("g")
.selectAll("g")
.data(color.domain())
.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', function(d, i) {
var height = legendRectSize;
var x = 800;
var y = (i * height) + 50;
return 'translate(' + x + ',' + y + ')';
});
legend.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', color)
.style('stroke', color);
legend.append('text')
.attr('x', legendRectSize)
.attr('y', legendSpacing)
.text(function(d) {
if (d == 'United States of America' || d == 'Canada') {
return 'NA ' + d;
}
else if (d == 'Sweden' || d == 'Finland' || d == 'Norway' || d == 'Denmark' || d == 'Republic of Ireland' || d == 'United Kingdom' || d == 'Netherlands' || d == 'Belgium' || d == 'Germany' || d == 'Luxembourg'|| d == 'France' || d == 'Switzerland' || d == 'Austria' || d == 'Italy' || d == 'Spain' || d == 'Russia' || d == 'Turkey' ) {
return 'EU ' + d;
}
else if (d == 'Brazil') {
return 'SA ' + d;
}
else if (d == 'Egypt' || d == 'Israel' || d == 'South Africa'){
return 'AF ' + d;
}
else if(d == 'China' || d == 'South Korea' || d == 'Japan' || d == 'Hong Kong' || d== 'Taiwan' || d == 'Singapore'){
return 'AS ' + d;
}
else if (d == 'Australia' || d == 'New Zealand'){
return 'OC' + d;
}
});
});
function position(d) {
var v = dragging[d];
return v == null ? x(d) : v;
}
function transition(g) {
return g.transition().duration(500);
}
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p) { return [position(p), y[p](d[p])]; }));
}
function brushstart() {
d3.event.sourceEvent.stopPropagation();
}
// Handles a brush event, toggling the display of foreground lines.
function brush() {
var actives = dimensions.filter(function(p) { return !y[p].brush.empty(); }),
extents = actives.map(function(p) { return y[p].brush.extent(); });
foreground.style("display", function(d) {
return actives.every(function(p, i) {
return extents[i][0] <= d[p] && d[p] <= extents[i][1];
}) ? null : "none";
});
}
</script>