D3 V4,我如何在每个数据点都有一个刻度线,但只有选择数据点的标签(D3 V4, How can I have a tick mark at every data point, but a labe

编程入门 行业动态 更新时间:2024-10-25 10:31:10
D3 V4,我如何在每个数据点都有一个刻度线,但只有选择数据点的标签(D3 V4, How can I have a tick mark at every data point, but a label at only select data points)

从图像中可以看出我正在使用月度数据。 我正试图找到一种方法来显示每个刻度线,但只显示4月份的标签。 例如:2014年4月,2015年4月,2016年4月和2017年4月 - 并在两者之间保留刻度线。 提前致谢。

从此代码生成的X轴

g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(d3.timeMonth.every(1)) .tickFormat(d3.timeFormat("%b %Y") ) ); g.select('.axis.axis--x') .selectAll("text") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-65)" );

感谢Sira,这就是我最终的结果:

g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(d3.timeMonth.every(1)) .tickFormat(d3.timeFormat("%b %Y") ) ); g.select('.axis.axis--x') .selectAll("text") .style("text-anchor", "end") .style("opacity", function(d){ if (d3.select(this).text().includes("Apr")){return "1"}else{return "0"} }) .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-65)" );

As you can see from the image I'm using monthly data. I'm trying to find a way to display every tick mark, but only the April month labels. example: Apr 2014, Apr 2015, Apr 2016 and Apr 2017 - and keep the tick marks in between. Thanks in advance.

X axis generated from this code

g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(d3.timeMonth.every(1)) .tickFormat(d3.timeFormat("%b %Y") ) ); g.select('.axis.axis--x') .selectAll("text") .style("text-anchor", "end") .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-65)" );

Thanks to Sira, this is what I ended up with:

g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x).ticks(d3.timeMonth.every(1)) .tickFormat(d3.timeFormat("%b %Y") ) ); g.select('.axis.axis--x') .selectAll("text") .style("text-anchor", "end") .style("opacity", function(d){ if (d3.select(this).text().includes("Apr")){return "1"}else{return "0"} }) .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-65)" );

最满意答案

看一下d3.class 。 在回调中,您将分别访问基准,索引和this 。

因此,如果您希望仅在数据中包含“Apr”时显示刻度,您可以执行以下操作:

g.select('.axis.axis--x') .selectAll("text") .style("text-anchor", "end") .attr("display", function(d, i, this) { if (!d.contains("Apr") { return "none"; } }) .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-65)" );

您可以将if替换为其他语句,这些语句将为您希望它消失的数据点返回"none" 。

Take a look at d3.class with callback. Inside the callback, you will have an access to the datum, index and this respectively.

So if you want the ticks to only show up if it contains "Apr" in the data, you can do something like:

g.select('.axis.axis--x') .selectAll("text") .style("text-anchor", "end") .attr("display", function(d, i, this) { if (!d.contains("Apr") { return "none"; } }) .attr("dx", "-.8em") .attr("dy", ".15em") .attr("transform", "rotate(-65)" );

You can replace the if with other statements that will return "none" for the data point that you want it to disappear.

更多推荐

本文发布于:2023-04-27 21:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329064.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都有   刻度   据点   标签   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!