Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.JS add class to each country

I'm using this for a simple visualisation.

http://bl.ocks.org/KoGor/5994804

I want to add the country's name to each path. I've tried to loop over countries, but I have no idea how to link with the SVG.

var world = svg.selectAll("path.land")
            .data(countries)
            .enter().append("path")
            .attr("class", "land")
            .attr("d", path)
like image 818
Marius Avatar asked Feb 06 '15 15:02

Marius


1 Answers

You can use a function to build the class attribute dynamically for each data item:

var world = svg.selectAll("path.land")
            .data(countries)
            .enter().append("path")
            .attr("class", function(d) { return "land " + d.name })
            .attr("d", path)
like image 136
joews Avatar answered Oct 21 '22 07:10

joews