Google chart formatting
I have the following script which I need to modify a little. Here is the 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"}}
]
and here is the script:
<html>
<head>
<script language="javascript" src="http://开发者_如何学Pythoncode.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>
This works by giving me a nice piechart, How do I get the "f" value into the chart too from the JSON data?
Add an f column:
data.addColumn('number', 'v');
data.addColumn('number', 'f'); // <- new line
And, add the data to the rows:
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
}
But, I don't know how a pie chart handles that.
精彩评论