我需要绘制google.visualization.AnnotatedTimeLine
一个<div id="visualization" style="width: 800px; height: 400px; display: none;"></div>
div。Google 使用 div 的宽度和高度来确定可视化的大小。不幸的是,谷歌无法获得宽度或高度,因为我正在向显示器绘图:无;div,因此我得到一个错误:容器宽度为零。期望一个有效的宽度。错误。
我唯一能想到的就是绘制到一个显示器上:block; div 然后隐藏 div,但这样做看起来有点奇怪,因为图表会显示一瞬间。还有其他选择吗?
重现的示例代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Google Visualization API Sample</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['annotatedtimeline']});
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Sold Pencils');
data.addColumn('string', 'title1');
data.addColumn('string', 'text1');
data.addColumn('number', 'Sold Pens');
data.addColumn('string', 'title2');
data.addColumn('string', 'text2');
data.addRows([
[new Date(2008, 1 ,1), 30000, null, null, 40645, null, null],
[new Date(2008, 1 ,2), 14045, null, null, 20374, null, null],
[new Date(2008, 1 ,3), 55022, null, null, 50766, null, null],
[new Date(2008, 1 ,4), 75284, null, null, 14334, 'Out of Stock', 'Ran out of stock on pens at 4pm'],
[new Date(2008, 1 ,5), 41476, 'Bought Pens', 'Bought 200k pens', 66467, null, null],
[new Date(2008, 1 ,6), 33322, null, null, 39463, null, null]
]);
var annotatedtimeline = new google.visualization.AnnotatedTimeLine(
document.getElementById('visualization'));
annotatedtimeline.draw(data, {'displayAnnotations': true});
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial; border: 0 none;">
<div id="visualization" style="width: 800px; height: 400px; display: none;"></div>
<input type="button" onclick="toggle();" value="Toggle Visualization">
<script language="javascript">
function toggle() {
var ele = document.getElementById('visualization');
if(ele.style.display == "block") {
ele.style.display = "none";
} else {
ele.style.display = "block";
}
}
</script>
</body>
</html>
参考:
https://developers.google.com/chart/interactive/docs/gallery/annotatedtimeline
http://code.google.com/apis/ajax/playground/?type=visualization