gpt4 book ai didi

javascript - 比较 D3.js 中的两个时间对象

转载 作者:行者123 更新时间:2023-11-30 09:51:25 25 4
gpt4 key购买 nike

在 D3.js 中,我目前正在尝试根据数据数组是否包含该时间来填充一个空数组(数组仅包含每 5 分钟后的时间戳)。如果数据数组包含时间,则应复制该值,否则为 null。但是当我尝试比较两个时间对象时,它无法正常工作。

以下是代码(当前代码不是复制值而是打印以便于调试):

var data = [{"mytime": "2015-12-01T23:10:00.000Z", "value": 64},
{"mytime": "2015-12-01T23:15:00.000Z", "value": 67},
{"mytime": "2015-12-01T23:20:00.000Z", "value": 70},
{"mytime": "2015-12-01T23:25:00.000Z", "value": 64},
{"mytime": "2015-12-01T23:30:00.000Z", "value": 72},
{"mytime": "2015-12-01T23:35:00.000Z", "value": 75},
{"mytime": "2015-12-01T23:40:00.000Z", "value": 71},
{"mytime": "2015-12-01T23:45:00.000Z", "value": 80},
{"mytime": "2015-12-02T00:00:00.000Z", "value": 80},
{"mytime": "2015-12-02T00:05:00.000Z", "value": 85}
];

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ").parse;

data.forEach(function(d) {
d.mytime = parseDate(d.mytime);
});

var act_data = [];

var x_extent = d3.extent(data, function(d){
return d['mytime']});

time_arr = d3.time.minute.range(x_extent[0],x_extent[1],5);
for(var key in time_arr){
var temp;
temp = contains(data,time_arr[key],'mytime');
}


function contains(data,value,variable)
{
var i =data.length;
while(i--){

if (data[i][variable] === value)
{
console.log('found');
return i;
}
}
return false;
}

输出应该是这样的:

data = [{"mytime": "2015-12-01T23:10:00.000Z", "value": 64},
{"mytime": "2015-12-01T23:15:00.000Z", "value": 67},
{"mytime": "2015-12-01T23:20:00.000Z", "value": 70},
{"mytime": "2015-12-01T23:25:00.000Z", "value": 64},
{"mytime": "2015-12-01T23:30:00.000Z", "value": 72},
{"mytime": "2015-12-01T23:35:00.000Z", "value": 75},
{"mytime": "2015-12-01T23:40:00.000Z", "value": 71},
{"mytime": "2015-12-01T23:45:00.000Z", "value": 80},
{"mytime": "2015-12-01T23:50:00.000Z", "value": null},
{"mytime": "2015-12-01T23:55:00.000Z", "value": null},
{"mytime": "2015-12-02T00:00:00.000Z", "value": 80},
{"mytime": "2015-12-02T00:05:00.000Z", "value": 85}
];

最佳答案

您可以使用(D3 3.x API)

这给了我们

var data = [...];

// for parsing and formatting
var timeformat = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ");

// range to operate on
var extent = d3.extent(data, function(d){
return timeformat.parse(d.mytime);
});

// scale representing the time stamps
var scale = d3.time.scale()
.domain(extent) // the boundaries
.ticks(d3.time.minute, 5); // 5 minutes intervals

// easier to determine if a value exists
var hashed = d3.map(data, function(d) { return d.mytime; });

// or if your version < 3.5
/*
var hashed = d3.map();
data.forEach(function(d) {
hashed.set(d.mytime, d);
});
*/


// transforms the scale into the desired output
var res = scale.map(function(t) {
var ft = timeformat(t);
return {
mytime: ft,
value: (hashed.has(ft)) ? hashed.get(ft).value : null
};
});

还有一个演示 http://jsfiddle.net/nikoshr/qxcLry8z/

var data = [
{"mytime": "2015-12-01T23:10:00.000Z", "value": 64},
{"mytime": "2015-12-01T23:15:00.000Z", "value": 67},
{"mytime": "2015-12-01T23:20:00.000Z", "value": 70},
{"mytime": "2015-12-01T23:25:00.000Z", "value": 64},
{"mytime": "2015-12-01T23:30:00.000Z", "value": 72},
{"mytime": "2015-12-01T23:35:00.000Z", "value": 75},
{"mytime": "2015-12-01T23:40:00.000Z", "value": 71},
{"mytime": "2015-12-01T23:45:00.000Z", "value": 80},
{"mytime": "2015-12-02T00:00:00.000Z", "value": 80},
{"mytime": "2015-12-02T00:05:00.000Z", "value": 85}
];


var timeformat = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ");

var extent = d3.extent(data, function(d){
return timeformat.parse(d.mytime);
});

var scale = d3.time.scale()
.domain(extent)
.ticks(d3.time.minute, 5);

var hashed = d3.map();
data.forEach(function(d) {
hashed.set(d.mytime, d);
});

var res = scale.map(function(t) {
var ft = timeformat(t);
return {
mytime: ft,
value: (hashed.has(ft)) ? hashed.get(ft).value : null
};
});

var $table = $('<table></table>');
res.forEach(function(d) {
$table.append('<tr><td>'+d.mytime+'</td><td>'+d.value+'</td></tr>')
});
$('body').append($table);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

关于javascript - 比较 D3.js 中的两个时间对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com