2015-02-06 17 views
4

我正在使用它來進行簡單的可視化。D3.JS爲每個國家添加課程

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

我想每個國家的道路他的名字添加。我試圖遍歷國家,但我不知道要與SVG鏈接。

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

究竟是什麼你想怎麼辦?將另一個類添加到生成的路徑或在地球上添加文本? – 2015-02-06 15:14:23

+0

我想將類添加到路徑中,例如 http://jsfiddle.net/7buzp4ab/這裏有代碼生活 – Marius 2015-02-06 15:15:00

+0

這可能嗎? – Marius 2015-02-06 15:24:22

回答

4

您可以使用函數構建class屬性動態地對每個數據項:

var world = svg.selectAll("path.land") 
      .data(countries) 
      .enter().append("path") 
      .attr("class", function(d) { return "land " + d.name }) 
      .attr("d", path) 
+0

@Marius:只有一個函數調用當前對象'd',然後你可以調用任何鍵值。 – Hugolpz 2015-02-06 20:57:46