Unable to display a degree char (°) in a SVG

Viewed 908

I'm trying to display a degree character (°) in a SVG manipulated by D3.js. So far I've tried different charsets but I'm always getting a � instead and the following character codes simply display themselves as regular text: ° or °.

I'm using <meta charset="UTF-8">.

How would you display that symbol? Maybe there's a SVG property I could set?

1 Answers

I can't see why this should be a problem. You said you added <meta charset="UTF-8"> to the HTML file, but did you actually save it using UTF-8 encoding? Some plain text editors default to other character sets like iso-8859-1, so you might have incorrect character codes in there somewhere. If your Javascript is stored in a separate file, make sure that is properly encoded too.

Also bear in mind that character entities like &deg; won't work in SVG files.

var svg = d3.select("body")
  .append("svg")
  .attr("width", 400)
  .attr("height", 100);

svg.append("text")
  .text("Water boils at 100°C")
  .attr("x", 20)
  .attr("y", 50)
  .attr("font-family", "sans-serif")
  .attr("font-size", "40px");
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Related