0

I'm trying to understand reading external file .csv in d3 works with the type

I'm following Mike's http://bl.ocks.org/mbostock/1157787

I have read tutorial for reading csv, but I couldn't figure it out what type in this line actually does ?

d3.csv("stocks.csv", type, function(error, data) {

There are type function below

function type(d) {
  d.price = +d.price;
  d.date = parseDate(d.date);
  return d;
}

Can anyone help me understand this ? d.price = +d.price is that mean reading the next price ? or adding ?

JPC
  • 5,063
  • 20
  • 71
  • 100
  • This is not duplicate though, I title asking for "type" in .CSV I still don't understand how type involved in the read csv function. is it convert the data ? – JPC Nov 04 '14 at 15:55
  • 1
    Unlike JSON, CSV data is untyped, so all the values load as strings. `type()` provides an opportunity to parse/convert String values into Numbers when appropriate. `+d.price` is a shorthand way to convert such a string into a number. You can try this in your dev console: `+('42') === 42 // true` vs `'42' === 42 // false`. Or check out the difference between `'42' + 42` and `+('42') + 42`. – meetamit Nov 04 '14 at 16:56
  • so if I use json , there's no need for adding type in there correct ? @meetamit thank you . – JPC Nov 04 '14 at 18:44

0 Answers0