【发布时间】:2016-09-19 14:16:37
【问题描述】:
我正在 D3 版本 4 中构建一个条形图,使用时间刻度作为我的 x 轴。轴本身渲染得很好,但条形图已关闭;图表中的最后一个条形出现在轴范围之外。如何指定正确的范围和/或 x 值?
代码片段:
const xScale = (width, dates) =>
scaleTime()
.domain([new Date(dates[0]), new Date(last(dates))])
.range([0, width])
svg.select('g')
.selectAll(`.${styles['bar']}`)
.data(series)
.enter()
.append('rect')
.attr('class', styles['bar'])
.attr('x', (d) => x(d.date))
.attr('width', Math.floor(width / series.length))
.attr('y', (d) => {
const scaled = y(d.count)
return height - scaled > 0 && height - scaled < 2
? height - 2
: scaled
})
.attr('height', (d) => {
const scaled = height - y(d.count)
return scaled > 0 && scaled < 2
? 2
: scaled
})
【问题讨论】:
-
看起来最后一个条的左边缘出现在范围内。我想您可以通过使您的范围略宽于
width来解决此问题。 (说宽度 + 一个条的预期宽度。) -
不幸的是,这也没有正确对齐。我添加了一个 JSFiddle 来说明这个问题。
标签: javascript d3.js