plot = {
const height = 100;
const width = 400;
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("g")
.selectAll("rect")
.data(data1)
.join("rect")
.attr("x", d => d.x)
.attr("y", d => d.y)
.style("fill", "black")
.style("opacity", "0.5");
svg
.append("g")
.style("font", "10px sans-serif")
.style("fill", "#000")
.selectAll("text")
.data(data1)
.join("text")
.attr("x", d => d.x)
.attr("y", d => d.y)
.text(d => d.name);
return Object.assign(svg.node(), {
update() {
svg.selectAll("text").each(function(d) {
d.bbox = this.getBBox();
console.log(d.bbox);
});
const xMargin = 4;
const yMargin = 2;
svg
.selectAll("rect")
.data(data1)
.join("rect")
.attr("width", d => d.bbox.width + 2 * xMargin)
.attr("height", d => d.bbox.height + 2 * yMargin)
.attr('transform', function(d) {
return `translate(-${xMargin}, -${d.bbox.height * 0.8 + yMargin})`;
});
}
});
}