I'm using D3.js to load csv file. It should look like this:
id,
a,
b,
But the csv is created inside my code, so I store it in a variable like this:
var flare = 'id,\na,\nb,\n'
However, the script does not work:
d3.csv(flare, function(error, data){
if(error) throw error;
});
How to solve the problem?
Depending on the version of D3 you are going to use, you have to choose the appropriate function:
In versions 3.x d3.csv.parse()
is what you are looking for:
Parses the specified string, which is the contents of a CSV file, returning an array of objects representing the parsed rows.
For your example this would be
var flare = 'id,\na,\nb,\n';
var data = d3.csv.parse(flare);
For version 4 and above the CSV parser has become part of the d3-dsv module. The function is now named d3.csvParse()
.
var flare = 'id,\na,\nb,\n';
var data = d3.csvParse(flare);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With