我有以下脚本需要修改一下。这是json:
[
{"User1":{"v": 50.00,"f": "£100"}},
{"User2":{"v": 10.00,"f": "£20"}},
{"User3":{"v": 10.00,"f": "£20"}},
{"User4":{"v": 10.00,"f": "£20"}},
{"User5":{"v": 20.00,"f": "£40"}}
]
这是脚本:
<html>
<head>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChartAjax);
function drawChartAjax() {
$.ajax({
url: 'chart_json.aspx',
type: 'POST',
dataType: 'json',
success: function(data) {
drawChart(data);
}
});
}
function drawChart(json) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'User');
data.addColumn('number', 'v');
data.addRows(json.length);
for(var j in json) {
for(var k in json[j]) {
data.setValue(parseInt(j), 0, k);
data.setValue(parseInt(j), 1, json[j][k].v);
}
}
var chart = new google.visualization.PieChart( document.getElementById('chart_div') );
chart.draw(data, {width: 500, height: 300, is3D: true, title: 'Work In Progress'});
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
这可以通过给我一个漂亮的饼图,如何从JSON数据中获取图表中的“f”值?
答案 0 :(得分:1)
添加f列:
data.addColumn('number', 'v');
data.addColumn('number', 'f'); // <- new line
然后,将数据添加到行:
for(var k in json[j]) {
data.setValue(parseInt(j), 0, k);
data.setValue(parseInt(j), 1, json[j][k].v);
data.setValue(parseInt(j), 2, parseInt(json[j][k].f.substring(1))); // <- new line
}
但是,我不知道饼图是如何处理的。