Skip to content

Instantly share code, notes, and snippets.

@hepplerj
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save hepplerj/50d13807aa1405a8204d to your computer and use it in GitHub Desktop.

Select an option

Save hepplerj/50d13807aa1405a8204d to your computer and use it in GitHub Desktop.
TokenArc
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Token Arcs</title>
<meta charset="utf-8" />
</head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="visualization.js"></script>
<style>
body {
font-family: 'Source Sans Pro', sans-serif;
font-weight: 300;
}
b {
font-weight: 900;
}
.outline {
fill: none;
stroke: #888888;
stroke-width: 1px;
}
#tooltip {
font-size: 10pt;
font-weight: 900;
fill: #000000;
stroke: #ffffff;
stroke-width: 0.25px;
}
.node {
stroke: #ffffff;
stroke-weight: 1px;
}
.link {
fill: none;
stroke: #888888;
stroke-weight: 1px;
stroke-opacity: 0.5;
}
.highlight {
stroke: red;
stroke-weight: 4px;
stroke-opacity: 1.0;
}
</style>
<body>
<h2>TokenArc</h2>
<script>
d3.json("tokens.json", arcDiagram);
</script>
</body>
</html>
{
"nodes":[
{"name":"i","group":1},
{"name":"s","group":2},
{"name":"f","group":3},
{"name":"d","group":4},
{"name":"c","group":5},
{"name":"i","group":6},
{"name":"s","group":7},
{"name":"f","group":8},
{"name":"d","group":9},
{"name":"c","group":10}
],
"links":[
{"source":0,"target":2,"value":1},
{"source":0,"target":2,"value":8},
{"source":2,"target":1,"value":10},
{"source":0,"target":2,"value":6},
{"source":3,"target":0,"value":1},
{"source":0,"target":1,"value":1},
{"source":0,"target":5,"value":1},
{"source":2,"target":9,"value":1},
{"source":9,"target":0,"value":1},
{"source":8,"target":2,"value":1},
{"source":7,"target":1,"value":1},
{"source":4,"target":2,"value":1},
{"source":0,"target":1,"value":1},
{"source":6,"target":8,"value":1},
{"source":6,"target":1,"value":1},
{"source":4,"target":2,"value":1}
]
}
var width = 960,
height = 500,
margin = 20,
pad = margin / 2,
radius = 8,
yfixed = pad + radius;
/**
* Helper Functions
*/
function addTooltip(circle) {
// code
}
/**
* Main
*/
function arcDiagram(graph) {
var svg = d3.select("body").append("svg")
.attr("id", "arc")
.attr("width", width)
.attr("height", height);
// create plot within svg
var plot = svg.append("g")
.attr("id", "plot")
.attr("transform", "translate(" + pad + ", " + pad + ")");
// fix graph links to map to objects
graph.links.forEach(function(d,i) {
d.source = isNaN(d.source) ? d.source : graph.nodes[d.source];
d.target = isNaN(d.target) ? d.target : graph.nodes[d.target];
});
linearLayout(graph.nodes);
drawLinks(graph.links);
drawNodes(graph.nodes);
}
// layout nodes linearly
function linearLayout(nodes) {
nodes.sort(function(a,b) {
return a.group - b.group;
})
var xscale = d3.scale.linear()
.domain([0, nodes.length - 1])
.range([radius, width - margin - radius]);
nodes.forEach(function(d, i) {
d.x = xscale(i);
d.y = yfixed;
});
}
function drawNodes(nodes) {
var color = d3.scale.category20();
d3.select("#plot").selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("id", function(d, i) { return d.name; })
.attr("cx", function(d, i) { return d.x; })
.attr("cy", function(d, i) { return d.y; })
.attr("r", function(d, i) { return radius; })
.style("fill", function(d, i) { return color(d.group); })
.on("mouseover", function(d,i) { addTooltip(d3.select(this)); })
.on("mouseout", function(d,i) { d3.select("#tooltip").remove(); });
}
function drawLinks(links) {
var radians = d3.scale.linear()
.range([Math.PI / 2, 3 * Math.PI / 2]);
var arc = d3.svg.line.radial()
.interpolate("basis")
.tension(0)
.angle(function(d) { return radians(d); });
d3.select("#plot").selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("transform", function(d,i) {
var xshift = d.source.x + (d.target.x - d.source.x) / 2;
var yshift = yfixed;
return "translate(" + xshift + ", " + yshift + ")";
})
.attr("d", function(d,i) {
var xdist = Math.abs(d.source.x - d.target.x);
arc.radius(xdist / 2);
var points = d3.range(0, Math.ceil(xdist / 3));
radians.domain([0, points.length - 1]);
return arc(points);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment