jqplot不显示ajax数据
我有以下 jQuery 代码:
$(document).ready(function () {
var group = 'test';
$.ajax({
type: "POST",
async: false,
url: "/validate.asmx/GetGraphData",
contentType: "application/json;",
data: "{'groupBy': '" + group + "'}",
dataType: 'json',
success: function (data) {
Plot(data.d);
}
});
});
function Plot(dataIn) {
alert(dataIn);
$.jqplot('chartcontainer', [[[ 'test1', 1 ], [ 'test2', 5]]],
{
seriesDefaults: {
renderer: $.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: { show: true, location: 'e' }
}
);
}
webmethod (在剪切它进行测试之后)如下所示:
[WebMethod]
public string GetGraphData(string groupBy)
{
PaymentModelDataContext db = new PaymentModelDataContext();
var q = from Event in db.TrackingEvents
group Event by Event.campaignID;
//string returnJSON;
//string returnJSON = "[";
//foreach (var grp in q)
//{
// returnJSON += "['" + grp.Key + "'," + grp.Count() + "],";
//}
//returnJSON += "]";
//var ser = new JavaScriptSerializer();
//returnJSON = ser.Serialize(q);
//return returnJSON;
return "[['test1' , 1],['test2' , 5]]";
}
如果我采用在此处返回的相同字符串并将其作为文本放入 jquery 代码中,则会显示该图。我在绘图函数中放置了一个警报,数据是我发送的。 有什么想法吗?
谢谢你!
I have the following jQuery code:
$(document).ready(function () {
var group = 'test';
$.ajax({
type: "POST",
async: false,
url: "/validate.asmx/GetGraphData",
contentType: "application/json;",
data: "{'groupBy': '" + group + "'}",
dataType: 'json',
success: function (data) {
Plot(data.d);
}
});
});
function Plot(dataIn) {
alert(dataIn);
$.jqplot('chartcontainer', [[[ 'test1', 1 ], [ 'test2', 5]]],
{
seriesDefaults: {
renderer: $.jqplot.PieRenderer,
rendererOptions: {
showDataLabels: true
}
},
legend: { show: true, location: 'e' }
}
);
}
The webmethod (after cutting it for testing) looks like this:
[WebMethod]
public string GetGraphData(string groupBy)
{
PaymentModelDataContext db = new PaymentModelDataContext();
var q = from Event in db.TrackingEvents
group Event by Event.campaignID;
//string returnJSON;
//string returnJSON = "[";
//foreach (var grp in q)
//{
// returnJSON += "['" + grp.Key + "'," + grp.Count() + "],";
//}
//returnJSON += "]";
//var ser = new JavaScriptSerializer();
//returnJSON = ser.Serialize(q);
//return returnJSON;
return "[['test1' , 1],['test2' , 5]]";
}
If I take the same string I return here and put it as text in the jquery code, the plot is shown. I put an alert in the plot function, and the data is as I sent it.
Any ideas?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用数据渲染器
use the dataRenderer
将 ajax 数据放入 jqPlot dataRenderer 是一种选择。但它也会按照您选择的方式工作。
确保数据不被处理为字符串。
如果您的数据是
"[['test1' , 1],['test2' , 5]]"
则arr=eval("[['test1' , 1],['test2' , 5]]")
应该根据您的数据生成一个数组。如果您使用
console.warn("[['test1' , 1],['test2' , 5]]");
和console.warn(arr );
在 Firebug 控制台上。Putting ajax data to jqPlot dataRenderer is one option. But it will also work at the way you choose.
Make sure, that the data is not processed as string.
if your data is
"[['test1' , 1],['test2' , 5]]"
thanarr=eval("[['test1' , 1],['test2' , 5]]")
should generate an array from your data.You will see the difference, if you use
console.warn("[['test1' , 1],['test2' , 5]]");
andconsole.warn(arr);
on the firebug console.