【问题标题】:Using timeline Google Chart API in PHP - Date/Time formatting issues在 PHP 中使用时间轴 Google Chart API - 日期/时间格式问题
【发布时间】:2016-07-14 00:06:49
【问题描述】:

我正在尝试实现 Google 的 Timeline API https://developers.google.com/chart/interactive/docs/gallery/timeline#controlling-the-colors

我的日期格式有问题,因为我不确定如何将我的时间格式存储/转换为时间线图的正确格式。

我的数据库如下所示:

我正在尝试输出一个如下所示的图表:(这是一个硬编码示例)

我当前的代码如下所示:

    <?php
$connect=mysqli_connect("localhost","root","","smartcinema");
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT a.screen_name, m.title, s.show_startime, s.show_endtime FROM timetable AS t INNER JOIN showing AS s ON s.showing_id = t.showing_id JOIN auditorium AS a ON a.screen_id = t.screen_id JOIN movies AS m ON m.movie_id = t.movie_id";
$qresult = mysqli_query($connect,$query);
$rows = array();
$table = array();




$table['cols'] = array (
  array('id' => 'Screen', 'type' => 'string'),
  array('id' => 'Movie', 'type' => 'string'),
  array('id' => 'Start time', 'type' => 'date'),
  array('id' => 'End time', 'type' => 'date')
  );

while($res = mysqli_fetch_assoc($qresult)){
  $result[] = $res;
}

foreach ($result as $r) {

  $temp = array();
  $temp[] = array('v' => $r['screen_name']);
  $temp[] = array('v' => $r['title']);
  $temp[] = array('v' => 'new Date(0,0,0,'.date('H',strtotime($r['show_startime'])).','.date('i',strtotime($r['show_startime'])).','.date('s',strtotime($r['show_startime'])).')');
  $temp[] = array('v' => 'new Date(0,0,0,'.date('H',strtotime($r['show_endtime'])).','.date('i',strtotime($r['show_endtime'])).','.date('s',strtotime($r['show_endtime'])).')');
  $rows[] = array('c' => $temp);

}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
print_r($jsonTable);
?>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
  google.charts.load("current", {packages:["timeline"]});
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
    var container = document.getElementById('test');
    var chart = new google.visualization.Timeline(container);


    var options = {
      timeline: { colorByRowLabel: true },
      backgroundColor: '#ffd'
    };

    chart.draw(dataTable, options);
  }
</script>

<div id="test" ></div>

【问题讨论】:

标签: javascript php mysql google-api google-visualization


【解决方案1】:

当您print_r($jsonTable); 时——它与以下示例有何不同?

google.charts.load('current', {
  callback: drawChart,
  packages: ['timeline']
});

function drawChart() {
  var dataTable = new google.visualization.DataTable({
    cols: [
      {id: 'Screen', type: 'string'},
      {id: 'Movie', type: 'string'},
      {id: 'Start time', type: 'date'},
      {id: 'End time', type: 'date'}
    ],
    rows: [
      {c:[{v: 'Screen 1'}, {v: 'Batman v Superman: Dawn of Justice'}, {v: new Date(0, 0, 0, 10, 0, 0)}, {v: new Date(0, 0, 0, 12, 31, 0)}]},
      {c:[{v: 'Screen 1'}, {v: 'Batman v Superman: Dawn of Justice'}, {v: new Date(0, 0, 0, 12, 51, 0)}, {v: new Date(0, 0, 0, 15, 22, 0)}]},
      {c:[{v: 'Screen 1'}, {v: 'Batman v Superman: Dawn of Justice'}, {v: new Date(0, 0, 0, 15, 42, 0)}, {v: new Date(0, 0, 0, 18, 13, 0)}]},
      {c:[{v: 'Screen 2'}, {v: 'High-Rise'}, {v: new Date(0, 0, 0, 10, 0, 0)}, {v: new Date(0, 0, 0, 11, 52, 0)}]},
      {c:[{v: 'Screen 2'}, {v: 'High-Rise'}, {v: new Date(0, 0, 0, 12, 2, 0)}, {v: new Date(0, 0, 0, 13, 54, 0)}]},
      {c:[{v: 'Screen 2'}, {v: 'High-Rise'}, {v: new Date(0, 0, 0, 14, 4, 0)}, {v: new Date(0, 0, 0, 15, 56, 0)}]},
    ]
  });

  var container = document.getElementById('example');
  var chart = new google.visualization.Timeline(container);
  chart.draw(dataTable);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="example"></div>

【讨论】:

  • 所以当我打印 json 时,它看起来像这样: {"cols":[{"id":"Screen","type":"string"},{"id":"Movie ","type":"string"},{"id":"开始时间","type":"date"},{"id":"结束时间","type":"date"}], "rows":[{"c":[{"v":"Screen 1"},{"v":"蝙蝠侠大战超人:正义黎明"},{"v":"Date(0,0, 0,10,00,00)"},
  • 我没有收到任何错误,只是一个空白屏幕。
  • 感谢您的帮助!我浏览了您的代码并将我的代码与您的代码合并,并注意到我哪里出错了
【解决方案2】:

所以我回顾了我的代码,而不是定义:

var dataTable = new google.visualization.DataTable(&lt;?php echo $jsonTable; ?&gt;);

我在定义

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

我定义的变量名不正确,所以我现在的工作代码是:

<?php
$connect=mysqli_connect("localhost","root","","smartcinema");
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$query = "SELECT a.screen_name, m.title, s.show_startime, s.show_endtime FROM timetable AS t INNER JOIN showing AS s ON s.showing_id = t.showing_id JOIN auditorium AS a ON a.screen_id = t.screen_id JOIN movies AS m ON m.movie_id = t.movie_id";
$qresult = mysqli_query($connect,$query);
$rows = array();
$table = array();




$table['cols'] = array (
  array('id' => 'Screen', 'type' => 'string'),
  array('id' => 'Movie', 'type' => 'string'),
  array('id' => 'Start time', 'type' => 'date'),
  array('id' => 'End time', 'type' => 'date')
  );

while($res = mysqli_fetch_assoc($qresult)){
  $result[] = $res;
}

foreach ($result as $r) {

  $temp = array();
  $temp[] = array('v' => $r['screen_name']);
  $temp[] = array('v' => $r['title']);
  $temp[] = array('v' => 'Date(0,0,0,'.date('H',strtotime($r['show_startime'])).','.date('i',strtotime($r['show_startime'])).','.date('s',strtotime($r['show_startime'])).')');
  $temp[] = array('v' => 'Date(0,0,0,'.date('H',strtotime($r['show_endtime'])).','.date('i',strtotime($r['show_endtime'])).','.date('s',strtotime($r['show_endtime'])).')');
  $rows[] = array('c' => $temp);

}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
?>

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {
  callback: drawChart,
  packages: ['timeline']
});

function drawChart() {
  var dataTable = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
  var container = document.getElementById('example');
  var chart = new google.visualization.Timeline(container);
  chart.draw(dataTable);
}
</script>
<div id="example" ></div>

【讨论】:

  • 工作就像一个魅力!
猜你喜欢
  • 2015-07-07
  • 2021-07-08
  • 2020-10-26
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多