Skip to content Skip to sidebar Skip to footer

Escape Characters In D3.js Ticks

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.

Solution 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;}
<scriptsrc="https://d3js.org/d3.v4.min.js"></script>

Post a Comment for "Escape Characters In D3.js Ticks"