how to pass date into google charts

我们两清 提交于 2019-12-25 10:01:09

问题


I have a row like this,

[[[Date {Wed Dec 18 2013 00:00:00 GMT+0530 (India Standard Time)}, 0, null, 19 more...], 
  [Date {Tue Dec 17 2013 00:00:00 GMT+0530 (India Standard Time)}, 0, null, 19 more...], 
  [Date {Mon Dec 16 2013 00:00:00 GMT+0530 (India Standard Time)}, 0, null, 19 more...], 
  ...4 more...]]

Earlier the date in the above line was like this,

['2013-11-18', 0, null, null, 0, null, null, 0, null, null, 0, null, null, 0, null, null, 0, null, null, 0, null, null] 

and so on... So, I just converted into above format. But, how to pass it into google charts? Since now,I have used:

googledata.addRows(chartdata)

Where the chartdata contains above lines and I have also tried:

var googledata = google.visualization.arrayToDataTable(chartdata); 

I have rows like this,

googledata.addColumn('date', 'Date');

forloop 

{

googledata.addColumn('number', items[i]);
googledata.addColumn({type:'string', role:'annotation'}); 
googledata.addColumn({type:'string', role:'annotationText'}); 

}

Total 7 items are there and am generating that using for loop.

help me


回答1:


The arrayToDataTable method does not support inputting dates, so you can't use that. Your dates need to be input in one of two ways, depending on how you are loading the data. If you are manually building the DataTable, like this:

var googledata = new google.visualization.DataTable();
googledata.addColumn('date', 'Date');
// add other columns

then you need to input your dates as Date objects. As an example, this date Date {Wed Dec 18 2013 00:00:00 GMT+0530 (India Standard Time)} would be input as new Date(2013, 11, 18) (months are zero-indexed, so December is 11 not 12). If you are constructing your DataTable from a JSON string, like this:

var googledata = new google.visualization.DataTable(jsonData);

then you need to use a custom string format for the dates. It looks similar to a standard Date object, but is a string and does not contain the new keyword; your example date would be input as "Date(2013, 11, 18)".

[Edit - how to use a DataView to convert date strings into Date objects]

You can also use a DataView to convert date strings into Date objects. You need to parse the date string into its component parts and use those to construct a Date object. Assuming a date string in the format 'yyyy-MM-dd' in column 0 of your DataTable, this is how you would convert it to a Date object:

var view = new google.visualization.DataView(googledata);
view.setColumns([{
    type: 'date',
    label: data.getColumnLabel(0),
    calc: function (dt, row) {
        var dateArray = dt.getValue(row, 0).split('-');
        var year = dateArray[0];
        var month = dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
        var day = dateArray[2]; //The day is the third split
        return new Date(year, month, day);
    }
}, /* list of the rest of your column indices */]);


来源:https://stackoverflow.com/questions/20144848/how-to-pass-date-into-google-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!