2013-04-09 127 views
2

我想繪製使用谷歌API的折線圖。data.addRows不能在谷歌圖表工作

但圖表沒有繪製,我可以確定data.addRows(行)中的一些錯誤;因爲此data.addRows行後面的警報不會彈出。

如果我在data.addRows(rows)之前給出警報,彈出警報。

我不知道這裏出了什麼問題。任何幫助深表感謝。

<script type="text/javascript"> 
     google.load('visualization', '1.0', {'packages':['corechart']}); 

     function drawLineChart() { 
      var date = 0; 
      var status = 0; 
      var rows = new Array(); 
      var data = new google.visualization.DataTable(); 
      var $attendance_date = new Array(); 
      var $attendance_status = new Array(); 

      $attendance_date[0] = '2004'; 
      $attendance_date[1] = '2005'; 
      $attendance_date[2] = '2006'; 

      $attendance_status[0] = 1; 
      $attendance_status[1] = 3; 
      $attendance_status[2] = 5; 

      rows.push(['Year', 'Sales']); 
      for(var i = 0; i < 3 ; i++) { 
      date = $attendance_date[i]; 
      status = parseInt($attendance_status[i]); 
      rows.push([date, status]); 
      } 

      data.addColumn('string', 'Subject'); 
      // alert(rows); 
      data.addRows(rows); 
      alert(rows); 

      var options = { 
      title: 'My Chart', 
      'width':900, 
      'height':320, 
      'backgroundColor': '#d9d9e4' 
      }; 

      var chart = new google.visualization.LineChart(document.getElementById('chart_attendance_div')); 
      chart.draw(data, options); 
     } 
</script> 

回答

8

有一些問題與你在做什麼,讓我們一一瀏覽。

首先,使用addRowaddColumn時,可以使用addColumn命令定義標題。所以,你可以刪除以下行:

rows.push(['Year', 'Sales']); 

其次,要定義一個列作爲「主題」,當你真的有兩列(「年」和「銷售」)。你應該定義兩列:

data.addColumn('string', 'Year'); 
data.addColumn('number', 'Sales'); 

一旦你這樣做,你可以使用addRows命令與最終(工作)代碼:

function drawVisualization() { 
    var date = 0; 
    var status = 0; 
    var rows = new Array(); 
    var data = new google.visualization.DataTable(); 
    var $attendance_date = new Array(); 
    var $attendance_status = new Array(); 

    $attendance_date[0] = '2004'; 
    $attendance_date[1] = '2005'; 
    $attendance_date[2] = '2006'; 

    $attendance_status[0] = 1; 
    $attendance_status[1] = 3; 
    $attendance_status[2] = 5; 

    // This line is commented out because it should be deleted 
    // rows.push(['Year', 'Sales']); 
    for(var i = 0; i < 3 ; i++) { 
    date = $attendance_date[i]; 
    status = parseInt($attendance_status[i]); 
    rows.push([date, status]); 
    } 
    // This line is commented out because it should be deleted 
    // data.addColumn('string', 'Subject'); 

    // These lines should be added for column headers 
    data.addColumn('string', 'Year'); 
    data.addColumn('number', 'Sales'); 

    // alert(rows); 
    data.addRows(rows); 
    // alert(rows); 

    var options = { 
    title: 'My Chart', 
    'width':900, 
    'height':320, 
    'backgroundColor': '#d9d9e4' 
    }; 

    var chart = new google.visualization.LineChart(document.getElementById('visualization')); 
    chart.draw(data, options); 
} 

但如果你打算走這麼遠,你不妨去下一步。現在,您正在創建一個新數組,將其填充到for循環中,並將該數組傳遞給addRows命令。爲什麼不剪掉中間人?

 var data = new google.visualization.DataTable(); 
     var $attendance_date = new Array(); 
     var $attendance_status = new Array(); 

     $attendance_date[0] = '2004'; 
     $attendance_date[1] = '2005'; 
     $attendance_date[2] = '2006'; 

     $attendance_status[0] = 1; 
     $attendance_status[1] = 3; 
     $attendance_status[2] = 5; 

     data.addColumn('string', 'Year'); 
     data.addColumn('number', 'Sales'); 

     for(var i = 0; i < 3 ; i++) { 
     data.addRow([$attendance_date[i], parseInt($attendance_status[i])]) 
     } 

Presto,你的代碼更簡單,你跳過了一個步驟。