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
|
// JS module imports
import { useMemo, useState } from "react";
import Graph from 'vis-react';
// CSS imports
import '../css/Canvas.css';
import EdgeInfo from "./EdgeInfo";
/**
* This function renders and mantains the canvas.
* @param {Object} props The props for the canvas.
* @returns {import("react").HtmlHTMLAttributes} The canvas to be retured.
*/
function Visualization(props) {
/*let stocks = {};
let hideEdgeInfo = true;
const setStocks = (newStocks) => {
stocks = newStocks;
}
const setHideEdgeInfo = (val) => {
hideEdgeInfo = val;
}*/
const [key, setKey] = useState(0);
const [graphState, setGraphState] = useState({
nodes: [],
edges: []
});
const options = {
autoResize: true,
edges: {
color: "#ffffff"
}
};
const events = {
selectNode: event => {
props.setSelectedId(event.nodes[0])
}
}
/*const getEdgeInfo = (fromID, toID) => {
fetch("http://localhost:4567/edge-data", {
method: "POST",
body: JSON.stringify({
followerID: fromID,
leaderID: toID,
}),
headers: {
"Content-Type": "application/json",
},
credentials: "same-origin"
})
.then(res => res.json())
.then(data => {
console.log(data);
setStocks(data);
setHideEdgeInfo(false);
})
.catch(err => console.log(err));
}*/
const getNodes = () => {
let nodes = [];
const maxScore = props.data[0].suspicionScore;
const interval = maxScore / 4;
props.data.forEach(hub => {
if (hub.followers) {
let colorVal = '#f6f7d4';
const score = hub.suspicionScore;
if(score > (maxScore - interval)){
colorVal = '#d92027'
}
if(score <= (maxScore - interval) && score > (maxScore - interval*2)){
colorVal = '#f37121'
}
if(score <= (maxScore - interval*2) && score > (maxScore - interval*3)){
colorVal = '#fdca40'
}
nodes.push({
id: hub.id,
autoResize: true,
label: hub.name,
labelHighlightBold: true,
shape: "dot",
value: hub.suspicionScore*1000,
color: {
background: colorVal,
border: '#2b2e4a',
highlight:{
background: '#29bb89',
border: '#fdca40'
}
},
font: {
color: '#9fd8df',
size: 20,
}
});
}
});
return nodes;
}
const getEdges = () => {
let edges = []
props.data.forEach(hub => {
hub.followers.forEach(follower => {
edges.push({
from: follower.id,
to: hub.id,
dashes: false,
color:{
opacity: 0.7,
highlight:'#fdca40',
},
/*chosen: {
edge: () => getEdgeInfo(follower.id, hub.id)
}*/
});
});
});
return edges;
}
/*
* <EdgeInfo
stockList={stocks}
setHideInfo={setHideEdgeInfo}
hidden={hideEdgeInfo}>
</EdgeInfo>*/
// Hooks to update graph state when data changes
useMemo(() => {
setKey(key => key + 1);
setGraphState({nodes: getNodes(), edges: getEdges()})
}, [JSON.stringify(props.data)]);
return (
<div className="Map-canvas">
<Graph
key={key}
graph={graphState}
options={options}
events={events}>
</Graph>
</div>
);
}
export default Visualization;
|