2014-03-18 28 views
0

我遇到了一些问题,现在我已经工作了几天了,而且我真的坚持解决这个问题。问题是,我正在制作一个折线图,显示一段时间内数值为PH,氯气和温度的日期和时间。我不认为我已经格式化jsoncode正确的,因为它显示在这里:https://developers.google.com/chart/interactive/docs/reference#dataparamGoogle图表时间/日期格式问题

当我赞同我的jsoncode,它看起来像这样:

{"cols":[{"label":"Time","type":"date"},{"label":"Date","type":"date"},  {"label":"PH","type":"number"},{"label":"temperature","type":"number"}, {"label":"Chlorine","type":"number"}],"rows":[{"c":[{"v":"Date(2014, 2, 17, 15, 03, 14)"}, {"v":"7.00"},{"v":"34.00"},{"v":"3.40"}]}]} 

我的数据库看起来是这样的:

enter image description here

代码:

<?php 

    $con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!"); 
    mysql_select_db("chart", $con); 

    $sth = mysql_query("SELECT * FROM googlechart"); 

    $rows = array(); 
    //flag is not needed 
    $flag = true; 
    $table = array(); 

    $table['cols'] = array(

    array('label' => 'Time', 'type' => 'date'), 
    array('label' => 'Date', 'type' => 'date'), 
    array('label' => 'PH',  'type' => 'number'), 
    array('label' => 'temperature','type' => 'number'), 
    array('label' => 'Chlorine','type' => 'number'), 
    ); 

    $rows = array(); 

    while($r = mysql_fetch_assoc($sth)) { 

    // assumes dates are in the format "yyyy-MM-dd" 
    $dateString = $r['Date']; 
    $dateArray = explode('-', $dateString); 
    $year = $dateArray[0]; 
    $month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months 
    $day = $dateArray[2]; 

    // assumes time is in the format "hh:mm:ss" 
    $timeString = $r['Time']; 
    $timeArray = explode(':', $timeString); 
    $hours = $timeArray[0]; 
    $minutes = $timeArray[1]; 
    $seconds = $timeArray[2]; 
    echo $dateString."<br>"; 
    echo $timeString."<br>"; 
    $temp = array(); 
    $temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)"); 
    $temp[] = array('v' => (string) $r['PH']); 
    $temp[] = array('v' => (string) $r['temperature']); 
    $temp[] = array('v' => (string) $r['Chlorine']); 

    $rows[] = array('c' => $temp); 

    } 

    $table['rows'] = $rows; 
    $jsonTable = json_encode($table); 
    echo $jsonTable; 

?> 


<html> 
    <head> 

    <script type="text/javascript" src="https://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
     google.load("visualization", "1", {packages:["corechart"]}); 
     google.setOnLoadCallback(drawChart); 

     function drawChart() { 
     var data = new google.visualization.DataTable(<?=$jsonTable?>); 



     var options = { 
     /*width: 900, height: 900, */ 
      title: 'Visualization', 
      curveType: 'function', 
      legend: { position: 'bottom' }, 
      pointSize: 12, 
     vAxis: {title: "Values", titleTextStyle: {italic: false}}, 
     hAxis: {title: "Time", titleTextStyle: {italic: false}}, 
     explorer: { 
       actions: ['dragToZoom', 'rightClickToReset'], 
       axis: 'vertical' 
      } 


     }; 

     var chart = new google.visualization.LineChart(document.getElementById('chart_div')); 
     chart.draw(data, options); 


     } 
    </script> 

回答

1

T他的问题是因为Google Chart API需要JavaScript Date对象。你给图表的是一个字符串对象,尽管它的值看起来有Date()。

您需要在属性分配不带引号,并因为它需要Date对象,它会是这样的:

"v": new Date("2014-02-17 15:03:14") 
0
$temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)"); 

会象下面这样:

$temp[] = array('v' => 'Date('.date('Y',strtotime($r['Date'])).','.(date('n',strtotime($r['Date'])) - 1).','.date('d',strtotime($r['Date'])).','.date('H',strtotime($r['Time'])).','.date('i',strtotime($r['Time'])).','.date('s',strtotime($r['Time'])).')');