Skip to content

Instantly share code, notes, and snippets.

@kardeiz
Created October 15, 2012 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kardeiz/3892974 to your computer and use it in GitHub Desktop.
Save kardeiz/3892974 to your computer and use it in GitHub Desktop.
starting point for my labels v.2
<!DOCTYPE html>
<html>
<head>
<script src="http://d3js.org/d3.v2.js"></script>
<script type="text/javascript" src="https://raw.github.com/ZJONSSON/d3-plugins/master/force_labels/force_labels.js"></script>
<style>
.anchor { fill:blue}
.labelbox { fill:black;opacity:0.8}
.labeltext { fill:white;font-weight:bold;text-anchor:middle;font-size:16;font-family: serif}
.link { stroke:gray;stroke-width:0.35}
</style>
</head>
<body>
<script type="text/javascript">
var w=960,h=500,
x_mean = w/2,
x_std = w/10,
y_mean = h/2,
y_std = h/10,
labelBox,link,
data=[];
var svg=d3.select("body")
.append("svg:svg")
.attr("height",h)
.attr("width",w)
function refresh() {
// plot the data as usual
anchors = svg.selectAll(".anchor").data(data,function(d,i) { return i})
anchors.exit().attr("class","exit").transition().duration(1000).style("opacity",0).remove()
anchors.enter().append("circle").attr("class","anchor").attr("r",4).attr("cx",function(d) { return d.x}).attr("cy",function(d) { return h-d.y})
anchors.transition()
.delay(function(d,i) { return i*10})
.duration(1500)
.attr("cx",function(d) { return d.x})
.attr("cy",function(d) { return h-d.y})
// Now for the labels
anchors.call(labelForce.update) // This is the only function call needed, the rest is just drawing the labels
labels = svg.selectAll(".labels").data(data,function(d,i) { return i})
labels.exit().attr("class","exit").transition().delay(0).duration(500).style("opacity",0).remove()
// Draw the labelbox, caption and the link
newLabels = labels.enter().append("g").attr("class","labels")
newLabelBox = newLabels.append("g").attr("class","labelbox")
newLabelBox.append("circle").attr("r",11)
newLabelBox.append("text").attr("class","labeltext").attr("y",6)
newLabels.append("line").attr("class","link")
labelBox = svg.selectAll(".labels").selectAll(".labelbox")
links = svg.selectAll(".link")
labelBox.selectAll("text").text(function(d) { return d.num})
}
function redrawLabels() {
labelBox
.attr("transform",function(d) { return "translate("+d.labelPos.x+" "+d.labelPos.y+")"})
links
.attr("x1",function(d) { return d.anchorPos.x})
.attr("y1",function(d) { return d.anchorPos.y})
.attr("x2",function(d) { return d.labelPos.x})
.attr("y2",function(d) { return d.labelPos.y})
}
// Initialize the label-forces
labelForce = d3.force_labels()
.linkDistance(0.0)
.gravity(0)
.nodes([]).links([])
.charge(-60)
.on("tick",redrawLabels)
// and now for the data functionality
function randomize(count) {
z1=d3.random.normal()
z2=d3.random.normal()
data=data.concat(d3.range(count || 100).map(function(d,i) { return {z1:z1(),z2:z2(),num:data.length+i}}))
correlate()
}
function correlate() {
// var corr = 0;
// data.forEach(function(d) { d.x = x_mean+(d.z1*x_std),
d.y = y_mean+y_std*(corr*d.z1+d.z2*Math.sqrt(1-Math.pow(corr,2)))})
refresh()
}
randomize()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment