Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js ~ Is there a way to access csv header values into a dropdown?

I'm building an interactive graph that as the axis' values has two header values. It would be really better if I could chose the axis values from a drop down menu or something like that. What I don't know is if it's possible to access the header values of my csv like if they were part of an array, so I can make a dropdown menu to select from. Given a .csv that has many header values, and all of them are strings, is it possible to achieve this by using d3.csv.formatRows(rows)? I tried to use this inside the callback function with this:

var header = d3.csv("MyCsvFile.csv").formatRows(0);
console.log(header);

but didn't work. I'm not even sure I'm using the correct function, or if there is a function to do so! Any help is appreciated! Thanks!

like image 905
tomtomtom Avatar asked Dec 08 '13 17:12

tomtomtom


1 Answers

The documentation mentions the format that d3.csv.parse produces -- basically you will have key-value pairs with the header names as the keys. So all you need to do to get the header names is something like this.

d3.csv("foo.csv", function(error, data) {
  var headerNames = d3.keys(data[0]);
});
like image 59
Lars Kotthoff Avatar answered Nov 05 '22 14:11

Lars Kotthoff