Commit 5b9ba585 authored by S Anand's avatar S Anand

ENH: G.network() supports labels by default

parent 465bcbd3
......@@ -258,14 +258,27 @@ _G_network.force = function() {
.attr('fill', 'rgba(0,0,0,.5)')
}
function draw_label(labels) {
labels
.text(function(node) { return node.key + ': ' + node.value })
.attr({
'text-anchor': 'middle',
'dy': '-0.8em',
'fill': 'rgba(0,0,0,.8)',
'font-size': 8,
})
}
force.on('tick', function() {
self.lines.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 })
self.circles.attr('transform', function(d) {
var set_pos = function(d) {
return 'translate(' + d.x + ',' + d.y + ')'
})
}
self.circles.attr('transform', set_pos)
self.labels.attr('transform', set_pos)
})
force.drag()
......@@ -363,6 +376,7 @@ _G_network.force = function() {
}
self.circle = function(v) { if (!arguments.length) return draw_circle; draw_circle = v; return self }
self.label = function(v) { if (!arguments.length) return draw_label; draw_label = v; return self }
self.line = function(v) { if (!arguments.length) return draw_line; draw_line = v; return self }
self.draw = function(svg) {
......@@ -383,6 +397,9 @@ _G_network.force = function() {
var $circles = svg.select('g.circles')
if ($circles.empty())
$circles = svg.append('g').attr('class', 'circles')
var $labels = svg.select('g.labels')
if ($labels.empty())
$labels = svg.append('g').attr('class', 'labels')
force
.nodes(nodes)
......@@ -406,12 +423,25 @@ _G_network.force = function() {
.call(force.drag)
draw_circle(self.circles)
self.labels = $labels.selectAll('text.node')
.data(nodes, function(d) { return d.id })
self.labels.exit().remove()
enter = self.labels.enter()
.append('text')
.attr('class', 'node')
.on('dblclick.release', _node_release)
.call(force.drag)
draw_label(self.labels)
if (!svg.on('dblclick.release'))
svg.on('dblclick.release', function() {
if (d3.event.target == svg.node()) {
self.circles
.filter(function(d) { return d.fixed })
.classed('fixed', function(d) { return d.fixed = false })
self.labels
.filter(function(d) { return d.fixed })
.classed('fixed', function(d) { return d.fixed = false })
force.start()
}
})
......@@ -425,6 +455,10 @@ _G_network.force = function() {
return d.selected = extent[0][0] <= d.x && d.x < extent[1][0] &&
extent[0][1] <= d.y && d.y < extent[1][1]
})
self.labels.classed('selected', function(d) {
return d.selected = extent[0][0] <= d.x && d.x < extent[1][0] &&
extent[0][1] <= d.y && d.y < extent[1][1]
})
})
brush($brush)
......
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