-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
336 lines (268 loc) · 10.8 KB
/
index.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
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
function create_bar_plot( d3, area, date, pvalue, indices) {
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// deminsions
var margin = {top: 400, right: 30, bottom: 70, left: 400},
width = 1500 - margin.left - margin.right,
height = 1000 - margin.top - margin.bottom;
// set up svg
var svg = d3.select(area)
.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 file = "./r_data/" + date + "_" + pvalue + ".csv"
var types = ["positive", "negative"]
var color = d3.scaleOrdinal(types, ["green", "red"])
var market_cap_file = "./market_caps/market_caps.csv";
var market_cap_data;
// load market cap
d3.csv( market_cap_file ).then ( market => {
market_cap_data = market
}).then( () => {
// load the datat from q3.csv
return d3.csv(file)
})
.then( info => {
var data = format_data(info, indices);
let links = data['data'].links;
let nodes = data['data'].nodes;
//console.log("link")
//console.log(JSON.stringify(links))
var tone_min = data['tone_min']
var tone_max = data['tone_max']
//console.log("here is min_tone: " + tone_min)
//console.log("here is max_tone: " + tone_max)
all = add_market_cap( nodes, market_cap_data);
nodes = all["nodes"]
min_size = all['min']
max_size = all['max']
//console.log("here is min_size: " + min_size)
//console.log("here is max_size: " + max_size)
//console.log("here is nodes: " + JSON.stringify(nodes))
//console.log("nodes")
//console.log(JSON.stringify(nodes))
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id( d => {
//console.log("hey");
return d.id
}))
// add more spacing in line below
.force("charge", d3.forceManyBody().strength(-800))
.force("x", d3.forceX())
.force("y", d3.forceY());
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(types)
.join("marker")
.attr("id", d => {
//console.log("here is d: " + JSON.stringify(d));
return `arrow-${d}`
})
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -0.5)
.attr("markerWidth", d => {
//console.log('here is marker width: ' + JSON.stringify(d))
return 6
})
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("fill", color)
.attr("d", "M0,-5L10,0L0,5");
const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-width", 1.5)
.selectAll("path")
.data(links)
.join("path")
.attr("stroke", d => color(d.type))
.attr("stroke-width", d => {
// this effects line thickness
//console.log("here is stroke width: " + JSON.stringify(d.width))
return d.width
})
.attr("marker-end", d => {
//console.log("here is d: " + JSON.stringify(d));
// not give arrow to everything
if (d.source.id != d.target.id)
return `url(${new URL(`#arrow-${d.type}`, location)})`
});
const node = svg.append("g")
.attr("fill", "currentColor")
.attr("stroke-linecap", "round")
.attr("stroke-linejoin", "round")
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation))
// .on("mouseover", d => {
// console.log("you are hovering")
// console.log("d = " + JSON.stringify(d));
// need to add something with tooltip
// })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html("Market cap: " + d.size + " billions")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
})
node.append("circle")
.attr("stroke", "white")
.attr("stroke-width", 1.5)
.attr("r", d => {
return Math.log2(d.size);
})
.style("fill", d => {
//console.log("here is d: " + JSON.stringify(d))
// look here this needs to be done better more than likley
var tone = Math.log2(Math.abs(d.tone))
// took middle point of 50 (1 -100)
// was 100 but gave grey while colors with log being smaller number
tone = "" + (50 - tone) + "%";
if (d.color =="green") {
//see http://www.workwithcolor.com/hsl-color-picker-01.html
// to understand hsl
return "hsl(120, 100%," + tone +" )"
} else {
return "hsl(0, 100%," + tone +" )"
}
})
// look transparent
.style("opacity", 0.6);
node.append("text")
.attr("x", 8)
.attr("y", "0.31em")
.text(d => d.id)
.clone(true).lower()
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-width", 3);
simulation.on("tick", () => {
link.attr("d", linkArc);
node.attr("transform", d => `translate(${d.x},${d.y})`);
});
//invalidation.then(() => simulation.stop());
function linkArc(d) {
const r = Math.hypot(d.target.x - d.source.x, d.target.y - d.source.y);
return `
M${d.source.x},${d.source.y}
A${r},${r} 0 0,1 ${d.target.x},${d.target.y}`;
}
function drag(simulation) {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
// setting the fx values will "pin" the nodes to this position
d.fx = d.x;
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
//commeted out so notes wont reloacte
//d.fx = null;
//d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
//return svg.node();
}).catch( err => {
console.log("error = " + JSON.stringify(err))
console.log(err);
})
}
function add_market_cap(nodes, market_cap_data) {
var min = null
var max = null
//console.log("here is market cap data: " + JSON.stringify( market_cap_data));
//console.log("here is nodes: " + JSON.stringify(nodes));
for ( var x in market_cap_data) {
//console.log("here is maket[x]: " + JSON.stringify(market_cap_data[x])
for (var y in nodes) {
if ( market_cap_data[x][''] === nodes[y]['id'] ) {
//console.log("we have a match of: " + nodes[y]['id'])
nodes[y]['size'] = parseInt(market_cap_data[x]['Market Cap in Billion'])
if (min === null && max === null) {
min = nodes[y]['size']
max = nodes[y]['size']
} else {
if ( nodes[y]['size'] < min) {
min = nodes[y]['size']
}
if ( nodes[y]['size'] > max) {
max = nodes[y]['size']
}
}
}
}
}
//console.log("here is nodes: " + JSON.stringify(nodes));
return {'nodes': nodes, 'min': min, "max": max}
}
function format_data( info, indices) {
//console.log("here is info: " + JSON.stringify(info))
//var info = [{"":"AEX","AEX":"9.3","ATX":"0","BEL_20":"0","Bovespa":"0","BSE_Sensex":"0","CAC40":"0","CSE":"0","DAX_PERFORMANCE.INDEX":"0","Dow_30":"0","EURONEXT_100":"0","HANG_SENG_INDEX":"0","IBEX_35":"0","IDX_Composite":"0","KOSPI_Composite_Index":"0","MOEX_Russia_Index":"0","Nasdaq":"0","Nifty_50":"0","IPC_MEXICO":"0","MERVAL":"0","Nikkei_225":"0","Russell_2000":"0","SP_500":"0","SP_NZX_50_INDEX_GROSS":"0","SP_TSX_Composite_index":"0","SMI":"0"}]
var tone_min = null
var tone_max = null
var data = { nodes: [], links: []}
for( let x in info) {
let hold;
if (indices.includes(info[x][""])) {
for( let y in info[x]) {
let index = y
let value = parseFloat(info[x][y])
if ( index == '') {
hold = {'id': info[x]['']}
} else if (value != 0 && indices.includes(y)){
let color;
if( value > 0) color = 'green'
else color = "red"
hold['color'] = color
hold['tone'] = value
if (tone_min === null && tone_max === null) {
tone_min = value
tone_max = value
} else {
if (tone_min > value){
tone_min = value
}
if (tone_max < value){
tone_max = value
}
}
let type;
if(value > 0) type = 'positive'
else type = "negative"
data.links.push({
'source': info[x][''],
'target': index,
'width': Math.abs(value),
'type': type
});
}
}
data.nodes.push( hold);
} else {
//console.log("this is not included " + info[x][''])
}
}
return {'data': data, 'tone_min': tone_min, 'tone_max': tone_max};
}