向D3.js轴添加特定的刻度 [英] Adding a specific tick to a D3.js axis
问题描述
我用d3.svg.axis
和一个时间刻度创建了一个轴,并对滴答发生器产生的滴答感到满意.但是,我想确保始终标记一个特定的值.因此,例如,如果生成器生成以下日期
I've created an axis with d3.svg.axis
and a time scale and am happy with the ticks produced by the tick generator. However, I would like to ensure that a particular value is always marked. So for example the if the generator produces the following dates
2000-1-1、2001-1-1、2002-1-1、2003-1-1
2000-1-1, 2001-1-1, 2002-1-1, 2003-1-1
我可能想使轴显示
2000-1-1, 2000-7-21 ,2001-1-1、2002-1-1、2003-1-1
2000-1-1, 2000-7-21, 2001-1-1, 2002-1-1, 2003-1-1
我如何获取由报价生成器生成的报价数组,以便可以添加值并将其传递给tickValues
函数?
How do i get an array of the ticks made by the tick generator so that I can add my value and pass it into the tickValues
function?
我可以创建第二个轴,对其进行样式设置以删除域路径,并将任何其他日期传递给该轴tickValues
函数,但这似乎有点尴尬.
I could create a second axis, style it to remove the domain path and pass any additional dates to that ones tickValues
function but that seems a bit awkward.
还是我会以错误的方式进行操作?
Or am I going about this in the wrong way?
谢谢
推荐答案
设置比例尺后,轴,您可以不带任何参数调用ticks()
来获取其生成的值:
Once you've set up your scale & axis, you can call ticks()
with no parameters to get the values that it has generated:
ticks = myScale.ticks();
然后您可以推送/拼接/任何内容:
And then you can push/splice/whatever:
ticks.push(some_new_value):
然后将它们传递回tickValues
myAxis.tickValues(ticks);
在执行所有这些操作之前,请先调用该轴将其添加到SVG中.
Do all this before you .call this axis to add it to the SVG, of course.
这篇关于向D3.js轴添加特定的刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!