繁体   English   中英

使用JSON时,Google图表“未定义不是函数”

[英]'undefined is not a function' with google charts when using JSON

我有一些JSON数据,想尝试Google图表。 我从这里这里使用了文档中的示例

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

    // Load the Visualization API
    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: "get-stats.php",
          dataType:"json",
          async: false
          }).responseText;

      var options = {
        title: 'Stats'
      };    
      // Create our 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, options);
    }

    </script>
  </head>

  <body>
    <!--Div that will hold the chart-->
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>

调用chart.draw()时,站点上会出现错误“未定义不是函数”。

我的JSON文件被这样加载(get-stats.php)

<?php 

$string = file_get_contents("stats.json");
echo $string;

?>

JSON的格式如下:

{
    "cols":  [
                 {
                     "id":  "date",
                     "label":  "Date",
                     "type":  "datetime"
                 },
                 {
                     "id":  "cntall",
                     "label":  "Total",
                     "type":  "number"
                 },
                 {
                     "id":  "cntpers",
                     "label":  "Pers",
                     "type":  "number"
                 }
             ],
    "rows":  [
                 {
                     "c":  [
                               {
                                   "v":  "new Date(2013, 11, 17, 9, 54, 0)"
                               },
                               {
                                   "v":  320
                               },
                               {
                                   "v":  123
                               }
                           ]
                 },
                 {
                     "c":  [
                               {
                                   "v":  "new Date(2013, 11, 17, 11, 4, 0)"
                               },
                               {
                                   "v":  300
                               },
                               {
                                   "v":  67
                               }
                           ]
                 }
              ]
}

我怀疑它与JSON有关。 所有代码均来自google示例。

var data = new google.visualization.DataTable($jsonData);

试试这个

$jsondata => json_encode($jsonData);

并放在php标签中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM