简体   繁体   English

d3.js:动态为轴添加颜色

[英]d3.js:Add color to axes dynamically

I am trying to create a line chart in d3.js.The color of the axes for this chart would be available only at runtime and can vary in a vast set of colors.It is possible to set the colors for the axes using css classes like this . 我正在尝试在d3.js中创建一个折线图,该图表的轴颜色只能在运行时使用,并且可以在多种颜色中变化。可以使用CSS类为轴设置颜色像这样

But since the range of colors that my chart can take is vast and only available at runtime, its not feasible to use this solution.Using call(yAxis).style("stroke",userColor) sets the color for the font use on axes labels and ticks but not the color of axis itself. 但是,由于我的图表可以接受的颜色范围很大,并且只能在运行时使用,因此使用此解决方案是不可行的。使用call(yAxis).style("stroke",userColor)设置轴上使用的字体的颜色标签和刻度,而不是轴本身的颜色。

Is it possible to set the color of axis in d3.js axis dynamically using javascript? 是否可以使用javascript动态设置d3.js轴的轴颜色?

I think the what you might want is something like the following: 我认为您可能想要的是以下内容:

d3.selectAll('.axis path')
    .style("stroke", userColor)
    .style("fill", userColor)

In this case, .axis path should be some identifying css on your axis lines (possibly put in with d3 as well in your yAxis function). 在这种情况下, .axis path应该是您的轴线上的一些标识css(可能在yAxis函数中也带有d3)。 You probably only need one of fill and stroke , try it out to see what works for you. 您可能只需要fillstroke ,就可以尝试一下,看看有什么适合您的。 This should operate on the actual axis line, rather than the text element 这应该在实际轴线上而不是文本元素上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM