我正在寻找使用json文件或本地电子表格(.xlsx)在Google可视化中填充数据表的最简单方法。
继承我的代码:
<!DOCTYPE html>
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable.fromJSON(sampleData.json,0.6);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
继承我的json文件:
{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [
{"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
{"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
{"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
]
}
当我在浏览器中运行代码时,我得到了 “未捕获的ReferenceError:未定义sampleData”
任何人都知道如何才能使这项工作?
提前致谢
答案 0 :(得分:1)
您可以简单地将JSON文件的内容直接放在HTML中的字符串“sampleData.JSON”所在的位置。也就是说,取出“sampleData.JSON”并将其替换为JSON文件的全部内容。如果你这样做,我很确定你会取得一些进展。
答案 1 :(得分:0)
尝试将sampleData.json放在引号中,假设文件名为sampleData.json并且与HTML位于同一文件夹中。
var data = new google.visualization.DataTable.fromJSON('sampleData.json',0.6);