Google Charts ColumnChart不会呈现,因为“容器未定义” [英] Google Charts ColumnChart won't render because "Container is not defined"
问题描述
Chrome上的JavaScript控制台表示容器未定义,即使我的 div
的id和<$ c $中指定的id c> getElementById 匹配。有人知道为什么吗?
$ pre $ <脚本类型= 文本/ JavaScript的 >
google.load('visualization','1',{packages:['corechart']});
< / script>
< script type =text / javascript>
var data = [[''Year','Disney','SeaWorld','Universal'],['2013',95,100,102]];
var options =
{
标题:主题公园价格,
宽度:600,
身高:400,
hAxis: {title:Year},
};
var chart = new google.visualization.ColumnChart(document.getElementById('priceChart'));
函数drawChart()
{
chart.draw(data,options);
}
drawChart();
< / script>
<! - 图表代码结束 - >
< / head>
< body>
< div id =priceChartstyle =width:600px; height:400px; margin:0 auto;>< / div>
< / body>
< / html>
事件回调,否则会在您的 div
存在之前执行并因此失败:
google.setOnLoadCallback(function(){
//你的JS代码在这里
});
The JavaScript console on Chrome says that container is not defined even though my div
's id and the id specified in getElementById
match. Anybody know why?
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
var data = [['Year', 'Disney', 'SeaWorld', 'Universal'], ['2013', 95, 100, 102]];
var options =
{
title:"Theme Park Prices",
width:600,
height:400,
hAxis: {title: "Year"},
};
var chart = new google.visualization.ColumnChart(document.getElementById('priceChart'));
function drawChart()
{
chart.draw(data, options);
}
drawChart();
</script>
<!-- End of chart code -->
</head>
<body>
<div id="priceChart" style="width: 600px; height: 400px;margin: 0 auto;"></div>
</body>
</html>
You need to put your JS code inside some document onload event callback, otherwise it is executed before your div
exists and thus fails :
google.setOnLoadCallback(function(){
//your JS code here
});
这篇关于Google Charts ColumnChart不会呈现,因为“容器未定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!