gpt4 book ai didi

javascript - Google 可视化表不会从函数返回

转载 作者:行者123 更新时间:2023-12-03 10:10:37 25 4
gpt4 key购买 nike

我在为函数内部的谷歌时间轴制作数据表,然后在函数外部返回数据表时遇到一些麻烦。我的目的是最终在循环内部调用此函数,以从电子表格中生成多个数据表,目前它是静态的。

为什么这里的函数“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>

最佳答案

容器在您访问它时不可用,您必须在访问元素之前插入节点。

<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>

关于javascript - Google 可视化表不会从函数返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30127900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com