Adding FontAwesome icons to a D3 graph

后端 未结 9 2258
既然无缘
既然无缘 2020-11-27 12:07

I am trying to set an icon with FontAwesome instead of text in my D3 nodes. This is the original implmentation, with text:

g.append(\'svg:text\')
    .attr(\         


        
9条回答
  •  隐瞒了意图╮
    2020-11-27 12:30

    Given that the other answers don't work anymore (because d3js has been updated in the meanwhile) and because it's not a good solution to use svg:foreignObject due to compatability issues, here is an answer that works without having to use any hacks:

    .append("text")      // Append a text element
    .attr("class", "fa") // Give it the font-awesome class
    .text("\uf005");     // Specify your icon in unicode (https://fontawesome.com/cheatsheet)
    

    Here is a working example (click "Run code snippet" and the d3 code outputs three stars):

    var icons = [1, 2, 3];
    
    d3.select("body")
      .selectAll(".text")
      .data(icons)
      .enter()
      .append("text")       // Append a text element
      .attr("class", "fa")  // Give it the font-awesome class
      .text("\uf005");      // Specify your icon in unicode
    
    
    

提交回复
热议问题