问题
I'm running into difficulties trying to combine DateAxisRenderer and ajax on a jqPlot chart. The code below does not produce any errors but it creates a chart with no gridlines, no labels on the y axes, and no data points plotted. There is just a blank white chart background with two x axis labels both saying Dec 31, 69.
If I swap out the ajaxDataRenderer and use data from an array like what is done in this example (http://www.jqplot.com/tests/date-axes.php) everything renders correctly.
Here is my Javascript:
var ajaxDataRenderer = function(url, plot, options) {
var ret = null;
$.ajax({
async: false,
url: url,
type: "GET",
dataType:"json",
success: function(data) {
ret = data;
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.responseText);
}
});
return ret;
};
var jsonurl = "http://localhost:8080/chartdata";
var plot2 = $.jqplot('chart2', jsonurl, {
title:'Customized Date Axis',
dataRenderer: ajaxDataRenderer,
axes:{
xaxis:{
renderer:$.jqplot.DateAxisRenderer,
tickOptions:{formatString:'%b %#d, %y'}
}
}
});
The JSON being returned from jsonurl
looks like:
[["2008-09-30 4:00PM",15],["2008-10-30 4:00PM",8],["2008-11-30 4:00PM",17],["2008-12-30 4:00PM",10]]
Any ideas would be much appreciated!
回答1:
I figured out the problem. The JSON returned from the server needs to be wrapped in another group of square brackets.
It should look like:
[[["2008-09-30 4:00PM",15],["2008-10-30 4:00PM",8],["2008-11-30 4:00PM",17],["2008-12-30 4:00PM",10]]]
来源:https://stackoverflow.com/questions/15378723/jqplot-with-dateaxisrenderer-and-ajax