gpt4 book ai didi

javascript - 我可以在函数调用中写入我的 html 正文内容吗?

转载 作者:行者123 更新时间:2023-11-28 04:43:13 25 4
gpt4 key购买 nike

在我的代码中,我以图形和表格格式显示了表格信息。
但我不知道如何在drawchart()函数中包含该表代码。需要帮助
提前致谢。

我的代码是

<script>
$(function() {
var sampleData = [];
$.getJSON('sampleData.json', function(data) {
$.each(data.info, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.years + "</td>" + "<td>"
+ f.sales + "</td>" + "<td>" + f.expenses + "</td>"
+ "<td>" + f.profit + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
});

});

});
</script>
<script type="text/javascript">
google.charts.load('current', {'packages' : [ 'bar' ]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var lines = "anupama";
var data = google.visualization.arrayToDataTable([
[ 'Year', 'Sales', 'Expenses', 'Profit' ],
[ '2014', 1000, 400, 200 ], [ '2015', 1170, 460, 250 ],
[ '2016', 660, 1120, 300 ], [ '2017', 1030, 540, 350 ]

]);

var options = {
chart : {
title : 'Company Performance',
subtitle : 'Sales, Expenses, and Profit: 2014-2017',
},
bars : 'horizontal' // Required for Material Bar Charts.
};

var chart = new
google.charts.Bar(document.getElementById('barchart_material'));

chart.draw(data, google.charts.Bar.convertOptions(options));
}
</script>
</head>
<body>
<div id="barchart_material" style="width: 900px; height: 500px;"></div>

<div class="wrapper">
<div class="profile">
<table id="userdata" border="2">
<thead>
<th>Years</th>
<th>Sales</th>
<th>Expenses</th>
<th>profit</th>
</thead>
<tbody>

</tbody>
</table>

</div>
</div>

如果有任何建议,非常感谢。

最佳答案

试试这个:

var sampleData = [['Year', 'Sales', 'Expenses', 'Profit']];

$(function() {

$.getJSON('sampleData.json', function(data) {
$.each(data.info, function(i, f) {
sampleData.push([f.years, f.sales, f.expenses, f.profit]);

var tblRow = "<tr>" + "<td>" + f.years + "</td>" + "<td>" +
f.sales + "</td>" + "<td>" + f.expenses + "</td>" +
"<td>" + f.profit + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
});

});

});

google.charts.load('current', {
'packages': ['bar']
});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
var lines = "anupama";
var data = google.visualization.arrayToDataTable(sampleData);

var options = {
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
bars: 'horizontal' // Required for Material Bar Charts.
};

var chart = new
google.charts.Bar(document.getElementById('barchart_material'));

chart.draw(data, google.charts.Bar.convertOptions(options));
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="barchart_material" style="width: 900px; height: 500px;"></div>

<div class="wrapper">
<div class="profile">
<table id="userdata" border="2">
<thead>
<th>Years</th>
<th>Sales</th>
<th>Expenses</th>
<th>profit</th>
</thead>
<tbody>

</tbody>
</table>

</div>
</div>

关于javascript - 我可以在函数调用中写入我的 html 正文内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43603094/

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