为什么可以't我得到了这个d3.js文本的边界框
Why can't I get the Bounding Box of this d3.js text?
我有以下代码,它使用d3.js成功地将文本绘制到SVG上:
var myRect = svgContainer.append("text")
.attr("x", 10)
.attr("y", 20)
.text("Lorem Ipsum")
.attr("font-weight", "bold")
.attr("fill", "white");
现在我想得到该文本的边界框。我该怎么做??我尝试了以下方法:
console.log("getBBox = ", myRect.getBBox());
但它给了我一个错误:
Uncaught TypeError: myRect.getBBox is not a function
myRect是一个d3选择,而不是html元素。
试试这个代码:
myRect.node().getBBox();
var svgContainer = d3.select("svg");
var myRect = svgContainer.append("text")
.attr("x", 10)
.attr("y", 20)
.text("Lorem Ipsum")
.attr("font-weight", "bold")
.attr("fill", "white");
console.log("getBBox = ", myRect.node().getBBox());
svg{
background: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg width="500" height="800"></svg>
相关文章:
- 如何在d3.js中返回路径的y坐标
- 有条件更新d3.js力图中节点的最佳方法
- 使用D3.js计算带有字母间距的文本长度
- D3.js模式不适用于弧形或圆环图
- d3.js Chord图的动态工具提示
- 在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
- HTML5FileReader输出到D3.js图表
- D3.js生成有效的SVG,但不显示任何内容
- d3.js:限制画笔的大小
- d3.js用按钮更新条形图工具提示
- 如何为d3.js图表输出组织/嵌套数据
- d3-js快速事件调用问题
- D3.JS向rect添加缩放和列表项
- d3.js圆的半径是否可以由样式属性指定
- 如何在d3.js中自定义事件侦听器
- 在新窗口上使用d3.js
- 如何用d3.js绘制折线图
- 使用dc.js、d3.js和crossfilter引用错误
- D3.js剪辑路径截断了我的图的边缘
- D3.js和坐标系