0

I have entered the following code in the Chrome console:

d3.csv("dataset32.txt")
    .row(function(d) { return {time: +d.time, value: +d.val}; })
    .get(function(error, rows) { console.log(rows); });

This returns an array of 160 objects which has the data of my CSV file.

How do I reference the data in these objects?

The "dataset32.txt" is CSV data which looks like this:

time,val
0,1.8988762857143
0.0625,0
0.125,-2.6204492857143
0.1875,-0.34179771428571

The console prints out the result of the above commands as follows:

[Object, Object, Object…]
[0 … 99]
0: Object
time: 0
value: 1.8988762857143
__proto__: Object

So how do I reference the data inside these objects: "time" and "value"?

Carlos Muñiz
  • 1,858
  • 9
  • 25
  • 29
  • I'm not sure this answer my question. The CSV file is not in JSON format. Sorry, but I'm a beginner user. I'm editing my post to include a sample of the csv data – Carlos Muñiz Feb 16 '14 at 22:51
  • Good idea. The linked question is not about JSON anyway. You either have an array of objects or an array of arrays and the other question explains how to access those (which is what you are asking about, right?). – Felix Kling Feb 16 '14 at 22:52

1 Answers1

2

Here's a straight forward method for importing your csv file using d3.

d3.csv("path to the csv", function(data, error) { }).

More info about csv.

Here's a function that's help you.

Step 1 : Importing your CSV file.

d3.csv("path to your csv file", function(data, error) {
       // You're function goes in here.
})

Step 2 : Targeting your data.

// This can be done using the data variable assigned in the function.
data.forEach(function(d) {
      // Function goes in here.
})

Step 3 : Targeting your columns.

// Suppose you have 2 columns namely time and val.
data.forEach(function(d) {
     // You can target your columns using this fucntion.
     // function(d) {return d.colname}
     // Here I'm parsing your time and val col numbers to actual numbers after importing.

     d.time = +d.time;
     d.val = +d.val;
})

Documentation for d3.

Hope this helps.

Unknown User
  • 3,598
  • 9
  • 43
  • 81