Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Charts - Graph is blank

Using mysql/php/js to try and display a curve chart - currently the chart is displaying but it is blank.

google.load('visualization', '1.0', {'packages':['corechart']});

      google.setOnLoadCallback(drawChart);


      function drawChart() {

        var graph = Array();
        downloadUrl("map.php", function (data){
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for(var i =0; i<markers.length; i++){ 
           graph.push([i], [markers[i].getAttribute["alt"]]);
        }   

    });

      var data = google.visualization.arrayToDataTable(graph);
      data.addColumn('number', 'id');
      data.addColumn('number', 'Altitude');

        var options = {
          title: 'Altitude',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }

downloadURL is a method that retrieves information from my database - looking to retrieve altitude and simply plot it. The method definitely works ok as I'm also using it for adding markers to a google map...

like image 714
fst104 Avatar asked Oct 30 '22 20:10

fst104


1 Answers

There are several issues(additionally to the answer by Henrik)

  1. you must draw the chart in the callback of downLoadUrl
  2. getAttribute is a method, it must be markers[i].getAttribute("alt")
  3. you must convert the altitude to a number, currently it's a string(xml-attributes are always of type string)
  4. you are using push the wrong way, for each marker you add 2 rows, 1 for the id an 1 for the altitude

Fixed code:

function drawChart() {

  var graph =  [];
  downloadUrl("map.php", function (data){
      var xml     = data.responseXML,
          graph   = [],
          markers = xml.documentElement.getElementsByTagName("marker"),
          //create empty datatable
          data    = new google.visualization.DataTable(),
          options = {
                      title: 'Altitude',
                      curveType: 'function',
                      legend: { position: 'bottom' }
                    },
          chart;
      for(var i = 0; i<markers.length; i++){ 
        graph.push(//a single array(row) with 2 items(columns)
                   [
                    //column 0, id(index) of the marker
                    i,
                    //column 1, alt-attribute, converted to float
                    parseFloat(markers[i].getAttribute("alt"))
                   ]
                  );
      }
      //first add columns to the datatable
      data.addColumn('number', 'id');
      data.addColumn('number', 'Altitude');
      //then add the rows
      data.addRows(graph);

      chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
      chart.draw(data, options);   
  });
}
like image 94
Dr.Molle Avatar answered Nov 03 '22 00:11

Dr.Molle