Skip to content Skip to sidebar Skip to footer

Data Supplied For Flot.js Via Ajax Says Undefined

In the below, script I'm getting data from database to form a line graph. But the data says undefined and no graph is formed. var d; var arr = []; $(function() { var data;

Solution 1:

The AJAX call is asynchronous so all code that relies on its response must be placed in the callback function. Try this:

var d;
var arr = [];

$(function() {
    var data;
    $.ajax({
        dataType: "json",
        url: 'query_sales.php',
        success: function(data){
            $.each(data, function(i, item) {
                arr.push([item.datey, +item.bv]);
            }); 
            d = arr; //JSON.stringify(arr);

            console.log(d); // use console.log to debug

            $.plot("#placeholder", [d], {
                xaxis: { mode: "time" }
            });

            $("#whole").click(function () {
                $.plot("#placeholder", [d], {
                    xaxis: { mode: "time" }
                });
            });
        }
    });

    // Add the Flot version string to the footer
    $("#footer").prepend("Flot " + $.plot.version + " – ");
});

Post a Comment for "Data Supplied For Flot.js Via Ajax Says Undefined"