Javascript 使用d3.js了解范围
我在使用d3js和日期时遇到一些问题: 这是一些数据,一个带有权重和日期的对象数组Javascript 使用d3.js了解范围,javascript,d3.js,Javascript,D3.js,我在使用d3js和日期时遇到一些问题: 这是一些数据,一个带有权重和日期的对象数组 var weights = [{"atdate":"2013-09-07","weight":"82","comment":"Reprise !"}, {"atdate":"2013-09-10","weight":"81.3","comment":""}, {"atdate":"2013-09-15","weight":"80.8","comment":""}, {"atdate":"2013-09-16","
var weights = [{"atdate":"2013-09-07","weight":"82","comment":"Reprise !"},
{"atdate":"2013-09-10","weight":"81.3","comment":""},
{"atdate":"2013-09-15","weight":"80.8","comment":""},
{"atdate":"2013-09-16","weight":"80.8","comment":""},
{"atdate":"2013-09-17","weight":"80.3","comment":""},
{"atdate":"2013-09-18","weight":"81.2","comment":""},
{"atdate":"2013-09-19","weight":"80.9","comment":""},
{"atdate":"2013-09-20","weight":"80.7","comment":""},
{"atdate":"2013-09-21","weight":"81","comment":""},
{"atdate":"2013-09-22","weight":"80.8","comment":""}];
您可以看到,最小日期为2013-09-07,最大日期为2013-09-22
现在,我将这些数据发送到一个函数
function draw_spline (_weights) {
var parse_date = d3.time.format('%Y-%m-%d').parse;
var weights = _weights.map(function(w) {
w.atdate = parse_date(w.atdate);
w.weight = parseFloat(w.weight);
return w;
})
var x = d3.time.scale().range([0,width]);
var boundaries_dates = d3.extent(weights, function(d){return d.atdate});
console.log('boundaries_dates',boundaries_dates);
x.domain(boundaries_dates);
console.log('domain extent',x.domain());
...
我使用parse_date构造真实的日期对象。以下是控制台的输出:
boundaries_dates, Array [
Mon Sep 23 2013 00:00:00 GMT+0200 ,
Sun Sep 22 2013 00:00:00 GMT+0200 ]
domain extent, Array [
Sat Sep 07 2013 00:00:00 GMT+0200 ,
Sun Sep 22 2013 00:00:00 GMT+0200 ]
正如你所看到的,范围是错误的,而不是我今天的2013-09-07。我想知道最短的日期
让我头疼的是,如果我将扩展数据块发送到一个scale域,scale.domain()会返回我想要的值。(所以有一种方法可以得到我想要的,但感觉不对)
所以
如何使用d3.extent()获得正确的区段
如何从不包含日期的数组中缩放以获得正确的第一个日期
谢谢你对我有用——在JSFIDLE中对我也有用。。。可能是我的控制台输出有问题……听起来好像你在什么地方输入了错别字或范围问题。