【发布时间】:2015-04-25 10:47:39
【问题描述】:
我在静态/data.json 路径中有数据。我正在尝试使用谷歌图表绘制图表。我想取出 data.json 文件中的数据并将其用于谷歌图表。
data.json 如下所示
data = '[{"k": "RAM" , "v":0.515625},{"k": "Camera" , "v":0.515625},{"k": "Design" , "v":0.05},{"k": "Processor" , "v":0},]';
我的 html 代码在 template/isworked.html 中。
下面的代码是isworked.html
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="application/json" src="static/d.json"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
var mydata = JSON.parse(data);
function drawChart() {
// Create the data table.
//loading data from json
var data = new google.visualization.DataTable();
data.addColumn('string', 'Feature');
data.addColumn('number', 'Positive');
var f,n;
for(var i = 0 ; i<data.length;i++){
f = data[i].k
n = data[i].v
data.addRows([[f,n]])
}
// Set chart options
var options = {'title':'Pie Chart',
'width':800,
'height':700};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
我无法绘制图表。 此 data.json 是从 python 脚本生成的。我在 Ubuntu
中使用 flask 框架运行所有程序我没有这方面的经验。有人可以帮帮我吗?
【问题讨论】:
-
我收到如下错误:Uncaught ReferenceError: data is not defined
标签: javascript jquery python json flask