我正在尝试使用google API绘制折线图。
但是没有绘制图表,我可以在data.addRows(行)中识别出一些错误;因为此data.addRows行之后的警报不会弹出。
如果我在data.addRows(行)之前提供警告弹出窗口。
我不知道这里出了什么问题。非常感谢任何帮助。
<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>
答案 0 :(得分:8)
你正在做的事情有很多问题,让我们一个接一个地看看。
首先,当使用addRow
和addColumn
时,您可以使用addColumn
命令定义标头。因此,您可以删除以下行:
rows.push(['Year', 'Sales']);
其次,当您确实有两列('Year'和'Sales')时,您将单个列定义为'Subject'。您应该定义两列:
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,您的代码更简单,而且您正在跳过一步。