public
Last active

Hierarchical edge bundling with dynamically generated arcs for each group

  • Download Gist
bundle.html
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 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
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<link type="text/css" rel="stylesheet" href="Styles/style.css"/>
<style type="text/css">
 
path.arc {
cursor: move;
fill: #fff;
}
 
.node {
font-size: 10px;
}
 
.node:hover {
fill: #1f77b4;
}
 
.link {
fill: none;
stroke: #1f77b4;
stroke-opacity: .4;
pointer-events: none;
}
 
.link.source, .link.target {
stroke-opacity: 1;
stroke-width: 2px;
}
 
.node.target {
fill: #d62728 !important;
}
 
.link.source {
stroke: #d62728;
}
 
.node.source {
fill: #2ca02c;
}
 
.link.target {
stroke: #2ca02c;
}
 
</style>
</head>
<body>
<h2>
Flare imports<br>
hierarchical edge bundling
</h2>
<div id="bundle"></div>
<div style="position:absolute;bottom:0;font-size:18px;">tension: <input style="position:relative;top:3px;" type="range" min="0" max="100" value="85"></div>
<script type="text/javascript" src="http://d3js.org/d3.v2.js"></script>
<script type="text/javascript" src="Scripts/packages.js"></script>
<script type="text/javascript" src="Scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">
 
var w = 740,
h = 700,
rx = w / 2,
ry = h / 2,
m0,
rotate = 0
pi = Math.PI;
 
var splines = [];
 
var cluster = d3.layout.cluster()
.size([360, ry - 180])
.sort(function(a, b) { return d3.ascending(a.key, b.key); });
 
var bundle = d3.layout.bundle();
 
var line = d3.svg.line.radial()
.interpolate("bundle")
.tension(.85)
.radius(function(d) { return d.y; })
.angle(function(d) { return d.x / 180 * Math.PI; });
 
// Chrome 15 bug: <http://code.google.com/p/chromium/issues/detail?id=98951>
var div = d3.select("#bundle")
.style("width", w + "px")
.style("height", w + "px")
.style("position", "absolute");
 
var svg = div.append("svg:svg")
.attr("width", w)
.attr("height", w)
.append("svg:g")
.attr("transform", "translate(" + rx + "," + ry + ")");
 
svg.append("svg:path")
.attr("class", "arc")
.attr("d", d3.svg.arc().outerRadius(ry - 180).innerRadius(0).startAngle(0).endAngle(2 * Math.PI))
.on("mousedown", mousedown);
 
d3.json("flare-imports.json", function(classes) {
var nodes = cluster.nodes(packages.root(classes)),
links = packages.imports(nodes),
splines = bundle(links);
 
var path = svg.selectAll("path.link")
.data(links)
.enter().append("svg:path")
.attr("class", function(d) { return "link source-" + d.source.key + " target-" + d.target.key; })
.attr("d", function(d, i) { return line(splines[i]); });
 
var groupData = svg.selectAll("g.group")
.data(nodes.filter(function(d) { return (d.key=='Jobs' || d.key == 'Freelance' || d.key == 'Bayard') && d.children; }))
.enter().append("group")
.attr("class", "group");
var groupArc = d3.svg.arc()
.innerRadius(ry - 177)
.outerRadius(ry - 157)
.startAngle(function(d) { return (findStartAngle(d.__data__.children)-2) * pi / 180;})
.endAngle(function(d) { return (findEndAngle(d.__data__.children)+2) * pi / 180});
svg.selectAll("g.arc")
.data(groupData[0])
.enter().append("svg:path")
.attr("d", groupArc)
.attr("class", "groupArc")
.style("fill", "#1f77b4")
.style("fill-opacity", 0.5);
 
svg.selectAll("g.node")
.data(nodes.filter(function(n) { return !n.children; }))
.enter().append("svg:g")
.attr("class", "node")
.attr("id", function(d) { return "node-" + d.key; })
.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")"; })
.append("svg:text")
.attr("dx", function(d) { return d.x < 180 ? 25 : -25; })
.attr("dy", ".31em")
.attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
.attr("transform", function(d) { return d.x < 180 ? null : "rotate(180)"; })
.text(function(d) { return d.key.replace(/_/g, ' '); })
.on("mouseover", mouseover)
.on("mouseout", mouseout);
 
d3.select("input[type=range]").on("change", function() {
line.tension(this.value / 100);
path.attr("d", function(d, i) { return line(splines[i]); });
});
});
 
d3.select(window)
.on("mousemove", mousemove)
.on("mouseup", mouseup);
 
function mouse(e) {
return [e.pageX - rx, e.pageY - ry];
}
 
function mousedown() {
m0 = mouse(d3.event);
d3.event.preventDefault();
}
 
function mousemove() {
if (m0) {
var m1 = mouse(d3.event),
dm = Math.atan2(cross(m0, m1), dot(m0, m1)) * 180 / Math.PI;
div.style("-webkit-transform", "translate3d(0," + (ry - rx) + "px,0)rotate3d(0,0,0," + dm + "deg)translate3d(0," + (rx - ry) + "px,0)");
}
}
 
function mouseup() {
if (m0) {
var m1 = mouse(d3.event),
dm = Math.atan2(cross(m0, m1), dot(m0, m1)) * 180 / Math.PI;
 
rotate += dm;
if (rotate > 360) rotate -= 360;
else if (rotate < 0) rotate += 360;
m0 = null;
 
div.style("-webkit-transform", "rotate3d(0,0,0,0deg)");
 
svg.attr("transform", "translate(" + rx + "," + ry + ")rotate(" + rotate + ")")
.selectAll("g.node text")
.attr("dx", function(d) { return (d.x + rotate) % 360 < 180 ? 25 : -25; })
.attr("text-anchor", function(d) { return (d.x + rotate) % 360 < 180 ? "start" : "end"; })
.attr("transform", function(d) { return (d.x + rotate) % 360 < 180 ? null : "rotate(180)"; });
}
}
 
function mouseover(d) {
svg.selectAll("path.link.target-" + d.key)
.classed("target", true)
.each(updateNodes("source", true));
 
svg.selectAll("path.link.source-" + d.key)
.classed("source", true)
.each(updateNodes("target", true));
}
 
function mouseout(d) {
svg.selectAll("path.link.source-" + d.key)
.classed("source", false)
.each(updateNodes("target", false));
 
svg.selectAll("path.link.target-" + d.key)
.classed("target", false)
.each(updateNodes("source", false));
}
 
function updateNodes(name, value) {
return function(d) {
if (value) this.parentNode.appendChild(this);
svg.select("#node-" + d[name].key).classed(name, value);
};
}
 
function cross(a, b) {
return a[0] * b[1] - a[1] * b[0];
}
 
function dot(a, b) {
return a[0] * b[0] + a[1] * b[1];
}
 
function findStartAngle(children) {
var min = children[0].x;
children.forEach(function(d) {
if (d.x < min)
min = d.x;
});
return min;
}
 
function findEndAngle(children) {
var max = children[0].x;
children.forEach(function(d) {
if (d.x > max)
max = d.x;
});
return max;
}
</script>
</body>
</html>
flare-imports.json
JSON
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
[
{
"name":"root.Jobs.Freelance",
"imports":
[
"root.Freelance.Montrose_High_School",
"root.Freelance.Technology_in_Education",
"root.Freelance.Fandango",
"root.Freelance.ICPA",
"root.Freelance.Showtime",
"root.Freelance._VVF",
"root.Freelance.Team_Tactical"
]
},
{
"name":"root.Jobs.Bayard",
"imports":
[
"root.Bayard.ADP",
"root.Bayard.Centene",
"root.Bayard.University_of_Kentucky",
"root.Bayard.Hertz",
"root.Bayard.Six_Flags",
"root.Bayard.Kaiser_Permanente",
"root.Bayard.Ball_Aerospace"
]
},
{"name":"root.Freelance.Montrose_High_School","imports":[]},
{"name":"root.Freelance.Technology_in_Education","imports":[]},
{"name":"root.Freelance.Fandango","imports":[]},
{"name":"root.Freelance.ICPA","imports":[]},
{"name":"root.Freelance.Showtime","imports":[]},
{"name":"root.Freelance._VVF","imports":[]},
{"name":"root.Freelance.Team_Tactical","imports":[]},
{"name":"root.Bayard.ADP","imports":[]},
{"name":"root.Bayard.Centene","imports":[]},
{"name":"root.Bayard.University_of_Kentucky", "imports":[]},
{"name":"root.Bayard.Hertz","imports":[]},
{"name":"root.Bayard.Six_Flags", "imports":[]},
{"name":"root.Bayard.Kaiser_Permanente", "imports":[]},
{"name":"root.Bayard.Ball_Aerospace", "imports":[]}
]
packages.js
JavaScript
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
(function() {
packages = {
 
// Lazily construct the package hierarchy from class names.
root: function(classes) {
var map = {};
 
function find(name, data) {
var node = map[name], i;
if (!node) {
node = map[name] = data || {name: name, children: []};
if (name.length) {
node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
node.parent.children.push(node);
node.key = name.substring(i + 1);
}
}
return node;
}
 
classes.forEach(function(d) {
find(d.name, d);
});
 
return map[""];
},
 
// Return a list of imports for the given array of nodes.
imports: function(nodes) {
var map = {},
imports = [];
 
// Compute a map from name to node.
nodes.forEach(function(d) {
map[d.name] = d;
});
 
// For each import, construct a link from the source to target node.
nodes.forEach(function(d) {
if (d.imports) d.imports.forEach(function(i) {
imports.push({source: map[d.name], target: map[i]});
});
});
 
return imports;
}
 
};
})();
style.css
CSS
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
body {
font: 300 36px "Helvetica Neue";
height: 640px;
/*margin: 80px 160px 80px 160px;
overflow: hidden;*/
position: relative;
}
 
a:link, a:visited {
color: #777;
text-decoration: none;
}
 
a:hover {
color: #666;
}
 
blockquote {
margin: 0;
}
 
blockquote:before {
content: "“";
position: absolute;
left: -.4em;
}
 
blockquote:after {
content: "”";
position: absolute;
}
 
body > ul {
margin: 0;
padding: 0;
}
 
h1 {
font-size: 64px;
}
 
h1, h2, h3 {
font-weight: inherit;
margin: 0;
}
 
h2, h3 {
text-align: right;
font-size: inherit;
position: absolute;
bottom: 0;
right: 0;
}
 
h2 {
font-size: 24px;
position: absolute;
}
 
h3 {
bottom: -20px;
font-size: 18px;
}
 
.invert {
background: #1f1f1f;
color: #dcdccc;
}
 
.invert h2, .invert h3 {
color: #7f9f7f;
}
 
.string, .regexp {
color: #f39;
}
 
.keyword {
color: #00c;
}
 
.comment {
color: #777;
font-style: oblique;
}
 
.number {
color: #369;
}
 
.class, .special {
color: #1181B8;
}
 
body > svg {
position: absolute;
top: -80px;
left: -160px;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.