-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstranged3behavior.html
62 lines (50 loc) · 1.35 KB
/
stranged3behavior.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
// d3 v 5.2.0 loaded
// Beware of the following trap, due to javascript copying the pointer,
// not the data of an an object when using the '=' sign.
var space;
var csv1 =
`id,idperson,nameplace
A,1,NameA
B,1,NameB
C,1,NameC`
var csv2 =
`source,target,idperson,v1
"A","B",1,540
"B","C",1,1200`
var parsedcsv1 = d3.csvParse(csv1);
var parsedcsv2 = d3.csvParse(csv2);
space = {
'places' : parsedcsv1,
'movements' : parsedcsv2
};
console.log("space",space);
run();
function run() {
var simulation = d3.forceSimulation()
.force('link',d3.forceLink()
.id(function(d) { return d.id; })
.distance(function(d) { return d.time; })
)
;
// Beware, uncommenting the lines below breaks space['movements'] when applying simulation force to links. This is standard javascript behavior
// var my_nodes = space['places'];
// var my_links = space['movements'];
// simulation.nodes(my_nodes);
// simulation.force('link').links(my_links);
// Workaround: copy the object's data instead of the pointer :
var my_nodes = JSON.parse(JSON.stringify(space['places'])) ;
var my_links = JSON.parse(JSON.stringify(space['movements']));
simulation.nodes(my_nodes);
simulation.force('link').links(my_links);
}
</script>
</body>
</html>