Ahmad Mobin
Ahmad Mobin

Reputation: 153

D3.js version 6: Loading a TSV and changing Variable types

I am relatively new to D3.js (version 6). I am trying to load a local .txt file "data.tsv (see github repo at the end).

Here is what I have tried:

var psv=d3.dsvFormat("|");

d3.text("data.txt", function(d){
 var rows= psv.parse(data); 
    
}).then(function(data){
    console.log(rows);
});

The console log says: dataHandlers.js:39 Uncaught (in promise) ReferenceError: rows is not defined

Note: I have uploaded the .txt on my github page! https://raw.githubusercontent.com/AhmadMobin/D3-Learning/main/data.txt

Thank you so much in advance in helping a newbie on his D3.js journey

Note: I did ask a very similar question on how to load .csv file in which a kind soul did provide me with a solution :) D3.js version 6: Loading a CSV and changing Variable types

Upvotes: 2

Views: 198

Answers (2)

Ahmad Mobin
Ahmad Mobin

Reputation: 153

Here is my full script for completeness to show how I changed the variable type:


d3.dsv("|", "data.txt", function(d){
return{
    month: parseDate(d.month),
    price: Number(d.price.trim().slice(1))
}

}).then(function (data) {
   console.log(data);
   
   });

Huge shoutout to @Gerardo Furtado !


 

Upvotes: 0

Gerardo Furtado
Gerardo Furtado

Reputation: 102194

You're missing a return here:

function(d){
    var rows= psv.parse(data);    
}

But that's not the issue: not only d3.text doesn't accept a row conversion function, even if it did the rows variable would never be automagically passed to the promise (only the text itself).

That said, all you need is d3.dsv:

d3.dsv("|", "https://raw.githubusercontent.com/AhmadMobin/D3-Learning/main/data.txt").then(function(data) {
  console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

Upvotes: 2

Related Questions