Skip to content

Instantly share code, notes, and snippets.

@cherihung
Created July 3, 2013 16:02
Show Gist options
  • Save cherihung/5919796 to your computer and use it in GitHub Desktop.
Save cherihung/5919796 to your computer and use it in GitHub Desktop.
Translate data from CSV to d3nest style data objects, when d3.nest() does not produce the desire objects. Or when you need to customize the returned objects for any reason, the class function is handy.
function drawMap(id, ds) {
//load data
d3.csv(ds, function(data) {
var nodes = classes(data);
for (var i=0;i<1;i++){
d3.select('#'+id)
.selectAll('#'+id+' li')
.data(nodes)
.enter()
.append("li")
.attr("id",function(d){return d.abb.trim()})
.attr("class",function(d){console.log(d.selected); return d.selected})
.attr("data-state",function(d){return d.abb.trim()})
.text(function(d){return d.letter});
}
});
//translate csv into nodes
function classes(nodes) {
var mapdata = [];
//Lazily compute children.
nodes.forEach(function(d) {
mapdata.push({
letter: d.letter,
abb: d.abb,
state: d.fullname,
selected: d.selected});
});
return mapdata;
}
}; //end drawmap();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment