-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
146 lines (119 loc) · 3.88 KB
/
index.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
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
<html>
<head>
<script src="vendor/d3.v3.min.js"></script>
<script src="vendor/cola.v3.min.js"></script>
<script src="vendor/ramda.min.js"></script>
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
fill: none;
stroke: #000;
stroke-width: 1.5px;
opacity: 0.4;
marker-end: url(#end-arrow);
}
.label {
fill: black;
font-family: Verdana;
font-size: 12px;
text-anchor: middle;
}
</style>
</head>
<body>
<div id="graph"></div>
<script>
var width = 960,
height = 500;
var color = d3.scale.category10();
var d3cola = cola.d3adaptor()
.avoidOverlaps(true)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("producer-consumer.json", function (error, net) {
// nodes = transitions + places
const transitions = R.map(p => ({name: p.label, cx: p.x, cy: p.y, group: 'transitions'}), net.transitions)
const places = R.map(p => ({name: p.label, cx:p.x, cy:p.y, group: 'places'}), net.places)
const nodes = R.concat(transitions, places)
// offset place identifiers
const N = R.length(transitions)
const inLink = i => x => ({source: x + N, target: i})
const outLink = i => x => ({source: i, target: x + N})
// transition, list index
const trLinks = R.addIndex(R.map)((tr, i) => R.concat(
R.map(inLink(i), tr.pre || []),
R.map(outLink(i), tr.post || [])
))
const arcs = R.compose(R.unnest, R.unnest, trLinks)(net.transitions)
var graph = {nodes: nodes, links: arcs}
var nodeRadius = 15;
graph.nodes.forEach(function (v) {
v.height = v.width = 2 * nodeRadius;
});
d3cola
.nodes(graph.nodes)
.links(graph.links)
.flowLayout("x", 0)
.symmetricDiffLinkLengths(16)
.start(10,20,20);
// define arrow markers for graph links
svg.append('svg:defs').append('svg:marker')
.attr('id', 'end-arrow')
.attr('viewBox', '0 -5 10 10')
.attr('refX', 6)
.attr('markerWidth', 3)
.attr('markerHeight', 3)
.attr('orient', 'auto')
.append('svg:path')
.attr('d', 'M0,-5L10,0L0,5')
.attr('fill', '#000');
var path = svg.selectAll(".link")
.data(graph.links)
.enter().append('svg:path')
.attr('class', 'link');
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", nodeRadius)
.style("fill", function (d) { return color(d.group); })
.call(d3cola.drag);
var label = svg.selectAll(".label")
.data(graph.nodes)
.enter().append("text")
.attr("class", "label")
.text(function (d) { return d.name; })
.call(d3cola.drag);
d3cola.on("tick", function () {
// draw directed edges with proper padding from node centers
path.attr('d', function (d) {
var deltaX = d.target.x - d.source.x,
deltaY = d.target.y - d.source.y,
dist = Math.sqrt(deltaX * deltaX + deltaY * deltaY),
normX = deltaX / dist,
normY = deltaY / dist,
sourcePadding = nodeRadius,
targetPadding = nodeRadius + 2,
sourceX = d.source.x + (sourcePadding * normX),
sourceY = d.source.y + (sourcePadding * normY),
targetX = d.target.x - (targetPadding * normX),
targetY = d.target.y - (targetPadding * normY);
return 'M' + sourceX + ',' + sourceY + 'L' + targetX + ',' + targetY;
});
node.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
label.attr("x", function (d) { return d.x; })
.attr("y", function (d) {
var h = this.getBBox().height;
return d.y + h/4;
})
});
});
</script>
</body>
</html>