Reputation: 89
I'm trying to plot D3 timeseries by referring : http://mcaule.github.io/d3-timeseries/
which takes Date in this format : 'new Date('2013-01-01')
Now, I have following array:
var data =[{
"time": "2017-06-23T23:37:20-07:00",
"value": 25.189767114257663
},
{
"time": "2017-06-23T23:37:30-07:00",
"value": 24.637318860009692
},
{
"time": "2017-06-23T23:37:40-07:00",
"value": 24.896462306379043
},
{
"time": "2017-06-23T23:37:50-07:00",
"value": 24.348000192323468
}]
I need to give this array as input to the d3 timeseries function. But I'm unable to plot anything as above time is not a Date object. How do I append or convert that time string to date object. Because I absolutely need to have that new Date thing otherwise that function is not at all executing.
I tried to convert time into date object using:
data.map(function(ele) {
ele.time = new Date(ele.time);
});
So, this thing appended Zone to all the times. When I tried to give this array as input to d3 function, it doesn't work.
Upvotes: 1
Views: 810
Reputation: 724
You can map the timestamp to a date object by iterating over data and assigning a new field called date.
data.forEach(function(elem, index, arr) {
arr[index]['date'] = new Date(
arr[index]['timestamp'].split('T')[0]);
});
Pretty basic string splitting, and there are probably more elegant solutions, but here's a start.
Upvotes: 1