jqPlot with DateAxisRenderer and ajax

眉间皱痕 提交于 2019-12-11 03:14:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!