Skip to content

Instantly share code, notes, and snippets.

@Y4suyuki
Created October 7, 2013 08:33
Show Gist options
  • Save Y4suyuki/6864482 to your computer and use it in GitHub Desktop.
Save Y4suyuki/6864482 to your computer and use it in GitHub Desktop.
cluster toggle
{"description":"cluster toggle","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"flare.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"inlet.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":true,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/KKztdwo.png","inline-console":false}
{
"name": "flare",
"children": [
{
"name": "analytics",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "size": 3938},
{"name": "CommunityStructure", "size": 3812},
{"name": "MergeEdge", "size": 743}
]
},
{
"name": "graph",
"children": [
{"name": "BetweennessCentrality", "size": 3534},
{"name": "LinkDistance", "size": 5731}
]
}
]
}
]
}
.link {
fill:none;
stroke: #ccc;
stroke-width: 1.5px;
}
.node circle {
stroke: steelblue;
stroke-width: 2.5px;
}
var svg = d3.select("svg")
var flare_d = tributary.flare;
var cluster = d3.layout.cluster()
.size([435,263]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var nodes = cluster.nodes(flare_d),
links = cluster.links(nodes);
var link = svg.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("d", diagonal);
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + d.y + ", " + d.x + ")"});
node.append("circle")
.attr("r", 4.5)
.attr("fill", "none")
.on("mouseover", function() {
d3.select(this).attr("fill", "steelblue");
})
.on("mouseout", function() {
d3.select(this).attr("fill", "none");
})
.on("click", function(d) {
toggle(d);
update(d);
});
node.append("text")
.attr("dx", function(d) { return d.childlen ? -8 : 8; })
.attr("dy", function(d) { return 3; })
.style("textanchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.size ? d.name + ": " + d.size : d.name; })
function toggle(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
}
function update(source) {
var nodes = cluster.nodes(flare_d),
links = cluster.links(nodes);
var node = d3.selectAll(".node")
.data(nodes),
link = d3.selectAll(".link").data(links);
console.log(node.enter());
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + source.y0 + "," + source.x0 + ")";
})
.on("click", function() {
toggle(d);
update(d);
});
nodeEnter.append("circle")
.attr("r", 4.5)
.attr("fill", "none");
nodeEnter.append("text")
.attr("dx", function(d) { return d.childlen ? -8 : 8; })
.attr("dy", function() { return 3; })
.style("textanchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.size ? d.name + ": " + d.size : d.name; });
var nodeUpdate = node.transition()
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeUpdate.select("circle")
.attr("r", 4.5)
.attr("fill", function(d) { return d._children? "lightsteelblue" : "#fff";});
var nodeExit = node.exit().transition()
.attr("transform", "translate(" + source.y + "," + source.x + ")")
.remove();
link.enter().insert("path")
.attr("class", ".link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0 };
return diagonal({source: o, target: o});
})
.transition()
.attr("d", diagonal);
link.transition()
.attr("d", diagonal);
link.exit().transition()
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
}).remove();
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment