I've started using d3 to pass some CSV data, and normally my CSV's look like
num,value
1,2
3,1
5,2
but I'm loading one CSV with the filename "sample2.csv" which doesn't have any headers, and simply looks like
5,3
7,5
, the code I'm using looks like
d3.queue()
.defer(d3.csv, "sample1.csv").defer(d3.csv, "sample2.csv").defer(d3.csv, "sample3.csv")
.await(function(error, sample1, sample2, sample)
{
}
I've read that for the CSV without headers, you need to use csvParseRows
Is this correct? And how could I pass csvParseRows into my code for "sample2.csv"
Does anyone know much about d3 and how this could be done?
Thanks
If using d3.queue, you can parse the text of the csv as usual, then in the await function, you'll need to parse rows:
d3.queue()
.defer(d3.text,"test.csv")
.await(ready);
function ready(error, text) {
var csv = d3.csvParseRows(text);
console.log(csv);
/* use csv data */
}
The csvParseRows method takes a string of text representing the contents of the file, not the url of the file. We can get those contents with d3.text, process it once we've loaded it.
With d3v5 an higher though we'd not need d3-queue as we can use a very similar approach without:
d3.text("test.csv").then(ready)
function ready(text) {
var csv = d3.csvParseRows(text);
console.log(csv);
/* use csv data */
}
And if we had many files:
Promise.all([d3.text("file1.csv"),d3.text("file2.csv")]).then(ready);
function ready(files) {
var csvs = files.map(function(file) {
return d3.csvParseRows(file);
});
console.log(csvs);
/* use csv data */
}