Skip to content

Commit

Permalink
Adding (rough) labels in the streamgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
giorgiocaviglia committed May 9, 2014
1 parent b4ed50f commit 9dce669
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion charts/streamgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@
.values(['silhouette','wiggle','expand','zero'])
.defaultValue('silhouette')

var showLabels = chart.checkbox()
.title("show labels")
.defaultValue(true)

var colors = chart.color()
.title("Color scale")

chart.draw(function (selection, data){
console.log(data)

var g = selection
.attr("width", +width() )
Expand Down Expand Up @@ -117,8 +120,35 @@
.enter().append("path")
.attr("class","layer")
.attr("d", area)
.attr("title", function (d){ return d[0].group; })
.style("fill", function (d) { return colors()(d[0].group); });

if (!showLabels()) return;

g.selectAll("text.label")
.data(labels(layers))
.enter().append("text")
.attr("x", function(d){ return x(d.x); })
.attr("y", function(d){ return y(d.y0 + d.y/2); })
.text(function(d){ return d.group; })
.style("font-size","11px")
.style("font-family","Arial, Helvetica")


function labels(layers){
return layers.map(function(layer){
var l = layer[0], max = 0;
layer.forEach(function(d){
if ( d.y > max ) {
max = d.y;
l = d;
}
})
return l;
})

}

})

})();

0 comments on commit 9dce669

Please sign in to comment.