2

I am probably getting it all backward, and I am afraid I haven't done much javascript for a long time and things have changed quite a bit since then. The answer may thus be very trivial, but I wasn't able to find anything useful online.

Very simply, I would like to papaparse a csv file, either locally (/log.csv) or remotely (http://mywebsite.com/log.csv), the data from which I would like to be able to use in the rest of the script. In other words, ideally something like that:

var mydata = Papa.parse("http://fetconsulting.co.uk/demo-fleetdrive/log_full.csv", {
   download: true,
   complete: function(results) {
       console.log(results);
   }
});

alert(mydata.data.length);
myfantasticplottingfunction(mydata);

Thoughts?

Thanks a ton!

  • The last two lines are executed before the download and parsing is complete. If you want to run code that uses the results of parsing, it has to go in the callback. – Matt Sep 24 '15 at 14:19

1 Answers1

2

This solved my problem:

Papa.parse("http://mywebsite.com/log.csv", {
    download: true,
    complete: function(results) {

        (function myfantasticplottingfunction(container) {

         // Do amazing things with envision.js on results
         console.log(results);
         alert(results.length);

         return new envision.templates.TimeSeries(options);
        })(document.getElementById("editor-render-0"));
     }
});

Basically, I did indeed get things backward and misunderstood callback functions. The variable results was out of scope, because produced asynchronously, and therefore difficult/impossible to return "the usual way". The easy fix was to place my visualisation within the scope of the callback.

Thanks!