问题
I am trying to render a jqPlot bar chart from an AJAX request and I can't get it to work.
I have the following JavaScript code:
var ajaxDataRenderer = function (url, plot) {
var ret = null;
$.ajax({
// have to use synchronous here, else returns before data is fetched
async: false,
url: url,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
ret = data;
}
});
return ret;
};
var jsonurl = "WebService1.asmx/HelloWorld";
plo12 = $.jqplot('chart2', jsonurl, {
title: 'AJAX JSON Data Renderer',
dataRenderer: ajaxDataRenderer,
seriesDefaults: {
renderer: $.jqplot.BarRenderer,
rendererOptions: {
// Put a 30 pixel margin between bars.
barMargin: 30,
// Highlight bars when mouse button pressed.
// Disables default highlighting on mouse over.
highlightMouseDown: true
},
pointLabels: { show: true }
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer
},
yaxis: {
// Don't pad out the bottom of the data range. By default,
// axes scaled as if data extended 10% above and below the
// actual range to prevent data points right on grid boundaries.
// Don't want to do that here.
padMin: 0
}
},
legend: {
show: true,
location: 'e',
placement: 'outside'
}
});
And my WebService1.asmx
looks like this:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService {
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "[[2],[7],[14]]";
}
}
I have seen this solution, but I am new to this and I can't figure out what my problem is.
回答1:
I'm using this JQplot library via GWT. Make sure you have loaded the .JS files like jquery.jqplot,bar renders and so on..I think it will work or I might be wrong if you have already checked this.
回答2:
Please notice that in the link to a solution you provide there is /
in front of the jsonurl String. This might as well be it.
This might be also seen looking by the url of the requests you are sending, for example, in a firebug
console.
Otherwise can you double-check that the method returns anything? Just run it without the graph and output whatever it gives to the console.
Other than that I see no problems in the js
code.
回答3:
You can try with setting ret = [data]
jqPlot expects an object in array format to render the charts.
Thanks
来源:https://stackoverflow.com/questions/10414491/datarender-in-jqplot-asp-net-from-an-ajax-request