簡體   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