JSON inline Date converting

28 views Asked by At

I have D3 data graphic like here: https://www.metricsgraphicsjs.org/examples.htm#lines with example code

MG.data_graphic({
title: "Singleton",
description: "Handling a solitary data point.",
data: [{'date': new Date('2015-03-05T21:00:00Z'), 'value': 12000}],
width: 600,
height: 200,
right: 40,
target: '#singleton'
});

and difference is that I store data into database. But I have problem with displaying dates. My JSON data (pulled from MySQL) looks like this:

var data1 = [
 [
    {
     "date": "2018-04-28T13:42:55Z",
     "value": 20.8,
     "added": "2018-04-28T13:42:55Z"
    }
 ],
 [
    {
     "date": "2018-04-28T13:42:55Z",
     "value": 22.8,
     "added": "2018-04-28T13:42:55Z"
    }
 ],
 [
    {
    "date": "2018-04-28T13:42:55Z",
    "value": 23.9,
    "added": "2018-04-28T13:42:55Z"
    }
 ],
 [
    {
    "date": "2018-04-28T13:42:55Z",
    "value": 21.3,
    "added": "2018-04-28T13:42:55Z"
    }
 ]
]; 

When I try to display that data, it does not work, shows me errors like <path> attribute d: Expected number, "MNaN,86.570057581…" But on other hand, if I set another variable for example

 var data2 = [
               [
                 {'date': new Date('2015-03-05T21:07:00Z'), 'value': 12000},
                 {'date': new Date('2015-03-05T21:10:00Z'), 'value': 5500},
                 {'date': new Date('2015-03-05T21:15:00Z'), 'value': 1000}
                ],
                [
                 {'date': new Date('2015-03-05T21:05:00Z'), 'value': 18000},
                 {'date': new Date('2015-03-05T21:07:00Z'), 'value': 15000},
                 {'date': new Date('2015-03-05T21:15:00Z'), 'value': 7000}
                ]               
             ];

This second example works and I was wondering what this "new Date()" does to data. How can I "fix" first data1 array and store it in another variable with correct dates? - I did try this type of date: Date Sat Apr 28 2018 13:56:55 GMT+0000 (UTC) but that did not help, did not work. Like this:

[
    {'date': 'Apr 28 2018 13:57:55 GMT+0000 (UTC)', 'value': 18000},
    {'date': 'Apr 28 2018 13:58:55 GMT+0000 (UTC)', 'value': 15000},
    {'date': 'Apr 28 2018 13:59:55 GMT+0000 (UTC)', 'value': 7000}
]

This did not work. Does "new Date()" converts date and time to integer or string or something else?

I just want to find a way to change data1 variable and convert its dates to proper values to be able to show them on graph.

Thanks guys!

0

There are 0 answers