Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a Date value from JSON to Google Visualization API

is there a way to retrieve date value from JSON in Google Visualization API? Here is the snipplet for playground please copy the code below into it

When you run the code you won't have anything in result. you should remove the quotes from the date value I marked with comment in order to retrieve result.

function drawVisualization() {
var JSONObject = {
cols:
    [
        {id: 'header1', label: 'Header1', type: 'string'},
        {id: 'header2', label: 'Header2', type: 'date'}
    ],
rows: 
    [
        {
            c:
                [
                    {v: 'Value1'}, 
                    {v: "new Date(2010, 3, 28)"}  // <= This is the format I receive from WebService
                ]
        },
        {
            c:
                [
                    {v: 'Value2'},
                    {v: new Date(2010, 3, 28)} // <=This is the format Google API accepts
                ]
        }
    ]
};

var data = new google.visualization.DataTable(JSONObject, 0.5);

visualization = new google.visualization.Table(document.getElementById('table'));
visualization.draw(data, {'allowHtml': true});
}
like image 850
cilerler Avatar asked Sep 03 '10 17:09

cilerler


1 Answers

I just ran into this problem myself, so I thought I'd paste the answer from the google api documentation, located here http://code.google.com/apis/chart/interactive/docs/dev/implementing_data_source.html#jsondatatable

"JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based."

like image 134
Jan Drewniak Avatar answered Oct 22 '22 18:10

Jan Drewniak