轴#0的数据列不能是字符串类型?

时间:2013-08-09 13:32:30

标签: json google-visualization

我是Google图表的新手。我知道很多次都会问过这个问题。但所有那些安静似乎并没有解决我的问题。所以我发布了迄今为止尝试过的最新内容。

<?php
    $dbhost="localhost";
    $dblogin="root";
    $dbpwd="";
    $dbname="myDB";

    $db =  mysql_connect($dbhost,$dblogin,$dbpwd);
    mysql_select_db($dbname);    

    $day = date('d');
    $month = date('m');
    $lastMonth = (string)($month-1);    
    $lastMonth = strlen($month - 1) == 1? '0'.$lastMonth : $lastMonth;

    $SQLString = "SELECT    
            count(analytics.day) as counts,
            analytics.day, month,
            date FROM analytics  
            WHERE year = '2012' AND month = '$month' 
            OR (month = '$lastMonth' and day > '$day') 
            GROUP BY day, month 
            ORDER BY date asc";     

    $result = mysql_query($SQLString);    
    $num = mysql_num_rows($result);   

# set heading   
    $data[0] = array('day','counts');       
    for ($i=1; $i<($num+1); $i++)
    {
        $data[$i] = array(substr(mysql_result($result, $i-1, "date"), 5, 5),
            (int) mysql_result($result, $i-1, "counts"));
    }   
    echo json_encode($data);
    mysql_close($db);
?>

这是数据库:

Database

这是我的.html文件

<html>
<head>
    <title></title>
    <!-- Load jQuery -->
    <script language="javascript" type="text/javascript" 
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js">
    </script>
    <!-- Load Google JSAPI -->
    <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 jsonData = $.ajax({
                url: "php/tracker.php",
                dataType: "json",
                async: false
            }).responseText;

            var obj = jQuery.parseJSON(jsonData);
            var data = google.visualization.arrayToDataTable(obj);

            var options = {
                title: 'yet to be decided'
            };

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

    </script>
</head>
<body>
    <div id="chart_div" style="width: 900px; height: 500px;">
    </div>
</body>
</html>

0 个答案:

没有答案