javascript - 使用谷歌图表创建表格图表
问题描述
大家好,我只是想用下面这段代码中的代码制作一个谷歌图表表,我正在通过 axios 调用一个公共 api,但我无法将数据插入到表行中,你能告诉我我在做什么错吗. API = https://dummy.restapiexample.com/api/v1/employees
我想从这个 api 的数据创建一个 html 表。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Visualization API Sample</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<!--
One script tag loads all the required libraries!
-->
<script type="text/javascript"
src='https://www.gstatic.com/charts/loader.js'></script>
<script>
const age=[];
const emp_name=[];
const salary=[];
const id=[];
axios.get('https://dummy.restapiexample.com/api/v1/employees')
.then(response=> {
console.log("Response",response.data.data);
for(const obj of response.data.data){
console.log(obj);
age.push(obj.employee_age);
emp_name.push(obj.employee_name);
salary.push(obj.employee_salary);
id.push(obj.id);
}
for(var i=0; i<emp_name.length; i++){
console.log(
[id[i],emp_name[i],salary[i],age[i]]
)
}
})
.catch(err=>{
console.log("errr",err);
})
google.charts.load('current', {'packages':['table']});
google.charts.setOnLoadCallback(drawTable);
function drawTable() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'Id');
data.addColumn('string', 'Employee Name');
data.addColumn('number', 'Employee Salary');
data.addColumn('number', 'Employee Age');
for(var i=0; i<id.length; i++){
data.addRows([
[id[i],emp_name[i],salary[i],age[i]]
])
}
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true, width: '100%', height: '100%'});
}
</script>
</head>
<body>
<div id="table_div" style="height: 400px; width: 400px;"></div>
</body>
</html>
解决方案
推荐阅读
- c - 始终包含标准标题是一种好习惯吗?
- c++ - 使用 Qt5 & c++ 制作扫雷游戏
- java - 如何使用 Java 客户端更新 Elastic Search 上的条目
- python - 检测是否使用 django 打开了多个浏览器/选项卡
- java - Kotlin 生成的字节码会影响方法计数吗?
- python - 文字字符串插值 - 限制范围?
- modeling - 如何修复我的罗丹平台 Event-B 项目中的未知配置 org.animb.valuation.valBase?
- pdf - pdf 文件中的外部参照表
- gcovr - 使用 gcvor 覆盖其他目录中的文件
- angular - Angular 路由和 RxJs 订阅共享