dongyou7292 2014-07-16 16:48
浏览 50
已采纳

表没有使用谷歌图表的列

I have looked through many posts and tried their suggestions. A new problem arises each time though.

Objective: To create a Google line chart taking data from MYSQL table and encoding it in JSON. From there, I attempt to use an html file to display the chart.

someFile.php:

<?php

//connect to the database 
$username = ...;
$password = ...;
$hostname = ...;
$database = ...;

$conn = mysqli_connect($hostname, $username)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";  

//Select database
$db_found = mysqli_select_db($conn,$database);

if($db_found) {
   print "Database Found<br><br>";
}
else{
   print "Database NOT Found<br>";
}


$sql = mysqli_query($conn, 'SELECT * FROM Travel_Test');

if (!$sql) {
   die("Error running $sql: " . mysql_error());
}


$results = array(
'cols' => array (
    array('label' => 'travel_time_date', 'type' => 'datetime'),
    array('label' => 'travel_time_duration', 'type' => 'number')
),
'rows' => array()
);
while($row = mysqli_fetch_assoc($sql))
{

   //date assumes "yyyy - mm -- dd" format
   $dateArr = explode('-', $row['travel_time_date']);
   $year = (int) $dateArr[0];
   $month = (int) $dateArr[1]; //subtract 1 to make compatible with javascript months
   $day = (int) $dateArr[2];

   //time assumes "hh:mm:ss" format
   $timeArr = explode(':', $row['travel_time_timestamp']);
   $hour = (int) $timeArr[0];
   $minute = (int) $timeArr[1];
   $second = (int) $timeArr[2];

   $results['rows'][] = array('c' => array(
      array('v' => "travel_time_date($year, $month, $day, $hour, $minute, $second)"),
      array('v' => $row['travel_time_duration'])
   ));      
}

$json = json_encode($results,  JSON_NUMERIC_CHECK );
echo $json;
//header("Access-Control-Allow-Origin: *");
?>

JSON output:

{"cols":[{"label":"travel_time_date","type":"datetime"},
{"label":"travel_time_duration","type":"number"}],
"rows":[{"c":
[{"v":"travel_time_date(2014, 2, 1, 1, 30, 30)"},{"v":55}]},{"c": 
[{"v":"travel_time_date(2014, 2, 1, 2, 30, 30)"},{"v":80}]},{"c":
[{"v":"travel_time_date(2014, 2, 1, 3, 30, 30)"},{"v":60}]},{"c":
[{"v":"travel_time_date(2014, 2, 1, 4, 30, 30)"},{"v":120}]}]}

anotherFile.html:

<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">


    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {
        var jsonData = $.ajax({
        url: "http:///Volumes/OS%20X%20Mountain%20Lion/Applications/XAMPP/xamppfiles/htdocs/traveltrak/travelGraphData.php",
        dataType:"json",
        async: false
        }).responseText;
        //success: function (jsonData) {
            //Create data table out of JSON data loaded from server
            var data = new google.visualization.DataTable(jsonData);

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
            chart.draw(data, {width: 400, height: 240});
        //}
    //});
    }
    </script>
 </head>

 <body>
 <div id="chart_div"></div>
 </body>
</html>

When I tried to preview the page, I checked the developer's console. It states no issues. The error is when the page says: Table has no columns. Any suggestions from here please?

  • 写回答

1条回答 默认 最新

  • doumu5023 2014-07-16 18:14
    关注

    Your dates are not input correctly; the format is 'Date(year, month, day, hours, minutes, seconds)'. You need to replace "travel_time_date" with "Date":

    $results['rows'][] = array('c' => array(
        array('v' => "Date($year, $month, $day, $hour, $minute, $second)"),
        array('v' => $row['travel_time_duration'])
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题