简体   繁体   中英

how to add standard google fonts to D3.js svg text element

How do I add google fonts to d3 text element, for example:

   g.append('text')
      .attr('x', width / 2)
      .attr('y', height / 3)
      .text('roboto')
      .style("font-family", "roboto")
      .style("font-size", "80px")
      .attr("fill", "grey");

This is separate to the below question which focuses on custom type face

Using google fonts with d3.js and SVG

The straight forward solution:

Use the link element, as shown below, in your layout's head

 <link href='https://fonts.googleapis.com/css?family=Inconsolata:700' rel='stylesheet' type='text/css'>

This imports the font in question, and then within your svg element add the font family:

 svg.append('text')
      .text('Inconsolata')
      .style("font-family", "Inconsolata")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM