【发布时间】:2015-10-14 14:27:24
【问题描述】:
我正在尝试创建多个 Google 图表,但无法正常工作。我已经尝试了所有可以在 Stack Overflow 上找到的东西。我最近尝试了this 修复,但没有奏效。我想我可能会遗漏一些东西。任何人都可以看到现在的代码有什么问题吗?
预期行为:
页面显示条形图。然后,条形图下方会显示一个折线图。
当前行为:
页面显示条形图。折线图不显示。
这里是JSFiddle。附带说明一下,JavaScript 似乎只能在 JSFiddle 上内联工作。如果我把它移到 JavaScript 部分,它就不能正常工作。也许这与调用的外部资源有关?
无论如何,我目前正在为这个实验做这一切。
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="https://www.google.com/jsapi" type="text/javascript">
</script>
<script type="text/javascript">
// Load the Visualization API and the chart packages.
google.load('visualization', '1.1', {
packages: ['line', 'bar', 'corechart']
});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the charts, passes in the data and
// draws them.
function drawChart() {
// Create the data table.
var BarData = new google.visualization.arrayToDataTable([
['', 'Customer', 'Segment Avg'],
['TTM Sales', 4, 2],
['TTM Orders', 5, 3],
['TTM Categories', 7, 4]
]);
// Create the data table.
var LineData = new google.visualization.arrayToDataTable([
['Year', 'Customer', 'Segment Avg'],
['2011', 4, 5],
['2012', 5, 3],
['2013', 4, 2]
]);
// Set chart options
var BarOptions = {
chart: {
title: 'Performance',
},
width: 900,
height: 500
};
// Set chart options
var LineOptions = {
chart: {
title: 'Sales History'
},
width: 900,
height: 500
};
// Instantiate and draw our chart, passing in some options.
var BarChart = new google.charts.Bar(document.getElementById(
'bar_chart'));
BarChart.draw(BarData, BarOptions);
var LineChart = new google.charts.Line(document.getElementById(
'line_chart'));
LineChart.draw(LineData, LineOptions);
};
</script>
<title>Test Chart Page</title>
</head>
<body>
<!--Divs that will hold the charts-->
<div id="bar_chart"></div>
<div id="line_chart"></div>
</body>
</html>
【问题讨论】:
-
问题与时间有关。这是您小提琴的工作分支,超时将两个图表的执行分开。 jsfiddle.net/m7mn72e2
-
另请注意,在失败的测试用例中,子元素由 googlecharts 代码添加到两个 div。
标签: javascript html charts google-visualization