I have some data in a csv that I would like to read in using d3 (version 4) and assign to a global variable. All of the examples I can find online (here and here) read in a csv and then print the results to the console.
So if I run:
d3.csv("mydata.csv", function(data) {
console.log(data[0]);
});
Or
d3.csv("mydata.csv", function(d) {
return {
country : d.country,
month : d.month,
count : +d.count
};
}, function(data) {
console.log(data[0]);
});
I get the first line of my csv file.
However, if I try to run a similar script and write the results to a variable, ie:
var mydata = d3.csv("mydata.csv");
Or
var mydata = d3.csv("mydata.csv", function(d) {
return {
country : d.country,
month : d.month,
count : +d.count
};
}, function(data) {
console.log(data[0]);
});
And try to examine the variable mydata
in the console, I am given a list of methods, not the actual data. Ie,
What am I doing wrong? How to I assign the data in the csv to a global variable, so I can eventually use it for visualization? I think I am interested in a synchronous solution, so I am open to not using d3.csv() if there are other functions out there that will simply read in the data and assign it to a global variable.