为各个轴文本元素分配 ID [英] Assign IDs to Individual Axis Text Elements

查看:30
本文介绍了为各个轴文本元素分配 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 d3.axis() 创建轴标签和刻度线,我想使用 d3.select("#axisTextIDValue") 单独引用它们,但我看不到任何方法来操作单个的 id 属性轴中的文本元素.有谁知道如何为轴的刻度或文本元素创建 ID 值?

I'm using d3.axis() to create axis labels and tick marks that I'd like to refer to individually using d3.select("#axisTextIDValue") but I see no way to manipulate the id attribute of individual text elements in an axis. Does anyone know how to create ID values for the ticks or text elements of an axis?

推荐答案

虽然其他答案确实解释了如何选择刻度,但如果您想为生成的元素分配唯一的 id,可以通过给您为轴调用的 g 的 id 或类,然后根据它们的数组位置为子文本元素设置 id:

While the other answer does explain how to select the ticks, if you want to assign unique ids to generated elements like these, you can do so using subselections by giving an id or class to the g that you've called for your axis and then setting ids for the child text elements based on their array position:

 d3.select("svg").append("g").attr("id", "axisG").call(axis);
 d3.select("#axisG").selectAll("text").attr("id", function(d,i) {return "axisText" + i});

 d3.select("#axisText0").node(); //demonstrates selectability

也就是说,这可能不是一个好主意,并且有更好的方法来处理由 D3 即时生成的刻度/标签/voronoi/等,但我想确保放置一个在这里回答,以防有人正在寻找如何做这种事情.

That said, this probably isn't a very good idea and there are better ways to deal with ticks/labels/voronoi/etc that are generated on-the-fly by D3, but I wanted to make sure to put an answer up here just in case somebody is looking for how to do this kind of thing.

这篇关于为各个轴文本元素分配 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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