读取 d3 文本元素的宽度 [英] read width of d3 text element

查看:13
本文介绍了读取 d3 文本元素的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取在 d3 中创建的 元素的宽度.

I would like to read the width of <text> elements, created in d3, after they're created.

我试过了

svg.selectAll("text")
    .each(function () {
        console.log('text', d3.select(this).style("width"));
    }); // 'auto'

svg.selectAll("text")
    .each(function () {
        console.log('text', $(this).css("width"));
    }); // '0px'

提前致谢

推荐答案

在一个选择方法中(比如attr):

In a selection method (such as attr) :

   this.getComputedTextLength(). 

在选择一个元素时,你可以说

In a selection of one element, you can say

   selection.node().getComputedTextLength(). 

您也可以使用 getBBox 作为宽度和高度

You can also use getBBox for the width and height

这篇关于读取 d3 文本元素的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆