Javascript D3 V4,我怎么能在每个数据点上都有一个记号标记,而只在选定的数据点上有一个标签
正如你从图片中看到的,我使用的是月度数据。我试图找到一种方法来显示每个记号,但只显示4月份的标签。示例:2014年4月、2015年4月、2016年4月和2017年4月-并将勾号保持在两者之间。 提前谢谢 多亏了Sira,这就是我的结局:Javascript D3 V4,我怎么能在每个数据点上都有一个记号标记,而只在选定的数据点上有一个标签,javascript,d3.js,Javascript,D3.js,正如你从图片中看到的,我使用的是月度数据。我试图找到一种方法来显示每个记号,但只显示4月份的标签。示例:2014年4月、2015年4月、2016年4月和2017年4月-并将勾号保持在两者之间。 提前谢谢 多亏了Sira,这就是我的结局: g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBo
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)" );
看一看回调。在回调中,您将分别访问数据、索引和此
因此,如果您希望勾号仅在数据中包含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,这些语句对于希望其消失的数据点将不返回任何值 像这样的事@伊恩,这很酷,但不是我想要的。没有办法隔离特定的标签。若标签包含Apr return不透明度1,否则返回我想你们的意思是@RyanMorton
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)" );