使用jqPlot绘制JSON数据 [英] plotting JSON data using jqPlot
问题描述
我正在尝试使用由jqMobile驱动的HTML 5应用中的 jqPlot
库来绘制一些 JSON
数据。我将以下代码放在html页面的正文中。我在这里缺少什么吗?
I'm trying to plot some JSON
Data using the jqPlot
library within an HTML 5 app powered by jqMobile. I'm placing the following code within the 'body' of the html page. Is there something I'm missing here?
<script>
$(document).ready(function() {
// get the JSON data from server
$.getJSON("myspecialurl", function(data) {
success: function(data) {
plotData(data);
}
});
// plot the data
function plotData(data) {
ds = [];
$(data).find('latitude').each(function() {
ds.push([$(this).attr('answer'), parseInt($(this).attr('count'))]);
});
$.jqplot('chart1', [ds], {
seriesDefaults: {
renderer: $.jqplot.DonutRenderer
},
legend: {
show: true
}
});
}
}
</script>
修改:新绘图方法
function plotData( data ) {
// ds = [];
// $(data).find('latitude').each( function() {
// ds.push( [ $(this).attr('answer'), parseInt( $(this).attr('count') ) ] );
// } );
var array = data.contacts;
$.jqplot('chart1', array[0].latitude, {
seriesDefaults:{
renderer:$.jqplot.DonutRenderer
},
legend: {show:true}
});
}
推荐答案
粗略有问题而电脑又是对的。这就是你的代码应该是这样的。您定义成功就像使用 ajax
方法一样, getJSON
成功作为第二个参数传递。
Of coarse there is a problem and the computer is again right. This is how your code should look like. You were defining success as if you were using ajax
method, with getJSON
success is passed as the 2nd parameter.
$.getJSON("myspecialurl", function(data) {
plotData(data);
});
编辑
我发现你还没有关闭 ready
正常运行。它应该是});
而不仅仅是}
。
EDIT
I spot also that you are not closing the ready
function appropriately. It should be });
rather than just }
.
这篇关于使用jqPlot绘制JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!