Javascript NVD3折线图X轴记号缺失
我正在使用NVD3在此处显示折线图: 但NVD3似乎会自动隐藏XAxis上的一些记号标签,但只隐藏边缘附近的记号,即10月2-3日和27-28日(第一个和最后一个记号除外)。我知道这是一个自动减少,因为当我增加图表的宽度时,刻度开始显示。然而,我发现这种还原行为很奇怪,线形图没有像multiBarChart那样的ReduceTexticks选项 我希望自己能够控制减少行为,如: 但它不起作用。有人知道怎么控制吗Javascript NVD3折线图X轴记号缺失,javascript,d3.js,nvd3.js,linechart,Javascript,D3.js,Nvd3.js,Linechart,我正在使用NVD3在此处显示折线图: 但NVD3似乎会自动隐藏XAxis上的一些记号标签,但只隐藏边缘附近的记号,即10月2-3日和27-28日(第一个和最后一个记号除外)。我知道这是一个自动减少,因为当我增加图表的宽度时,刻度开始显示。然而,我发现这种还原行为很奇怪,线形图没有像multiBarChart那样的ReduceTexticks选项 我希望自己能够控制减少行为,如: 但它不起作用。有人知道怎么控制吗 由于默认情况下showMaxMin设置为true,因此还原行为有效。添加.showM
由于默认情况下
showMaxMin
设置为true,因此还原行为有效。添加.showMaxMin(false)
修复了以下问题:
chart.xAxis.axisLabel("XAxisLabel")
.showMaxMin(false)
.tickValues(tickvalues)
.tickFormat(function (d) {
return tickformat[d];
})
;
如果希望边界刻度和接近边界(MaxMin)值的刻度,可以修改源 在nv.models.axis()中,当底部/顶部方向的showMaxMin为真时,会给出一个缓冲区:
if (showMaxMin && (axis.orient() === 'top' || axis.orient() === 'bottom')) {
var maxMinRange = [];
wrap.selectAll('g.nv-axisMaxMin')
.each(function(d,i) {
try {
if (i) // i== 1, max position
maxMinRange.push(scale(d) - this.getBoundingClientRect().width - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case)
else // i==0, min position
maxMinRange.push(scale(d) + this.getBoundingClientRect().width + 4)
}catch (err) {
if (i) // i== 1, max position
maxMinRange.push(scale(d) - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case)
else // i==0, min position
maxMinRange.push(scale(d) + 4);
}
});
// the g's wrapping each tick
g.selectAll('g').each(function(d, i) {
if (scale(d) < maxMinRange[0] || scale(d) > maxMinRange[1]) {
if (d > 1e-10 || d < -1e-10) // accounts for minor floating point errors... though could be problematic if the scale is EXTREMELY SMALL
d3.select(this).remove();
else
d3.select(this).select('text').remove(); // Don't remove the ZERO line!!
}
});
}
使用
.tickValues()
而不是.ticks()
。我以前尝试过使用.tickFormat()
,但它在上面缺少的tickLabel上为我提供了一个过滤器。此外,使用此技巧时,隐藏标签甚至不会显示在工具提示中。我相信tickValues()
也会有同样的结果。嗯,我会使用x轴的时间刻度——这会让你更好地控制显示的内容。这应该会有帮助。注意不要有任何超出范围的值,否则它们将显示在yaxis的左侧。您还可以使用xDomain
设置xAxis的范围
if (showMaxMin && (axis.orient() === 'top' || axis.orient() === 'bottom')) {
var maxMinRange = [];
wrap.selectAll('g.nv-axisMaxMin')
.each(function(d,i) {
try {
if (i) // i== 1, max position
maxMinRange.push(scale(d) - this.getBoundingClientRect().width - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case)
else // i==0, min position
maxMinRange.push(scale(d) + this.getBoundingClientRect().width + 4)
}catch (err) {
if (i) // i== 1, max position
maxMinRange.push(scale(d) - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case)
else // i==0, min position
maxMinRange.push(scale(d) + 4);
}
});
// the g's wrapping each tick
g.selectAll('g').each(function(d, i) {
if (scale(d) < maxMinRange[0] || scale(d) > maxMinRange[1]) {
if (d > 1e-10 || d < -1e-10) // accounts for minor floating point errors... though could be problematic if the scale is EXTREMELY SMALL
d3.select(this).remove();
else
d3.select(this).select('text').remove(); // Don't remove the ZERO line!!
}
});
}
try {
if (i) // i== 1, max position
maxMinRange.push(scale(d));
else // i==0, min position
maxMinRange.push(scale(d))
}catch (err) {
if (i) // i== 1, max position
maxMinRange.push(scale(d));
else // i==0, min position
maxMinRange.push(scale(d));
}