Adding FontAwesome icons to a D3 graph

后端 未结 9 2207
既然无缘
既然无缘 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:34

    I'm truly new to d3, but font awesome works by styling an element with a class attribute.

    The only way I found is to append a foreignObject and set on it the relevant HTML needed by font awesome.

    Reference:

    https://developer.mozilla.org/en-US/docs/Web/SVG/Element/foreignObject?redirectlocale=en-US&redirectslug=SVG%2FElement%2FforeignObject

    http://fortawesome.github.io/Font-Awesome/examples/

    Code:

    g.append('svg:foreignObject')
        .attr("width", 100)
        .attr("height", 100)
        .append("xhtml:body")
        .html('');
    

    Demo: http://jsbin.com/eFAZABe/3/

提交回复
热议问题