1

I'm new to d3.js, when loading data, examples I've seen so far looks like

d3.csv("path/to/file.csv")...

I have a function that returns a dynamic server side generated csv, i use synchronous POST ajax to generate them, but for simplicity's sake lets say, I have a js function like this

function makeCSV(){
    return "id,value\n"+
           "0,value 1\n"+
           "1,value 2\n"+
}

is there a way to do something like this? and is this approach recommended at all (synchronous ajax is sure to cause lag)

d3.csv(makeCSV())...

other related concerns about CSV data loading is, is it required that .csv file is served, or would it be possible to simply put URL that makes and downloads a dynamically generated csv file, this could work as a replacement for AJAX call but manually encoding each parameter looks tedious and non-dynamic

d3.csv("?action=makeCSV&param1=val1&param2=val2")...
3
  • 1
    yes , you can do d3.csv(makeCSV(),function(error, data) { data.forEach(function(d) { d.; });) Commented Jun 24, 2016 at 3:30
  • @NagaSaiA Of course you can, but this will result in an error because makeCSV() doesn't return a URL. Commented Jun 24, 2016 at 8:44
  • I just added an answer for the first part of your question, but I don't quite understand the second part about dynamically loading CSVs. Could you please be a bit more specific and add some more details. This maybe a question in its own right, though. Commented Jun 24, 2016 at 8:46

1 Answer 1

2

According to the spec d3.csv() requires the first argument be the url to load the CSV from. If you want to just parse a string containing your CSV's content no matter how this was created, you need to use d3.csv.parse() instead. For your code this would be some like this

var rows = d3.csv.parse(makeCSV());

function makeCSV(){
    return "id,value\n"+
           "0,value 1\n"+
           "1,value 2\n";
}

var rows = d3.csv.parse(makeCSV())

console.log(rows);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.