Adding FontAwesome icons to a D3 graph

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

    Thanks to all that replied. My final solution is based on the answer by CarlesAndres:

    g.append('text')
        .attr('text-anchor', 'middle')
        .attr('dominant-baseline', 'central')
        .attr('font-family', 'FontAwesome')
        .attr('font-size', '20px')
        .text(function(d) { return ICON_UNICODE[d.nodeType]; });
    

    Be careful with your CSS: it takes precedence over the SVG attributes.

    And this is the way it looks:

    Node Graph

    The good thing about this, compared to the foreignObject solution, is that events are properly handled by D3.

提交回复
热议问题