Google时间轴和一系列日期 [英] Google Timelines and an Array of Dates

查看:280
本文介绍了Google时间轴和一系列日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个数组,并且我的日期和国家一起(它从我的数据库中出来)。如何基于这些数据创建我的AnnotatedTimeLine?我收到错误第一列必须包含日期,或日期和时间与我目前的设置

I am creating an array and I have Dates along with Country (It's coming out of my database). How do I create my AnnotatedTimeLine based off this data? I get the error "First column must contain date, or date and time" with my current set up

创建数组

while($r = mysqli_fetch_assoc($sth)) {
$callDate = strtotime($r[CALLDATEEST]);
$convertedCallDate = date("Y-m-d",$callDate);
$pieChartArray .= "[$convertedCallDate, '$r[COUNTRY]'],\n";
}
$pieChartArray  = substr(trim($pieChartArray), 0, -1);
?>

函数

    var data = google.visualization.arrayToDataTable([
            ['Call times', 'Country'],
            <?php echo $pieChartArray; ?>
            ]);


推荐答案

您需要输入日期作为日期对象。最简单的方法是对数据使用DataTable JSON结构:

You need to input the dates as date objects. The easiest way to do that is to use the DataTable JSON structure for your data:

$data = array(
    'cols' => array(
        array('type' => 'date', 'label' => 'Call times'),
        array('type' => 'number', 'label' => 'Country')
    ),
    'rows' => array()
);
while($r = mysqli_fetch_assoc($sth)) {
    $callDate = strtotime($r[CALLDATEEST]);
    $year = (int) date("Y",$callDate);
    $month = ((int) date("m",$callDate)) - 1; // convert months to javascript's 0-indexed months
    $day = (int) date("d",$callDate);

    $data['rows'][] = array('c' => array(
        array('v' => "Date($year, $month, $day)"),
        array('v' => $r[COUNTRY])
    ));
}

然后在javascript中:

then in javascript:

var data = new google.visualization.DataTable(<?php echo json_encode($data, JSON_NUMERIC_CHECK); ?>);

这篇关于Google时间轴和一系列日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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