Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use d3.csv to load variable instead of file?

Tags:

csv

d3.js

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?

like image 608
Sijia Xiao Avatar asked Sep 05 '25 03:09

Sijia Xiao


1 Answers

Depending on the version of D3 you are going to use, you have to choose the appropriate function:

v3.x

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);

v4+

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);
like image 70
altocumulus Avatar answered Sep 07 '25 22:09

altocumulus