Adding FontAwesome icons to a D3 graph

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

    I know this question is old, been resolved, but - this worked for me today.

    From this site

    svg.append('svg:foreignObject')
        .attr("width", 50)
        .attr("height", 50)
        .append("xhtml:body")
        .html('<i class="fa fa-user"></i>');
    

    But for my chart, I dropped the append xhtml:body, otherwise it wouldn't let me set x and y coords.

    The element will adopt the width and height of the font you set.

    d3.select('svg')
        .append('svg:foreignObject')
        .attr('class', 'handle')
        .attr('x',  +getLeftBarPosition(i+1, 'handle')[0] + +getLeftBarPosition(i+1, 'handle')[1])
        .attr('y', state.barHeight/2)
        .html('<i class="fa fa-user"></i>')
    
    0 讨论(0)
  • 2020-11-27 12:39

    Just to put in code here what worked for me based on CarlesAndres's answer and mhd's comment:

    node.append("text")
        .attr("style","font-family:FontAwesome;")
        .attr('font-size', "50px" )
        .attr("x", 440)
        .attr("y", 440)
        .text(function(d) { return '\uf118' });
    
    0 讨论(0)
  • 2020-11-27 12:44

    For those who banging their head hard. D3 - 6.2.0 and FontAwesome - 5.13.0 Below worked

    nodeEnter.append('text')
        .attr('width', "10px" ).attr('height', "10px" ) // this will set the height and width
        .attr("class","fas fa-adjust") // Font Awesome class, any
        .attr("x", 15) // for placement on x axis
        .attr("y",-5); // for placement on y axis
    
    0 讨论(0)
提交回复
热议问题