我在为函数内部的google时间轴制作数据表时遇到了一些麻烦,然后返回函数的数据表外侧。我的意图是最终在循环内部调用这个函数,从电子表格中生成几个数据表,现在它是静态的。
为什么这里的函数“makeTable”不会返回我的表汇编的结果?
感谢您的帮助。
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>
<script type="text/javascript">
var container = document.getElementById('example5.4');
var chart = new google.visualization.Timeline(container);
var firsttable = new google.visualization.DataTable();
//initiate and return table, with contents
firsttable = makeTable();
//determine height of chart based on row count in tables
var height = firsttable.getNumberOfRows() * 41 + 60;
var options = {'colors': ['#cbb69d'],'width': 1500, 'height': height,'chartArea': {'width': '100%', 'height': '50%'}};
//make chart?
google.setOnLoadCallback(chart.draw(firsttable, options));
//insert div code for new chart.
document.write("<div id='example5.4' style='width: 900px, height: 300px'></div><br>");
function makeTable()
{
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'Project Name' });
dataTable.addColumn({ type: 'string', id: 'Stage' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows([['Project 1', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 2', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 3', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 4', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 5','LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 6', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 7', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 8', 'LNTP', new Date(2015,9, 15), new Date(2015,10, 31)],['Project 9', 'LNTP', new Date(2014,11, 26), new Date(2015,3, 12)]]);
return dataTable;
}
</script>
<!-- <div id="example5.4" style="width: 900px; height: 150px;"></div> -->
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:5px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:bold;padding:5px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-ohfc{ background-image: url("bgnoise_lg.png");background-size: 10px 10px;background-color:#38fff8;text-align:Left}
.tg .tg-med2{background-image: url("bgnoise_lg.png");background-size: 10px 10px;background-color:#38fff8;font-weight: bold;}
.tg .tg-v88f{background-image: url("light_grey.png");background-size: 10px 10px;background-color:#f8ff00}
.tg .tg-v9m3{background-image: url("light_grey.png");background-size: 10px 10px;background-color:#f8ff00;color:#333333}
.tg .tg-031e{padding:0px 0px}
</style>
<html>
<div id="testdiv"></div>
</html>
答案 0 :(得分:1)
当您访问container
时,<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>
<script type="text/javascript">
//first insert div code for new chart.
document.write("<div id='example5.4' style='width: 900px, height: 300px'></div><br>");
//then access the element
var container = document.getElementById('example5.4');
var chart = new google.visualization.Timeline(container);
var firsttable = new google.visualization.DataTable();
//initiate and return table, with contents
firsttable = makeTable();
//determine height of chart based on row count in tables
var height = firsttable.getNumberOfRows() * 41 + 60;
var options = {'colors': ['#cbb69d'],'width': 1500, 'height': height,'chartArea': {'width': '100%', 'height': '50%'}};
//make chart?
google.setOnLoadCallback(chart.draw(firsttable, options));
function makeTable()
{
var dataTable = new google.visualization.DataTable();
dataTable.addColumn({ type: 'string', id: 'Project Name' });
dataTable.addColumn({ type: 'string', id: 'Stage' });
dataTable.addColumn({ type: 'date', id: 'Start' });
dataTable.addColumn({ type: 'date', id: 'End' });
dataTable.addRows([['Project 1', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 2', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 3', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 4', 'LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 5','LNTP', new Date(2015,12, 31), new Date(2016,3, 31)],['Project 6', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 7', 'LNTP', new Date(2015,5, 31), new Date(2015,7, 31)],['Project 8', 'LNTP', new Date(2015,9, 15), new Date(2015,10, 31)],['Project 9', 'LNTP', new Date(2014,11, 26), new Date(2015,3, 12)]]);
return dataTable;
}
</script>
不可用,您必须在访问该元素之前插入该节点。
DB.addUser