-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
trumpvcruzold.html
485 lines (416 loc) · 12.3 KB
/
trumpvcruzold.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Removing values from a chart</title>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<style type="text/css">
/* No style rules here yet */
body {
background-color: #E4E4E4;
}
p {
color: #000000;
font-size: 1.5em;
}
</style>
</head>
<body>
<p>Click on this text to look at the next hour's tweets.</p>
<!-- <a href="/clintonvsanders.html">Compare Hillary Clinton to Bernie Sanders.</a>-->
<a href="trumpvcruz.html">Compare Donald Trump to Ted Cruz.</a>
<a href="index.html">Compare All Candidates.</a>
<br>
<!-- <p id="hour">"These tweets are from" + hourtext</p>
<p id="hour-1">These tweets are from Sun Feb 07 7:18 - 7:59PM</p>
<p id="hour-2">These tweets are from Sun Feb 07 8:00 - 8:59PM</p>
<p id="hour-3">These tweets are from Sun Feb 07 9:00 - 9:59PM</p>
<p id="hour-4">These tweets are from Sun Feb 07 10:00 - 10:59PM</p>
<p id="hour-5">These tweets are from Sun Feb 07 11:00 - 11:59PM</p>
<p id="hour-6">These tweets are from Mon Feb 08 at 12:00AM</p>
-->
<!-- <script type="text/javascript">
//d3.select("p")
$("p").on("click", function(){
var spans = $('p[id^="hour-"]').hide(),
i = 0;
(function cycle() {
spans.eq(i).fadeIn(600)
.delay(1800)*/
.delay(2500)
.fadeOut(600, cycle);
i = ++i % spans.length;
})() )};
</script> -->
<script type="text/javascript">
//Width and height
var hourtext = "8:00";
var w = 600;
var h = 250;
var hour = 1;
var dataset =
[
{ candidate: "Donald Trump", value: 871},
{ candidate: "Ted Cruz", value: 268 }
];
//
// var dataset =
// [{ candidate: "Bernie Sanders", value: 3146 },
// { candidate: "Donald Trump", value: 4912 },
// { candidate: "Hillary Clinton", value: 2891 },
// { candidate: "Marco Rubio", value: 1281 },
// { candidate: "Ted Cruz", value: 1494 }]
// ;
// var dataset =
// [{ candidate: "Bernie Sanders", value: 229 },
// { candidate: "Donald Trump", value: 164 },
// { candidate: "Hillary Clinton", value: 150 },
// { candidate: "Marco Rubio", value:27 },
// { candidate: "Ted Cruz", value:34 }]
// ;
function getDataSetByHour(hour){
if (hour == 1){
// var datasethour1 =
dataset =
[{ candidate: "Bernie Sanders", value: 630 },
{ candidate: "Hillary Clinton", value: 535 },
{ candidate: "Marco Rubio", value: 263 },
{ candidate: "Ted Cruz", value: 268 }]
;
}
else if (hour == 2){
//var datasethour2 =
dataset =
[//{ candidate: "Bernie Sanders", value: 364 },
{ candidate: "Donald Trump", value: 597},
//{ candidate: "Hillary Clinton", value: 464},
//{ candidate: "Marco Rubio", value: 161 },
{ candidate: "Ted Cruz", value: 178}]
;
}
else if (hour == 3){
// var datasethour3 =
dataset =
[//{ candidate: "Bernie Sanders", value: 861 },
{ candidate: "Donald Trump", value: 1234},
//{ candidate: "Hillary Clinton", value: 688},
//{ candidate: "Marco Rubio", value: 290},
{ candidate: "Ted Cruz", value: 394}]
;
}
else if (hour == 4){
//var datasethour4 =
dataset =
[//{ candidate: "Bernie Sanders", value: 636 },
{ candidate: "Donald Trump", value: 1198},
//{ candidate: "Hillary Clinton", value: 619},
//{ candidate: "Marco Rubio", value: 345},
{ candidate: "Ted Cruz", value: 356}]
;
}
else if (hour == 5 ){
// var datasethour5 =
dataset =
[//{ candidate: "Bernie Sanders", value: 652 },
{ candidate: "Donald Trump", value: 1004},
//{ candidate: "Hillary Clinton", value: 582},
//{ candidate: "Marco Rubio", value: 219 },
{ candidate: "Ted Cruz", value: 297}]
;
}
else if (hour == 6){
//var datasethour6 =
dataset =
[//{ candidate: "Bernie Sanders", value: 3},
{ candidate: "Donald Trump", value: 8},
//{ candidate: "Hillary Clinton", value: 3},
//{ candidate: "Marco Rubio", value: 3},
{ candidate: "Ted Cruz", value: 2}]
;
}
return dataset;
}
var xScale = d3.scale.ordinal()
.domain(d3.range(dataset.length))
.rangeRoundBands([0, w], 0.05);
var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d.value; })])
.range([0, h]);
//Define key function, to be used when binding data
var key = function(d) {
return d.candidate;
};
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create bars
svg.selectAll("rect")
.data(dataset, key) //Bind data with custom key function
.enter()
.append("rect")
.attr("x", function(d, i) {
return xScale(i);
})
.attr("y", function(d) {
return h - yScale(d.value);
})
.attr("width", xScale.rangeBand())
.attr("height", function(d) {
return yScale(d.value);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d.value * 10) + ")";
});
//Create labels
svg.selectAll("text")
.data(dataset, key) //Bind data with custom key function
.enter()
.append("text")
.text(function(d) {
return d.value + " " + d.candidate;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return xScale(i) + xScale.rangeBand() / 2;
})
.attr("y", function(d) {
return h - yScale(d.value) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");
// svg.append('text')
// .attr('x', width / 2)
// .attr('y', 20)
// .attr('text-anchor', 'middle')
// .attr('class', 'chart-title')
// .text('Sample Charts');
//On click, update with new data
d3.select("p")
.on("click", function() {
//d3.select("body").replace("p").text("These tweets are from" + hourtext);
//New values for dataset
//if(hour < 6){
if(hour < 5){
hour++;
}
else {
hour = 1;
}
getDataSetByHour(hour);
console.log("dataset: " + dataset);
console.log("number: " + hour)
// dataset =
// [{ candidate: "Bernie Sanders", value: 229 },
// { candidate: "Donald Trump", value: 164 },
// { candidate: "Hillary Clinton", value: 150 },
// { candidate: "Marco Rubio", value:27 },
// { candidate: "Ted Cruz", value:34 }]
// ;
//Update all rects
svg.selectAll("rect")
.data(dataset, key) //Bind data with custom key function
.transition()
// .enter()
// .append("rect")
//.attr("x", function(d, i) {
// return xScale(i);
// })
.attr("y", function(d) {
return h - yScale(d.value);
})
// .attr("width", xScale.rangeBand())
.attr("height", function(d) {
return yScale(d.value);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d.value * 10) + ")";
});
//Update all labels
svg.selectAll("text")
.data(dataset, key) //Bind data with custom key function
// .enter()
// .append("text")
.text(function(d) {
return d.value + " " + d.candidate;
})
// .attr("text-anchor", "middle")
.attr("x", function(d, i) {
return xScale(i) + xScale.rangeBand() / 2;
})
.attr("y", function(d) {
// return h - yScale(d.value) + 14;
return h - yScale(d.value) - 10;
})
// .attr("font-family", "sans-serif")
// .attr("font-size", "11px")
.attr("fill", "black");
});
// //On click, update with new data
// d3.select("p")
// .on("click", function() {
//
// var dataset =
// [{ candidate: "Bernie Sanders", value: 229 },
// { candidate: "Donald Trump", value: 164 },
// { candidate: "Hillary Clinton", value: 150 },
// { candidate: "Marco Rubio", value:27 },
// { candidate: "Ted Cruz", value:34 }]
// ;
// //Remove one value from dataset
// dataset.shift();
//
// //Update scale domains
// xScale.domain(d3.range(dataset.length));
// yScale.domain([0, d3.max(dataset, function(d) { return d.value; })]);
// //Select…
// var bars = svg.selectAll("rect")
// .data(dataset, candidate); //Bind data with custom key function
//
// //Enter…
// bars.enter()
// .append("rect")
// .attr("x", w)
// .attr("y", function(d) {
// return h - yScale(d.value);
// })
// .attr("width", xScale.rangeBand())
// .attr("height", function(d) {
// return yScale(d.value);
// })
// .attr("fill", function(d) {
// return "rgb(0, 0, " + (d.value * 10) + ")";
// });
//
// //Update…
// bars.transition()
// .duration(500)
// .attr("x", function(d, i) {
// return xScale(i);
// })
// .attr("y", function(d) {
// return h - yScale(d.value);
// })
// .attr("width", xScale.rangeBand())
// .attr("height", function(d) {
// return yScale(d.value);
// });
// //Exit…
// bars.exit()
// .transition()
// .duration(500)
// .attr("x", -xScale.rangeBand()) // <-- Exit stage left
// .remove();
// //Update all labels
// //
// //Exercise: Modify this code to remove the correct label each time!
// //
// svg.selectAll("text")
// .data(dataset, candidate) //Bind data with custom key function
// .transition()
// .duration(500)
// .text(function(d) {
// return d.value;
// })
// .attr("x", function(d, i) {
// return xScale(i) + xScale.rangeBand() / 2;
// })
// .attr("y", function(d) {
// return h - yScale(d.value) + 14;
// });
// });
</script>
</body>
</html>
<!--
<!DOCTYPE html>
<title>Hello this is a test </title>
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
}
div.bar {
display: inline-block;
width: 20px;
height: 75px; /* Gets overriden by D3-assigned height below */
margin-right: 2px;
background-color: teal;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
// var dataset =
// [{ candidate: "Bernie Sanders", value: 229 },
// { candidate: "Donald Trump", value: 164 },
// { candidate: "Hillary Clinton", value: 150 },
// { candidate: "Marco Rubio", value:27 },
// { candidate: "Ted Cruz", value:34 }]
// ;
var dataset = [ 5, 10, 15, 20, 25 ];
//var clinton = 5;
//var clint = 'Clinton';
d3.select("body").append("p").text("My First D3 Visualization!");
//var dataset;
//d3.json("tweets.02.07.2016.summary.json", function(json) {
// dataset = dataset + json;
// console.log(json); //Log output to console
//});
//var dataset = [ 5, 10, 15, 20, 25 ];
//d3.json("tweets.02.07.2016.summary.json", function(err, json) {
//d3.json("tweetsd3.json", function(err, json) {
d3.select("body").selectAll('div')
.on('click', function(){
// var dataset = [ 5, 10, 15, 20, 25 ];
svg.selectAll('rect')
.data(dataset)
.attr("y", function(d){
return h * yScale(d);
})
.attr("height", function(d){
return yScale(d);
})
})
// d3.json(totalCounts, function(err, json) {
//
//
// var dataset = json;
// alert("error: " + err);
// alert("hello");
// // alert(err);
// console.log(json); //Log output to console
// /* clintonSearch(dataset);*/
//
// for (clint in dataset){
// clinton++;
// }
// bar();
//
// return clinton;
// });
//d3.select("body").append("p").text("Clinton " + clinton);
/*
function clintonSearch(data) {
clinton = 5;
if (data.indexOf("clinton") > -1){
clinton++;
}
console.log(clinton);
return clinton;
}*/
//d3.select("body").append("p").text("Clinton " + clinton);
function bar(){ d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
.attr("class", "bar")
.style("height", function(d) {
var barHeight = d * 5;
return barHeight + "px";
});
}
</script> -->