0

I'm having trouble with this code:

google.charts.load('current', {
    packages: ['corechart', 'line']
});
google.charts.setOnLoadCallback(drawBasic);

function time(dati) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            dati = [];
            dati.push(JSON.parse(this.responseText));
            console.log(dati); //logs perfectly fine
            return dati;
        };
    };
    xmlhttp.open("GET", "graph-data.php", true);
    xmlhttp.send();
};
time();
console.log(dati); // logs nothing

function drawBasic(dati) {

    var data = new google.visualization.DataTable();
    data.addColumn('number', 'X');
    data.addColumn('number', 'Prijs');

    console.log(dati); //logs nothing

    data.addRows(dati);

    var options = {
        hAxis: {
            title: 'Time'
        },
        vAxis: {
            title: 'Prijs'
        }
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

    chart.draw(data, options);
}

The variables are not defined. How do I define them?

9
  • 2
    post your error Commented Jan 5, 2017 at 9:57
  • Why do you print console.log(dati); outside time-function? Commented Jan 5, 2017 at 9:58
  • @ricky data.js:17 Uncaught ReferenceError: dati is not defined Commented Jan 5, 2017 at 9:59
  • @Blauharley but I did? on line 17 Commented Jan 5, 2017 at 10:00
  • @PDek: Was the other way around ;) Commented Jan 5, 2017 at 10:01

1 Answer 1

2

You're getting that error because dati is an undeclared variable you're trying to take the value of on Line 17:

console.log(dati);

There is no in-scope dati identifier where that code appears. You have a dati parameter inside your time function, but that's only inside your time function; the line above is outside it.

Once you've fixed that problem, your next problem is covered here: How do I return the response from an asynchronous call? time starts an asynchronous operation that won't be completed yet when time returns.

You also seem to be expecting setOnLoadCallback to pass something to your drawBasic function:

google.charts.setOnLoadCallback(drawBasic);

I can't find the docs for it (!!), but this page's examples don't show the functions called by it accepting any parameters.


Guessing a bit, but I suspect you wanted to do something like this; see inline comments:

// A scoping function so we don't create globals
(function() {
    // Variables for the data and a loaded flag
    var dati = null;
    var loaded = false;

    // A function we can call from both async operations to see if both have completed
    function checkReady() {
        if (dati && loaded) {
            // They're both done, we can draw
            drawBasic(dati);
        }
    }

    // Load charts and get callback when done
    google.charts.load('current', {
        packages: ['corechart', 'line']
    });
    google.charts.setOnLoadCallback(function() {
        loaded = true;
        checkReady();
    });

    // Load our data
    function time() { // <== Note no `dati` parameter, we want to use the variable declared above
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                // Fill in the data -- are you sure you really want an array with the data
                // as its only entry?
                dati = [];
                dati.push(JSON.parse(this.responseText));

                // If we're ready, draw
                checkReady();
            };
        };
        xmlhttp.open("GET", "graph-data.php", true);
        xmlhttp.send();
    };
    time(); // Start the data load

    function drawBasic(dati) {

        var data = new google.visualization.DataTable();
        data.addColumn('number', 'X');
        data.addColumn('number', 'Prijs');

        console.log(dati); //logs nothing

        data.addRows(dati);

        var options = {
            hAxis: {
                title: 'Time'
            },
            vAxis: {
                title: 'Prijs'
            }
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

        chart.draw(data, options);
    }
})(); // The end of the scoping function, and `()` to invoke it

I'm sure that's not perfect, but hopefully it gets you going the right way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.