Commit b3388f0f authored by S Anand's avatar S Anand

ENH: G.map() supports labels via .label(). For #29

parent 6422accb
......@@ -5,6 +5,7 @@ G.map = function() {
projection,
path,
unpack,
draw_label,
size = function() { return 5; },
self = {}
......@@ -24,6 +25,36 @@ G.map = function() {
.translate(t)
}
function add_labels(selection) {
if (!draw_label)
return
// Create text nodes for each shape features
var update = selection.selectAll('text.map')
.data(shape.features)
update
.transition()
update.enter()
.append('text')
.attr('text-anchor', 'middle')
.attr('dy', '0.35em') // Vertically centered
.attr('class', 'map')
update.exit()
.remove()
// Transform the text nodes and position them at the center of the shape element
update.attr('transform', function(d) {
var centroid = path.centroid(d)
return 'translate(' + centroid[0] + ',' + centroid[1] + ')'
})
// Apply the label drawing function
draw_label(update)
return self
}
self.map = function(selection) {
center_shape()
......@@ -41,6 +72,7 @@ G.map = function() {
update.exit()
.remove()
add_labels(selection)
return update
}
......@@ -81,6 +113,7 @@ G.map = function() {
}
unpack(update)
add_labels(selection)
return update
}
......@@ -90,6 +123,7 @@ G.map = function() {
self.force = function(v) { if (!arguments.length) return force; force = v; return self; }
self.size = function(v) { if (!arguments.length) return size; size = v; return self; }
self.path = function(v) { if (!arguments.length) return path; path = v; return self; }
self.label = function(v) { if (!arguments.length) return draw_label; draw_label = v; return self; }
self.projection = function(v) {
if (!arguments.length) return projection
projection = v
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment