1

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.

6
  • you can preprocess data, convert all time string to date object, and use the result new data object as the input to d3. Commented Jun 25, 2017 at 4:49
  • no, you've converted it to a date string - data.forEach(item => item.time = new Date(item.time)) will give you Date objects in the time property Commented Jun 25, 2017 at 4:51
  • I've tried doing some thing like above. Please check edit. Commented Jun 25, 2017 at 4:54
  • yeah ... map returns a new array, which you're ignoring ... so you may as well use forEach instead of map ... Commented Jun 25, 2017 at 4:56
  • 1
    it doesn't work is an often used phrase on stack overflow that is redundant (why else would you be asking a question) and meaningless by itself - do you get errors in the (developer tools) console? what do you observe? how does it differ from what you expect? Commented Jun 25, 2017 at 4:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

can you tell me what is wrong with what I did, as I'm also converting it into a date object?
Would you mind sharing your code? I just edited my answer, so it might be more helpful now.
what you are doing will give same result as what I'm doing.As given in the link, I need something of this form: [{date:new Date('2013-01-01'),n:120,n3:124,ci_up:130,ci_down:118} ...]
its given in question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.