-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment2.html
418 lines (371 loc) · 14 KB
/
assignment2.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Assignment 2</title>
</head>
<body>
<h3>CMPS-3360 Assignment 2</h3>
<span>Alan Braeley & Carolina Hurtado-Pulido</span>
<br/>
<hr/>
<br/>
<br/>
<script src="scores.js" charset="utf-8"></script>
<script src="https://d3js.org/d3.v4.js"></script>
<div id="D3Scatter1">
<h2>1. Reproduce Scatter Plot in D3</h2>
<svg id="D3ScatterSVG" width="1000" height="1000" viewbox="0 0 1000 1000">
<text x="500" y="990">SATM</text>
<text x="30" y="35">SATV</text>
</svg>
<script>
var D3ScatterSVG = d3.select("#D3ScatterSVG")
D3ScatterSVG.append("rect")
.attr("x", "50")
.attr("y", "10")
.attr("width", "850")
.attr("height", "850")
.attr("fill", "white")
.attr("stroke", "black")
.attr("stroke-width", "1 px");
// Scatter circles
for(i=0; i< scores.length; i++) {
if (scores[i].GPA >= 3.0) {
var col ='green'
}
else if (scores[i].GPA < 3.0 && scores[i].GPA >= 2.0) {
var col ='yellow'
}
else {
var col ='red'
}
D3ScatterSVG.append("circle")
.attr("cx", scores[i].SATM)
.attr("cy", scores[i].SATV)
.attr("r", scores[i].ACT/2.5)
.attr("transform", "translate(0,1000), scale(1,-1)")
.style("fill", col)
.style("stroke", "black");
}
</script>
</div>
<div id="D3Scatter2">
<h2>2. Color</h2>
<div>
<h3>2a - Continuously interpolated (linear) color for the GPA values</h3>
<svg id="scatter2a" width="1000" height="1000">
<rect x="50" y="50" width="900" height="900" style="fill:white; stroke:black; stroke-width:1px"/>
<text x="500" y="990">SATM</text>
<text x="30" y="35">SATV</text>
<text x="70" y="85">GPA - Point color (Green are large values, Red are small values) </text>
</svg>
<script>
var minGPA=d3.min(scores, d =>d.GPA);
var maxGPA=d3.max(scores, d =>d.GPA);
var axis=[];
for (i=1; i<10; i++){
axis[i-1]=i*100;
}
var colorLinear=d3.scaleLinear().domain([minGPA, maxGPA]).range(["red", "green"]);
var scatter=d3.select("#scatter2a");
//Scatter circles
scatter.selectAll("circle")
.data(scores)
.enter()
.append("circle")
.attr("cx", d=>d.SATM)
.attr("cy", d=> 1000-(d.SATV)+50)
.attr("r", d=>d.ACT/2.5)
.style("fill", d=>colorLinear(d.GPA))
.style("stroke", "#bcbcbc");
//X-axis
scatter.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", d=>d)
.attr("y", 970) //Fix y
.text(d=>d);
//Y-Axis
scatter.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", 20) //Fix x
.attr("y", d=>1000-d)
.text(d=>d);
</script>
</div>
<div>
<h3>2b - Create a continuous color scale that uses three of the colors from the GnBu-5 for the GPA values</h3>
<svg id="scatter2b" width="1000" height="1000">
<rect x="50" y="50" width="900" height="900" style="fill:white; stroke:black; stroke-width:1px"/>
<text x="500" y="990">SATM</text>
<text x="30" y="35">SATV</text>
<text x="70" y="85">GPA - Point color (Ligh colors are smaller values) </text>
</svg>
<script>
var colorLinear3=d3.scaleLinear().domain([minGPA,(minGPA+maxGPA)/2 ,maxGPA]).range(["#f0f9e8", "#7bccc4", "#0868ac"]);
var scatterb=d3.select("#scatter2b");
//Scatter circles
scatterb.selectAll("circle")
.data(scores)
.enter()
.append("circle")
.attr("cx", d=>d.SATM)
.attr("cy", d=> 1000-(d.SATV)+50)
.attr("r", d=>d.ACT/2.5)
.style("fill", d=>colorLinear3(d.GPA)) // I was doing this line like for 3 hours!!!
.style("stroke", "#bcbcbc");
//X-axis
scatterb.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", d=>d)
.attr("y", 970) //Fix y
.text(d=>d);
//Y-Axis
scatterb.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", 20) //Fix x
.attr("y", d=>1000-d)
.text(d=>d);
</script>
</div>
<div>
<h3>2c - Create a quantized color scale that uses all five colors of GnBu-5 for the GPA values</h3>
<svg id="scatter2c" width="1000" height="1000">
<rect x="50" y="50" width="900" height="900" style="fill:white; stroke:black; stroke-width:1px"/>
<text x="500" y="990">SATM</text>
<text x="30" y="35">SATV</text>
<text x="70" y="85">GPA - Point color (Ligh colors are smaller values, 5 bins) </text>
</svg>
<script>
var colorQuantize=d3.scaleQuantize().domain([minGPA, maxGPA]).range(["#f0f9e8", "#bae4bc", "#7bccc4", "#43a2ca", "#0868ac"]);
var scatterc=d3.select("#scatter2c");
//Scatter circles
scatterc.selectAll("circle")
.data(scores)
.enter()
.append("circle")
.attr("cx", d=>d.SATM)
.attr("cy", d=> 1000-(d.SATV)+50)
.attr("r", d=>d.ACT/2.5)
.style("fill", d=>colorQuantize(d.GPA)) // I was doing this line like for 3 hours!!!
.style("stroke", "#bcbcbc");
//X-axis
scatterc.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", d=>d)
.attr("y", 970) //Fix y
.text(d=>d);
//Y-Axis
scatterc.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", 20) //Fix x
.attr("y", d=>1000-d)
.text(d=>d);
</script>
</div>
</div>
<div>
<h2>3. Axis</h2>
<div>
<svg height="1000" width="1000" viewbox="0 0 1000 1000" id="svg3">
</svg>
<script>
var svg3 = d3.select("#svg3")
svg3.append("rect").attr("x", "50").attr("y", "10").attr("width", "900").attr("height", "900").attr("fill", "white").attr("stroke", "black").attr("stroke-width", "1 px");
svg3.append("text").attr("x", "0").attr("y", "435").attr("font-size", "1em").text("SATV");
svg3.append("text").attr("x", "450").attr("y", "950").attr("font-size", "1em").text("SATM");
for(i=0; i< scores.length; i++) {
if (scores[i].GPA >= 3.0) {
var col ='green'
}
else if (scores[i].GPA < 3.0 && scores[i].GPA >= 2.0) {
var col ='yellow'
}
else {
var col ='red'
}
svg3.append("circle").attr("cx", scores[i].SATM).attr("cy", scores[i].SATV).attr("r", scores[i].ACT/2.5).attr("transform", "translate(0,1000), scale(1,-1)").style("fill", col).style("stroke", "black");
}
var height = 1000;
var width = 1000;
var padding = 25;
var svg = d3.select("#svg3").attr("width", width).attr("height", height+2*padding)
var spacing = height / scores.length;
var min = 0, max = 900, yMin = 900, yMax = 0;
var xScale = d3.scaleLinear()
.domain([min, max])
.range([0, width-100])
.nice();
var yScale = d3.scaleLinear()
.domain([yMin, yMax])
.range([0, height-100])
.nice();
/*
var color = d3.scaleLinear()
.domain([min, 0, max])
.range(["darkred", "lightgray", "steelblue"]);*/
var xAxis = d3.axisBottom()
.scale(xScale)
.ticks(width / 80).
tickSizeOuter(0);
var yAxis = d3.axisLeft().scale(yScale).ticks(height / 80).tickSizeOuter(0);
svg.append("g")
.classed("axis", true)
.attr("transform", "translate(" + (padding + 24.5) + "," + (height - 90) + ")")
.call(xAxis);
svg.append("g")
.classed("axis", true)
.attr("transform", "translate(" + (padding + 25) + "," + 9 + ")")
.call(yAxis);
</script>
</div>
</div>
<div>
<h2>4. Transitions</h2>
<div>
<h3>4a - Create 3 buttons to change between the three color schemes for the dots above </h3>
<div id="d4a">
<svg id="DynamicScatters" width="1000" height="1000">
<rect x="50" y="50" width="900" height="900" style="fill:white; stroke:black; stroke-width:1px"/>
<text x="500" y="990">SATM</text>
<text x="30" y="35">SATV</text>
</svg>
<div></div>
<script>
var Basic=d3.select("#DynamicScatters");
Basic.selectAll(null)
.data(scores)
.enter()
.append("circle")
.attr("cx", d=>d.SATM)
.attr("cy", d=> 1000-(d.SATV)+50)
.attr("r", d=>d.ACT/2.5)
.style("stroke", "#bcbcbc");
//X-axis
Basic.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", d=>d)
.attr("y", 970) //Fix y
.text(d=>d);
//Y-Axis
Basic.selectAll(null)
.data(axis)
.enter()
.append("text")
.attr("x", 20) //Fix x
.attr("y", d=>1000-d)
.text(d=>d);
//Buttons
var GreenRedScheme=function(){
Basic.selectAll("circle").transition().duration(400).style("fill", d=>colorLinear(d.GPA));
}
var BlueLinear=function(){
Basic.selectAll("circle").transition().duration(400).style("fill", d=>colorLinear3(d.GPA));
}
var BlueQuantize=function(){
Basic.selectAll("circle").transition().duration(400).style("fill", d=>colorQuantize(d.GPA));
}
var button1=d3.select("#d4a").append("button");
button1.text("First scheme");
button1.on("click", GreenRedScheme);
var button2=d3.select("#d4a").append("button");
button2.text("Second scheme");
button2.on("click", BlueLinear);
var button3=d3.select("#d4a").append("button");
button3.text("Third scheme");
button3.on("click", BlueQuantize);
</script>
</div>
</div>
<div>
<h3>4b - Create a 4th button that changes the horizontal (X) axis to be the cumulative SAT score (SATM+SATV). </h3>
<div id="d4b">
<svg id="DynamicScatters2" width="1000" height="1000">
<rect x="50" y="50" width="900" height="900" style="fill:white; stroke:black; stroke-width:1px"/>
<text x="500" y="990">SATM</text>
<text x="30" y="35">SATV</text>
</svg>
<div></div>
<script>
var ScatterScale=d3.select("#DynamicScatters2");
ScatterScale.selectAll(null)
.data(scores)
.enter()
.append("circle")
.attr("cx", d=>d.SATM)
.attr("cy", d=> 1000-(d.SATV)+50)
.attr("r", d=>d.ACT/2.5)
.style("stroke", "#bcbcbc");
//Axis
ScatterScale.append("g")
.classed("axis", true)
.attr("transform", "translate(" + 50 + "," + 950 + ")")
.attr("id", "xAxis")
.style("font-size", "1rem")
.call(xAxis);
ScatterScale.append("g")
.classed("axis", true)
.attr("transform", "translate("+50+","+50+")")
.attr("id", "yAxis")
.style("font-size", "1rem")
.call(yAxis);
var GreenRedScheme=function(){
ScatterScale.selectAll("circle").transition().duration(400).style("fill", d=>colorLinear(d.GPA));
}
var BlueLinear=function(){
ScatterScale.selectAll("circle").transition().duration(400).style("fill", d=>colorLinear3(d.GPA));
}
var BlueQuantize=function(){
ScatterScale.selectAll("circle").transition().duration(400).style("fill", d=>colorQuantize(d.GPA));
}
var CumulativeSAT=function(){
var minSS=d3.min(scores, d=>(d.SATM+d.SATV));
console.log(minSS);
var maxSS=d3.max(scores, d=>(d.SATM+d.SATV));
console.log(maxSS);
var newXscale=d3.scaleLinear().domain([minSS-30, maxSS]).range([50, 900]);
var newXaxis=d3.axisBottom().scale(newXscale).ticks(width/80).tickSizeOuter(0);
ScatterScale.selectAll("circle")
.transition().duration(400)
.attr("cx", d=>newXscale(d.SATM+d.SATV))
.attr("cy", d=> 1000-d.SATV+50)
.attr("r", d=>d.ACT/2.5)
.style("stroke", "#bcbcbc");
ScatterScale.select("#xAxis")
.classed("axis", true)
.attr("transform", "translate("+50+","+950+")")
.style("font-size", "1rem")
.call(newXaxis);
}
var button1b=d3.select("#d4b").append("button");
button1b.text("First scheme");
button1b.on("click", GreenRedScheme);
var button2b=d3.select("#d4b").append("button");
button2b.text("Second scheme");
button2b.on("click", BlueLinear);
var button3b=d3.select("#d4b").append("button");
button3b.text("Third scheme");
button3b.on("click", BlueQuantize);
var button4=d3.select("#d4b").append("button");
button4.text("New horizontal axis");
button4.on("click", CumulativeSAT);
</script>
</div>
</div>
</div>
</body>
</html>