function simpleLabel() {
let x = () => 0,
y = () => 0,
text = d => d,
size = 16,
padding = 5,
margin = 5,
textWrap = wrap(100).clamp(false),
textDims = textWrap.store();
const simpleLabel = function(context, layerMode) {
const selection = context.selection ? context.selection() : context;
const g = selection
.selectAll('g.label')
.data(d => (layerMode ? d : [d]))
.join(enter => enter.append('g').classed('label', true))
.attr('transform', d => `translate(${x(d)}, ${y(d)})`);
g.selectAll('text')
.data(d => [d])
.join('text')
.attr('dx', margin + padding)
.attr('dy', 0.35 * size)
.attr('font-size', size)
.text(d => text(d))
.call(textWrap);
g.selectAll('rect')
.data(d => [d])
.join(enter => enter.insert('rect', 'text'))
.attr('width', function() {
const textNode = d3
.select(this.parentElement)
.select('text')
.node();
return textDims.get(textNode).width + 2 * padding;
})
.attr('height', function() {
const textNode = d3
.select(this.parentElement)
.select('text')
.node();
return textDims.get(textNode).height + 2 * padding;
})
.attr('x', margin)
.attr('y', -0.5 * size - padding);
};
mixin(simpleLabel, textWrap, ['width', 'clamp']);
simpleLabel.x = function(_) {
return arguments.length ? ((x = _), this) : x;
};
simpleLabel.y = function(_) {
return arguments.length ? ((y = _), this) : y;
};
simpleLabel.text = function(_) {
return arguments.length ? ((text = _), this) : text;
};
simpleLabel.size = function(_) {
return arguments.length ? ((size = _), this) : size;
};
simpleLabel.padding = function(_) {
return arguments.length ? ((padding = _), this) : padding;
};
simpleLabel.margin = function(_) {
return arguments.length ? ((margin = _), this) : margin;
};
return simpleLabel;
}