简体   繁体   English

Google图表 - 图表为空白

[英]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. 使用mysql / php / js尝试显示曲线图 - 当前图表正在显示,但它是空白的。

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. downloadURL是一种从我的数据库中检索信息的方法 - 寻找高度并简单地绘制它。 The method definitely works ok as I'm also using it for adding markers to a google map... 该方法绝对可行,因为我也用它来添加标记到谷歌地图......

I believe you're either using arrayToDataTable wrong or your input is formatted wrong. 我相信你要么使用arrayToDataTable错误,要么输入格式错误。

Looking at documentation for arrayToDataTable the array should contain your column names etc, and you shouldn't be using data.addCoulmn() at all. 查看arrayToDataTable文档,该数组应包含您的列名等,您根本不应该使用data.addCoulmn()

Correct format of array should be: 正确的数组格式应该是:

array = [
    [{label:'ID', type:'number'},{label:'Altitude',type:'number'}],
    [1,15],
    [2,23]
]

There are several issues(additionally to the answer by Henrik) 有几个问题(除了Henrik的回答)

  1. you must draw the chart in the callback of downLoadUrl 你必须在downLoadUrl的回调中绘制图表
  2. getAttribute is a method, it must be markers[i].getAttribute("alt") getAttribute是一个方法,它必须是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) 你必须将高度转换为数字,目前它是一个字符串(xml-属性总是字符串类型)
  4. you are using push the wrong way, for each marker you add 2 rows, 1 for the id an 1 for the altitude 你正在使用push错方式,为每个标记添加2行,1为id,1为高度

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);   
  });
}

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

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