5

我正在尝试使用 Google 图表生成甘特图,并在使用现有 php 代码进行相应编码后,我得到一个空白 html。请帮助我成功显示图表。

  <html>
  <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 and the gantt chart package.
     google.load("visualization", "1", {packages: ["timeline"]});

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

    function drawChart() {
       $.ajax({
      url: "http://localhost:8080/index1.php",
      dataType:"json",
      async: false ,
      success: function(data) {
           jsonData = data;
         }
          });

      // 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.Timeline(document.getElementById('chart_div'));
      chart.draw(data, options);
    }

    </script>
  </head>

  <body>
    <div id="chart_div" style="width: 900px; height: 200px;></div>
  </body>
</html>
4

3 回答 3

5

你错过了:“关于 div 样式

<div id="chart_div" style="width: 900px; height: 200px;></div>

没有定义,div所以JS找不到它。

失败者getElementById

于 2014-03-23T13:14:33.723 回答
2

我认为你有data 格式问题试试这个

  /*
   // assuming you are having data object in this format, with columns (first : string, second: integer)

  data = [
      ['Mushrooms', 3],
      ['Onions', 1],
      ['Olives', 1],
      ['Zucchini', 1],
      ['Pepperoni', 2]
    ]
  */  

  // Load the Visualization API and the piechart package.
  google.load('visualization', '1', {'packages':['corechart']});

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


  function get_gchart_data(data) {
    var g_data = new google.visualization.DataTable();
    g_data.addColumn('string', 'Topping');
    g_data.addColumn('number', 'Slices');
    g_data.addRows(data);
    return g_data;
  }

  function drawChart() {
      $.ajax({
        url: "localhost:8080/index1.php",
        dataType:"json",
        async: false ,
        success: function(data) {
          var g_data = get_gchart_data(data);

          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.Timeline(document.getElementById('chart_div'));
          chart.draw(g_data, {width: 500, height: 240});
        }
      })
    };
于 2013-10-27T10:43:49.413 回答
0

试试这个,我认为你jsonData在错误的地方访问。

<script type="text/javascript">
  $(function() {
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

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

    function drawChart() {
      $.ajax({
        url: "localhost:8080/index1.php",
        dataType:"json",
        async: false ,
        success: function(data) {
         jsonData = data;
          // 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.Timeline(document.getElementById('chart_div'));
          chart.draw(data, {width: 500, height: 240});
        }
      })  
    };
  });
</script>
于 2013-10-27T08:14:59.693 回答