-
Notifications
You must be signed in to change notification settings - Fork 13
/
markoveg.html
158 lines (133 loc) · 3.83 KB
/
markoveg.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
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
#aggress {
fill: #FF3300;
}
.link.aggress {
stroke: #FF3300;
stroke-width: 2.5px;
}
.link.sub {
stroke-dasharray: 0,2 1;
}
circle {
fill: #FFFFCC;
stroke: orange;
stroke-width: 2.0px;
}
text {
font: 10px sans-serif;
pointer-events: none;
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
//
var links = [
{source: "Sniff", target: "Bite", type: "aggress"},
{source: "Bite", target: "Tailrattle", type: "aggress"},
{source: "Active", target: "Subordinate", type: "sub"},
{source: "Bite", target: "Subordinate", type: "sub"},
{source: "Sniff", target: "Subordinate", type: "sub"},
{source: "Subordinate", target: "Active", type: "standard"},
{source: "Sniff", target: "Active", type: "standard"},
{source: "Active", target: "Idle", type: "standard"},
{source: "Idle", target: "Active", type: "standard"},
{source: "Active", target: "Sniff", type: "standard"},
{source: "Active", target: "Rear", type: "standard"},
{source: "Rear", target: "Active", type: "standard"},
{source: "Sniff", target: "Idle", type: "standard"}
];
var nodes = {};
// Compute the distinct nodes from the links.
links.forEach(function(link) {
link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});
var width = 960,
height = 500;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(60)
.charge(-300)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// building arrow
// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
.data(["standard", "sub"])
.enter().append("marker")
.attr("id", function(d) { return d; })
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
// for aggress
svg.append("defs").selectAll("marker")
.data(["aggress"])
.enter().append("marker")
.attr("id", function(d) { return d; })
.attr("viewBox", "0 -5 10 10")
.attr("refX", 12)
.attr("refY", -1.5)
.attr("markerWidth", 5)
.attr("markerHeight", 5)
.attr("orient", "auto")
.append("path")
.attr("d", "M0,-5L10,0L0,5");
var path = svg.append("g").selectAll("path")
.data(force.links())
.enter().append("path")
.attr("class", function(d) { return "link " + d.type; })
.attr("marker-end", function(d) { return "url(#" + d.type + ")"; });
// define nodes
var circle = svg.append("g").selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.attr("r", 6)
.call(force.drag);
var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter().append("text")
.attr("x", 8)
.attr("y", ".31em")
.text(function(d) { return d.name; });
// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
path.attr("d", linkArc);
circle.attr("transform", transform);
text.attr("transform", transform);
}
function linkArc(d) {
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
}
function transform(d) {
return "translate(" + d.x + "," + d.y + ")";
}
//
svg.append("text")
.attr("x", 475)
.attr("y", 125)
.attr("text-anchor", "middle")
.text("Example d3 Markov Chain")
</script>