如何将日期传递到谷歌图表 [英] how to pass date into google charts

查看:131
本文介绍了如何将日期传递到谷歌图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的行,

  [[日期{2013年12月18日00:00:00 GMT + 0530(印度标准时间)},0,null,19 more ...],
[Date {Tue Dec 17 2013 00:00:00 GMT + 0530(印度标准时间)},0,null,19更多...],
[日期{Mon Dec 16 2013 00:00:00 GMT + 0530(印度标准时间)},0,null,19 more ...],
... 4更多...]]

早些时候在上面的行中的日期是这样的,

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

等等...所以,我只是转换成以上格式。但是,如何将它传递到谷歌图表?从现在起,我已经使用过:

  googledata.addRows(chartdata)

其中chartdata包含上面的行,并且我也尝试过:

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

我有这样的行,

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

forloop

{

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




$ b共有7个项目在那里, 。



帮助我

解决方案

arrayToDataTable 方法不支持输入日期,所以你不能使用它。您的日期需要以两种方式之一输入,具体取决于您如何加载数据。如果您手动构建DataTable,如下所示:

  var googledata = new google.visualization.DataTable(); 
googledata.addColumn('date','Date');
//添加其他列

然后您需要输入日期作为Date对象。举例来说,此日期日期{Wed Dec 2013 2013 00:00:00 GMT + 0530(印度标准时间)} 将被输入为 new日期(2013,11,18)(月份为零索引,所以12月份为11日,而不是12日)。如果您是从JSON字符串构建DataTable,如下所示:

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

然后您需要为日期使用自定义字符串格式。它看起来与标准的Date对象相似,但是是一个字符串,并且不包含 new 关键字;您的示例日期将输入为Date(2013,11,18)





您也可以使用DataView将日期字符串转换为Date对象。您需要将日期字符串解析为其组成部分并使用它们构造Date对象。假设DataTable的第0列中的日期字符串格式为'yyyy-MM-dd',那么您将如何将其转换为Date对象:

  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; //减1转换为JavaScript的0索引月
var day = dateArray [2]; //第三天是第三次拆分
返回新日期(年,月,日);
}
},/ *列索引其余部分的列表* /]);


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

解决方案

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 */]);

这篇关于如何将日期传递到谷歌图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆