你可能要考虑尝试d3fc extent component。你的情况,你可以在X标尺的两侧加有一天,当如下:
var xExtent = fc.extentDate()
// the property of the data that defines the x extent
.accessors([function(d) { return d.date; }])
// pad in domain units (in this case milliseconds)
.padUnit('domain')
// ensure that the scale is padded by one day in either direction
.pad([millisPerDay, millisPerDay]);
x.domain(xExtent(data));
您也可以使用这个组件来简化与Y域范围的计算:
var yExtent = fc.extentLinear()
// the property of the data that defines the y extent
.accessors([function(d) { return d.close; }])
// pad by 10% in the +ve direction
.pad([0, 0.1])
// ensure that the extent includes zero
.include([0]);
y.domain(yExtent(data));
这里的一个完整的例子:https://bl.ocks.org/ColinEberhardt/4b8737e0251f92075693a6e04df72638