你可能要考慮嘗試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