-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
268 lines (219 loc) · 6.8 KB
/
graph.js
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
$(document).ready(function() {
// Do stuff when the page is loaded
stockDatasets();
stockTweets();
})
var tweets_trump2018 = null;
var tweets_china = null;
var tweets_badsad = null;
var points_trump2018 = null;
var points_china = null;
var points_badsad = null;
var bool_2018 = true;
var bool_china = true;
var bool_badsad = true;
var btn_china = null;
var btn_badsad = null;
var color_china = '#f4511e';
var color_badsad = '#f4511e';
var graph = null;
window.onload=function(){
btn_china = document.getElementById("btn_china");
btn_china.addEventListener("mouseover", function() { changeColor('blue', 1); });
btn_china.addEventListener("mouseout", function() { changeColor('#f4511e', 1); });
btn_badsad = document.getElementById("btn_badsad");
btn_badsad.addEventListener("mouseover", function() { changeColor('blue', 2); });
btn_badsad.addEventListener("mouseout", function() { changeColor('#f4511e', 2); });
}
function changeColor(color, btn_index){
switch(btn_index) {
case 0:
break;
case 1:
color_china = color;
break;
case 2:
color_badsad = color;
break;
default:
}
drawVisualization();
console.log("color changed");
}
function onclick(point) {
console.log(point);
//window.alert("The point " + point.id + " at coordinates (" + point.x.toFixed(3) + ", " + point.y.toFixed(3) + ", " + point.z.toFixed(3) + ") has text: " + point.text)
window.alert("Tweet:\n" + point.text + "\n _____ \nSentiment prediction: \nNegative - " + point.x.toFixed(3) + "\nNeutral -" + point.z.toFixed(3) + "\nPositive - " + point.y.toFixed(3))
}
// Called when the Visualization API is loaded.
function drawVisualization() {
// create the data table.
data_d = new vis.DataSet();
var id_counter = 0
// create the animation data
if(bool_2018){
for (var i = 0; i < points_trump2018.length; i++) {
var style = '#f4511e'; // Color
id_counter = id_counter+1;
data_d.add({id: id_counter, text: tweets_trump2018[i], x: parseFloat(points_trump2018[i][0]), y: parseFloat(points_trump2018[i][2]), z: parseFloat(points_trump2018[i][1]), style: style});
//data_d.add({x: x, y: y, z: z, style: style});
}
}
// create the animation data
if(bool_china){
for (var i = 0; i < points_china.length; i++) {
var style = color_china; // Color
id_counter = id_counter+1;
data_d.add({id: id_counter, text: tweets_china[i], x: parseFloat(points_china[i][0]), y: parseFloat(points_china[i][2]), z: parseFloat(points_china[i][1]), style: style});
//data_d.add({x: x, y: y, z: z, style: style});
}
}
// create the animation data
if(bool_badsad){
for (var i = 0; i < points_badsad.length; i++) {
var style = color_badsad; // Color
id_counter = id_counter+1;
data_d.add({id: id_counter, text: tweets_badsad[i], x: parseFloat(points_badsad[i][0]), y: parseFloat(points_badsad[i][2]), z: parseFloat(points_badsad[i][1]), style: style});
//data_d.add({x: x, y: y, z: z, style: style});
}
}
//data.shift();
//data.pop();
// specify options
var options = {
width: '100%',
height: '100vh',
style: 'dot-color',
showPerspective: true,
showGrid: true,
keepAspectRatio: true,
verticalRatio: 1.0,
showLegend: false,
onclick: onclick,
xLabel: 'negative',
yLabel: 'positive',
zLabel: 'neutral',
dotSizeRatio: 0.01,
cameraPosition: {
horizontal: -3.14*3/4,
vertical: 0.785,
distance: 3
}
};
// create our graph
var container = document.getElementById('mygraph');
graph = new vis.Graph3d(container, data_d, options);
}
// Read points from csv.
function readPoints_2018(string_csv) {
points_trump2018 = parseCSV(string_csv)
points_trump2018.shift();
points_trump2018.pop();
//console.log(points_trump2018)
}
function readPoints_china(string_csv) {
points_china = parseCSV(string_csv)
points_china.shift();
points_china.pop();
//console.log(points_china)
}
function readPoints_badsad(string_csv) {
points_badsad = parseCSV(string_csv)
points_badsad.shift();
points_badsad.pop();
console.log("Points badsad");
console.log(points_badsad);
}
function parseCSV(csv) {
var rows = csv.split("\n");
return rows.map(function (row) {
return row.split(",");
});
};
function readCSV_points_2018(url) {
return $.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(response) {
readPoints_2018(response);
}
});
}
function readCSV_points_china(url) {
return $.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(response) {
readPoints_china(response);
}
});
}
function readCSV_points_badsad(url) {
return $.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(response) {
readPoints_badsad(response);
}
});
}
function readCSV_tweets_2018(url) {
return $.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(response) {
read_tweets_2018(response);
}
});
}
function readCSV_tweets_china(url) {
return $.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(response) {
read_tweets_china(response);
}
});
}
function readCSV_tweets_badsad(url) {
return $.ajax({
type: "GET",
url: url,
dataType: "text",
success: function(response) {
read_tweets_badsad(response);
}
});
}
function read_tweets_2018(string_csv) {
tweets_trump2018 = parseCSV(string_csv)
tweets_trump2018.shift();
tweets_trump2018.pop();
//console.log(tweets_trump2018);
}
function read_tweets_china(string_csv) {
tweets_china = parseCSV(string_csv)
tweets_china.shift();
tweets_china.pop();
}
function read_tweets_badsad(string_csv) {
tweets_badsad = parseCSV(string_csv)
tweets_badsad.shift();
tweets_badsad.pop();
}
function stockDatasets() {
readCSV_points_2018("https://ufity.com/uploads/points_trump_200.csv");
readCSV_points_china("https://ufity.com/uploads/pred_china.csv");
readCSV_points_badsad("https://ufity.com/uploads/pred_badsad.csv");
setTimeout(drawVisualization, 600);
}
function stockTweets() {
readCSV_tweets_2018("https://ufity.com/uploads/tweets_trump2018.csv");
readCSV_tweets_china("https://ufity.com/uploads/tweets_china.csv");
readCSV_tweets_badsad("https://ufity.com/uploads/tweets_badsad.csv");
}