Escape Characters in d3.js ticks

雨燕双飞 提交于 2019-12-02 04:30:16

问题


I need to display micromoles per liter (µmol/L) in my chart's tickFormat, but when I pass in "µmol/L", it shows the characters "µ" instead of the symbol for mu. How do I get it to render the symbol?


回答1:


In that case, you shouldn't use an HTML entity. Once you're dealing with an SVG , use this:

\u00B5

Check this snippet:

var svg = d3.select("body")
	.append("svg")
	.attr("width", 500)
	.attr("height", 200);
	
var scale = d3.scaleLinear()
	.range([40, 460])
	.domain([0, 100]);
	
var axis = d3.axisBottom(scale)
	.tickFormat(function(d){ return d + "\u00B5mol/L"})
    .ticks(5);

svg.append("g")
	.attr("transform", "translate(0,100)")
	.call(axis);
text { font-size: 14px;}
<script src="https://d3js.org/d3.v4.min.js"></script>


来源:https://stackoverflow.com/questions/38734835/escape-characters-in-d3-js-ticks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!