javascript 未捕获的参考错误:谷歌未定义谷歌图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17533789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught Reference Error: google is not defined google charts
提问by Pritish Shah
I am trying to load Google charts in android application. But, it throws me error that google is not defined. Below is my HTML file and JavaScript file.
我正在尝试在 android 应用程序中加载 Google 图表。但是,它向我抛出未定义 google 的错误。下面是我的 HTML 文件和 JavaScript 文件。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Patient Vitals</title>
<link href="../../common/scripts/lib/css/jquery.mobile.theme-1.1.0.css" rel="stylesheet" type="text/css" />
<link href="../../common/scripts/lib/css/jquery.mobile-1.0.1.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="../../common/scripts/lib/css/jquery.mobile.datebox.css" />
<link href="../scripts/css.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="../../common/scripts/jsapi.js" type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
</script>
<script src="../scripts/patient_vitals.js"></script>
</head>
<body>
<div class="PT_Care_vitals_swa_ofcurv_left" onclick="getPatientVitalInfo();"</div>
</body>
</html>
patient_vitals.js:
患者生命体征.js:
getPatientVitalInfo =function()
{
logInformation("Get Patient Vital Information ");
alert("Get Patient Vital info");
//code to retieve data from database
drawChart(temp);
}
drawChart = function(temp)
{
alert("Inside drawChart function");
//Convert array int DataTable
var data = google.visualization.arrayToDataTable(temp);
rowCount = data.getNumberOfRows();
// Instantiate and draw our chart, passing in some options.
chart = new google.visualization.LineChart(document.getElementById('chart_div'));
var options = {'tooltip': {'trigger':'none'}, 'year': "numeric", 'month': "numeric",'day': "numeric", 'pointSize':5};
//Function to perform select operation
//Drawing the chart in HTML page
chart.draw(data, options);
}
Here, google.visualization.arrayToDataTable(temp) throws this error. Any idea about this? how to resolve it? I also tried to keep google.Visulization inside .js file. But it also not working. If I am running the same code from Browser, it is working fine. Is it problem with android?
在这里, google.visualization.arrayToDataTable(temp) 抛出这个错误。对此有什么想法吗?如何解决?我还尝试将 google.Visulization 保留在 .js 文件中。但它也不起作用。如果我从浏览器运行相同的代码,它工作正常。是安卓的问题吗?
回答by davidkonrad
Seems a bit strange. Try change
好像有点奇怪。尝试改变
<script src="../../common/scripts/jsapi.js" type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
</script>
To
到
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">google.load('visualization', '1.0', {'packages':['corechart']});</script>
Then the google object should be loaded correct. Also, you have pasted chart initialization from a google chart-example,
然后应该正确加载 google 对象。此外,您已从谷歌图表示例粘贴图表初始化,
chart = new google.visualization.LineChart(document.getElementById('chart_div'));
but your only HTML is
但你唯一的 HTML 是
<div class="PT_Care_vitals_swa_ofcurv_left" onclick="getPatientVitalInfo();"</div>
you miss the chart_div
id
你错过了chart_div
id