2017-08-08 73 views
2

我正在研究这个例子:https://bl.ocks.org/d3noob/2505b09d0feb51d0c9873cc486f10f67 由d3noob制作,我意识到第一个和最后一个点部分在边缘。我想知道如何在x.domain的每一边制作一个缓冲区,以保持点距边缘的距离。如何修改处理d3.js中日期的域?

我相信,负责任的这一行是:

x.domain(d3.extent(data, function(d) { return d.date; })); 

我试图修改与d3.min和d3.max这条线,但是这似乎并没有工作,因为x轴处理日期。

回答

1

你可能要考虑尝试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