Reputation: 1
I need to make a chart using a jqPlot with data array like
[[2010-06-30 18:40:11, 18], [2010-06-30 18:42:17, 25]...]
it works great with the official example for "dateAxisRenderer plugin"
but now i need to make ajax-loaded chart. it seems to me that "AJAX JSON Data Renderer" method can't use dates as a "x" data source and only can take "y" values from simple [y1,y2,y3] array. how can i make him to take both coords from my array - date for "x" and number for "y"? JSON example:
$(document).ready(function(){
var ajaxDataRenderer = function(url, plot, options) {
var ret = null;
$.ajax({
async: false,
url: url,
dataType:"json",
success: function(data) {
ret = data;
}}
);
return ret;
};
var jsonurl = "jsondata.php";
var plot2 = $.jqplot('chart2', jsonurl,{
title: "AJAX JSON Data Renderer",
dataRenderer: ajaxDataRenderer,
dataRendererOptions: {
unusedOptionalUrl: jsonurl
}
});
});
thanks.
Upvotes: 0
Views: 5970
Reputation: 143
The basic issue in your code is renderer is not returning the response received form the ajax request. Success function call sets the variable after returning it.
Take a look at the code below.
Steps of execution:
plot
function on page loadrenderer
with response datajqplot
with arguments form the response data.var ajaxJqPlot = {
settings : {
data_url : "jsondata.php",
element_id : "chart2"
},
plot : function(settings){
t = this;
$.extend(t.settings, settings);
$.ajax({
async : false,
url : t.settings.data_url,
dataType :"json",
success : t.renderer
});
},
renderer : function(res){
t = this;
$.jqplot(t.settings.element_id, res.data, res.opt);
}
};
$(document).ready(function(){
ajaxJqPlot.plot();
});
Upvotes: 2