Created
July 3, 2013 16:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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