-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindexforce.html
More file actions
333 lines (273 loc) · 8.52 KB
/
indexforce.html
File metadata and controls
333 lines (273 loc) · 8.52 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scale.category20();
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("miserables.json", function(error, graph) {
if (error) throw error;
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); });
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", function(d) { return color(d.group); })
.call(force.drag);
node.append("title")
.text(function(d) { return d.name; });
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
<!-- <!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;
}
svg {
padding: 30px;
}
</style>
</head>
<body>
<button>Click on this text to look at the next hour's distribution of tweets.</button>
<br>
<a href="/clintonvsanders.html">Compare Hillary Clinton to Bernie Sanders.</a>
<a href="trumpvcruz.html">Compare Donald Trump to Ted Cruz.</a>
<br>
<p>Tweets Per Hour by Presidental Candidates</p>
<script type="text/javascript">
//Width and height
var hourtext = "8:00";
var w = 600;
var h = 250;
var hour = 1;
colors = d3.scale.category20()
var dataset =
[{ candidate: "Bernie Sanders", value: 630 },
{ candidate: "Donald Trump", value: 871},
{ candidate: "Hillary Clinton", value: 535 },
{ candidate: "Marco Rubio", value: 263 },
{ candidate: "Ted Cruz", value: 268 }]
;
function getDataSetByHour(hour){
if (hour == 1){
// var datasethour1 =
dataset =
[{ candidate: "Bernie Sanders", value: 630 },
{ candidate: "Donald Trump", value: 871},
{ 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 * 1.5; })])
.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,i){return colors(i)})
//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");
//On click, update with new data
d3.select("button")
.on("click", function() {
//New values for dataset
//if(hour < 6){
if(hour < 5){
hour++;
}
else {
hour = 1;
}
getDataSetByHour(hour);
// dataset =
// var key = function(dataset) {
// return dataset.candidate;
//};
console.log("dataset: " + dataset);
console.log("number: " + hour)
//Update all rects
svg.selectAll("rect")
.data(dataset, key) //Bind data with custom key function
.transition()
.attr("y", function(d) {
return h - yScale(d.value);
})
.attr("height", function(d) {
return yScale(d.value);
})
.attr("fill",function(d,i){return colors(i)})
//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) + (d.value /20);
})
});
// svg.append("text")
// .attr("text-anchor", "middle") // this makes it easy to centre the text as the transform is applied to the anchor
// .attr("transform", "translate("+ (5/2) +","+(h/2)+")rotate(-90)") // text is drawn off the screen top left, move down and out and rotate
// .text("Total # of Tweets");
//
// svg.append("text")
// .attr("text-anchor", "middle") // this makes it easy to centre the text as the transform is applied to the anchor
// .attr("transform", "translate("+ (w/2) +","+(h+(20))+")") // centre below axis
// .text("Presidental ");
//
// svg.append("text")
// .attr("text-anchor", "middle") // this makes it easy to centre the text as the transform is applied to the anchor
// .attr("transform", "translate("+ (w/2) +","+(h-(250))+")") // centre below axis
// .text("Frequency of PresidentalTweets per Hour" );
</script>
</body>
</html> -->