d3检索并添加到当前选择的属性值 [英] d3 retrieve and add to current selection's attribute value

查看:126
本文介绍了d3检索并添加到当前选择的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取元素翻译的值.

I'm trying to get the values for an element's translation.

例如,如果我选择x轴:

For example, if I select the x axis:

d3.select('.x.axis').attr("transform")

然后我得到

"translate(0,112)"

如何在不解析正则表达式的情况下获取0112?

How do I get the 0 and the 112 without parsing a regexp?

我正在尝试这样做,以便可以添加到值中.用伪代码:

I'm trying to do it so that I can add to the value. In pseudocode:

d3.selectAll('.x.axis').attr('transform', 'translate('
        .attr('transform').match(/(\d+)(\.\d+)?/g)[0] // <-- clearly won't work
        + additional_value
        + ', 0)');

推荐答案

D3提供了 transform()函数正是出于这个目的:

D3 provides the transform() function for exactly this purpose:

var t = d3.transform(d3.select('.x.axis').attr("transform")),
    x = t.translate[0],
    y = t.translate[1];

这篇关于d3检索并添加到当前选择的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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