最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - D3.js text element does not display unicode character correctly - Stack Overflow

programmeradmin0浏览0评论

I'm trying to append ➤ into SVG using unicode as seen below.

g.append("text")
        .attr("x", 10)
        .attr("y", 10)
        .text("➤");

➤ is displayed, but not ➤

I'm trying to append ➤ into SVG using unicode as seen below.

g.append("text")
        .attr("x", 10)
        .attr("y", 10)
        .text("➤");

➤ is displayed, but not ➤

Share Improve this question edited Aug 4, 2015 at 11:59 CuriousSuperhero asked Aug 4, 2015 at 7:47 CuriousSuperheroCuriousSuperhero 6,6714 gold badges29 silver badges52 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

Instead of using .text() use .html()

➤ is HTML/XML markup. You're not inserting into HTML/XML source code, so these escapes don't do anything. You can just do:

.text('➤');

And save the file in the same encoding the browser will be reading it as (typically, UTF-8, for a page with <meta charset="utf-8"/>.

If you really can't use non-ASCII characters—eg because your script needs to be included from multiple pages with different character sets, or because your text editor doesn't support Unicode—then the type of escape you want to use is JavaScript string literal encoding. ➤ is U+27A4 Black Rightwards Arrowhead, so:

.text('\u27A4');
发布评论

评论列表(0)

  1. 暂无评论