D3.JS给每个国家添加类

5
我将用于简单的可视化。 http://bl.ocks.org/KoGor/5994804 我想要向每个路径添加国家名称。我尝试循环遍历国家,但不知道如何与SVG链接。
var world = svg.selectAll("path.land")
            .data(countries)
            .enter().append("path")
            .attr("class", "land")
            .attr("d", path)

你到底想做什么?是在生成的路径上添加另一个类,还是在地球仪上添加文本? - Lars Kotthoff
我想给路径添加类,例如 <path class="land india" d="..."> 在这里你可以看到代码的实时演示:http://jsfiddle.net/7buzp4ab/ - Marius
1
这只是简单地 .attr("class", function(d) { return "land " + d.properties.name; }),对吧? - Lars Kotthoff
1个回答

8
您可以使用一个函数为每个数据项动态构建class属性:
var world = svg.selectAll("path.land")
            .data(countries)
            .enter().append("path")
            .attr("class", function(d) { return "land " + d.name })
            .attr("d", path)

@Marius:只需调用当前对象 d 的函数,然后您就可以调用任何键值。 - Hugolpz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接