【问题标题】:D3.JS add class to each countryD3.JS 为每个国家添加类
【发布时间】:2015-04-06 18:29:42
【问题描述】:

我正在使用它进行简单的可视化。

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

我想在每条路径中添加国家名称。我尝试遍历国家/地区,但我不知道如何与 SVG 链接。

var world = svg.selectAll("path.land")
            .data(countries)
            .enter().append("path")
            .attr("class", "land")
            .attr("d", path)

【问题讨论】:

  • 你到底想做什么?向生成的路径添加另一个类或在地球上添加文本?​​
  • 我想在路径中添加类,例如 jsfiddle.net/7buzp4ab 这里有代码
  • 这只是.attr("class", function(d) { return "land " + d.properties.name; }),不是吗?

标签: javascript svg d3.js


【解决方案1】:

您可以使用函数为每个数据项动态构建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,然后你可以从中调用任何键值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多