chinnusaccount
chinnusaccount

Reputation: 229

Ajax call for jqplot not working for jsfiddle

var jsonData = {
    "a": 10,
     "b": 20
};

var ajaxDataRenderer = function (url, plot, options) {
    var ret = null;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: jsonurl,
        data: {
            json: JSON.stringify(jsonData)
        },
        success: function (data) {
             $('#ajax').append(data.a).append( ['<br/>',data.b].join('') );
            ret = data;
        }
    });
    return ret;
  };

    var jsonurl = "/echo/json/";

    var plot1 = $.jqplot('bar_chart', jsonurl, {
        title: "AJAX JSON Data Renderer",
        dataRenderer: ajaxDataRenderer,
        dataRendererOptions: {
      unusedOptionalUrl: jsonurl
    }
    });

I have tried the above code in jsfiddle. I am getting the response for ajax request, but the jqplot not working..Please suggest on this. Following div ids are defined in jsfiddle

<div id="bar_chart"></div>

<h1>Mocking AJAX on jsfiddle</h1>

<div id="ajax"></div>

Upvotes: 0

Views: 224

Answers (1)

AnthonyLeGovic
AnthonyLeGovic

Reputation: 2335

The Ajax renderer is waiting an array in order to draw a plot. You give it an object. You have to convert your JS object into an array :

var array = $.map(data, function(value, index) {
  return [value];
});
return [array];

Please see a working fiddle here

Upvotes: 1

Related Questions