Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Error: Container is not defined

I'm trying to generate a gantt chart using Google charts and after coding accordingly with the existing php code, I m getting a blank html. Please help me on this to display the chart successfully.

  <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>
like image 800
Arun Kumar Avatar asked Oct 27 '13 07:10

Arun Kumar


2 Answers

You are missing : " on the div style

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

The div is not defined, so the JS cant find it.

The getElementById failed.

like image 170
gil meyerstein Avatar answered Oct 24 '22 00:10

gil meyerstein


I think you are having data format issue, try this

  /*
   // 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});
        }
      })
    };
like image 44
Raghvendra Parashar Avatar answered Oct 23 '22 23:10

Raghvendra Parashar