Home > OS >  how to add html special character in d3.js?
how to add html special character in d3.js?

Time:09-13

I want to add degree html code °

      const degreeNum = d3
        .select(`#windBox${order}`)
        .append("text")
        .attr("x", 250)
        .attr("y", 130)
        .style("font", "bold 50px sans-serif")
        .style("fill", "url(#lgTextDegree)")
        .text(`${degree}`); // here i want to add html code

i try Template literals like this

.text(`${°}`)

but not working. How to solve this problem??

CodePudding user response:

try .text(String.fromCharCode(176))

d3.select("#my\\~div_chart").on("click", function(){
    d3.select(this).text(String.fromCharCode(176))
});
<script src="https://d3js.org/d3.v4.min.js"></script>
<div id="my~div_chart">Click Me</div>

Note the use of escape characters (\\) to escape the ~ symbol

  • Related