我的谷歌图表有问题。我已经为这个问题奋斗了两天,我似乎无法找到解决方案。
我想创建一个监控系统,在那里我可以看到改变其中有两行的折线图。我正在JS代码中生成数据(至少目前为止)。
我不知道我做错了什么。我可以使用非常简单的静态示例使图表可见,但是一旦我尝试将某些数据添加到图表中,它就会停止绘制。
当前版本的错误是:
“未捕获的TypeError:无法读取未定义的属性'长度'”
但这是第n次我尝试了向图表添加更多数据的不同方法,以及第n次不同的错误。 因此,如果有人知道如何向谷歌图表添加数据,那么听到它会很棒。
提前致谢。
setInterval(function() {
//this is how I create a random data and call the drawChart function.
var temperature = (Math.random() * (35 - 30) + 30).toFixed(1),
humidity = (Math.random() * (40 - 15) + 15).toFixed(1),
timestamp = new Date();
google.charts.setOnLoadCallback(drawChart(timestamp, temperature, humidity));
}, 5000);
function drawChart(timestamp, temperature, humidity) {
//Cast temperature to proper format for the chart
//This works...don't understand why but it does.
temperature = parseFloat("temperature");
humidity = parseFloat("humidity");
// Define the chart to be drawn.
var data = new google.visualization.DataTable();
if (data[0].length == 0) {
var datetime = new Date();
data.addColumn('datetime', 'Time');
data.addColumn('number', 'Temperature');
data.addColumn('number', 'Humidity');
data.addRow(datetime, 0, 0);
var options = {
'title' : 'Temperature and Humidity',
hAxis: {title: 'Time'},
vAxis: {title: 'Temperature and Humidity'},
'width':550,
'height':400
};
}
data.addRow(timestamp, temperature, humidity);
// Instantiate and draw the chart.
var chart = new google.visualization.LineChart(document.getElementById('monitor-chart'));
chart.draw(data, options);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Projekti 1, sivusto</title>
<!-- Latest BOOTSTRAP minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest BOOTSTRAP JC-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- JS Generate new data Class -->
<script type="text/javascript" src="js/generate_data.js"></script>
<!-- GOOGLE CHARTS visualization -->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {packages: ['corechart','line']});
</script>
<!-- My css file -->
<link rel="stylesheet" type="text/css" href="css/ulkoasu.css"/>
</head>
<body>
<article>
<h1>This is Monitor content</h1>
<div id="monitor-data"></div>
<div id="monitor-chart"></div>
</article>
</body>
</html>
答案 0 :(得分:3)
首先, setOnLoadCallback
的参数应该是对函数的引用,如下所示......
google.charts.setOnLoadCallback(drawChart);
不是函数调用的结果 ...
google.charts.setOnLoadCallback(drawChart(timestamp, temperature, humidity));
和 setOnLoadCallback
通常每页加载仅使用一次,
在第一次回调完成后,不再需要此方法
无论如何,callback
都可以添加到load
语句中
setOnLoadCallback
并非真正需要
其他错误包括使用addRow
- &gt; data.addRow(timestamp, temperature, humidity);
列值应该在数组中传递......
data.addRow([timestamp, temperature, humidity]);
建议类似以下设置 - 当回叫触发时,
创建所有资源,例如图表,选项和数据表
drawChart
仅用于添加一行并绘制图表
请参阅以下工作代码段...
google.charts.load('current', {
callback: function () {
var chart = new google.visualization.LineChart(document.getElementById('monitor-chart'));
var options = {'title' : 'Temperature and Humidity',
animation: {
duration: 1000,
easing: 'out',
startup: true
},
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Temperature and Humidity'
},
height: 400
};
var data = new google.visualization.DataTable();
data.addColumn('datetime', 'Time');
data.addColumn('number', 'Temperature');
data.addColumn('number', 'Humidity');
var formatDate = new google.visualization.DateFormat({pattern: 'hh:mm:ss'});
var formatNumber = new google.visualization.NumberFormat({pattern: '#,##0.0'});
getTemp();
setInterval(getTemp, 5000);
function getTemp() {
var temperature = (Math.random() * (35 - 30) + 30);
var humidity = (Math.random() * (40 - 15) + 15);
var timestamp = new Date();
drawChart(timestamp, temperature, humidity);
}
function drawChart(timestamp, temperature, humidity) {
data.addRow([timestamp, temperature, humidity]);
formatDate.format(data, 0);
formatNumber.format(data, 1);
formatNumber.format(data, 2);
chart.draw(data, options);
}
},
packages:['corechart']
});
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="monitor-chart"></div>
&#13;