最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - D3.JS add class to each country - Stack Overflow

programmeradmin2浏览0评论

I'm using this for a simple visualisation.

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)

I'm using this for a simple visualisation.

http://bl.ocks/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)
Share Improve this question edited Dec 2, 2021 at 11:06 Shafkhan 5247 silver badges24 bronze badges asked Feb 6, 2015 at 15:09 MariusMarius 1412 silver badges8 bronze badges 3
  • What exactly are you trying to do? Add another class to the generated paths or add text on the globe? – Lars Kotthoff Commented Feb 6, 2015 at 15:14
  • I want to add class to the path, for example <path class="land india" d="..."> jsfiddle/7buzp4ab here you have the code live – Marius Commented Feb 6, 2015 at 15:15
  • 1 This would be simply .attr("class", function(d) { return "land " + d.properties.name; }), no? – Lars Kotthoff Commented Feb 6, 2015 at 15:27
Add a ment  | 

1 Answer 1

Reset to default 8

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)
发布评论

评论列表(0)

  1. 暂无评论