Skip to content

Instantly share code, notes, and snippets.

@jalapic
Last active February 26, 2016 04:42
Show Gist options
  • Save jalapic/6e0508ae81e6488ca38b to your computer and use it in GitHub Desktop.
Save jalapic/6e0508ae81e6488ca38b to your computer and use it in GitHub Desktop.
network small
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
.node circle {
fill: #DDD;
stroke: #777;
stroke-width: 2px;
}
.node text {
font-family: sans-serif;
text-anchor: middle;
pointer-events: none;
user-select: none;
-webkit-user-select: none;
}
.link {
stroke: #88A;
stroke-width: 4px;
marker-end: url(#end-arrow);
}
text {
font: 18px sans-serif;
pointer-events: none;
}
#end-arrow {
fill: #88A;
}
</style>
<body>
<script>
function bar() {
console.log("click");
force.stop();
force.start();
}
var links = [
{source: "A", target: "D", type: "high"},
{source: "E", target: "K", type: "high"},
{source: "E", target: "G", type: "low"},
{source: "E", target: "F", type: "high"},
{source: "D", target: "E", type: "high"}
];
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 = 700;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(105)
.charge(-775)
.on("tick", tick)
.start();
force.on("start", function () {
console.log("start");
});
force.on("end", function () {
console.log("end");
});
R=18
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
// add defs-marker
svg.append('svg:defs')
.append('svg:marker')
.attr('id', 'end-arrow')
.attr('viewBox', '0 0 10 10')
.attr('refX', 2+R)
.attr('refY', 5)
.attr('markerWidth', 4)
.attr('markerHeight', 4)
.attr('orient', 'auto')
.append('svg:path')
.attr('d', 'M0,0 L0,10 L10,5 z');
var link = svg.selectAll(".link")
.data(force.links())
.enter()
.append("line")
.attr("class", "link")
.attr('marker-end', 'url(#end-arrow)')
;
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("g")
.attr("class", "node")
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.call(force.drag);
node.append("circle")
.attr("r", R);
node.append("text")
.attr("x", 0)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
function tick() {
link
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; })
;
node
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
}
function mouseover() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", R+8);
d3.select(this).select("text").transition()
.duration(750)
.style("font-size", '32px')
.attr("dy", ".3em")
;
}
function mouseout() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", R);
d3.select(this).select("text").transition()
.duration(750)
.style("font-size", '18px')
.attr("dy", ".35em");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment