将ID分配给单个轴文本元素 [英] Assign IDs to Individual Axis Text Elements

查看:140
本文介绍了将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生成的ticks / labels / 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天全站免登陆