Skip to content

Instantly share code, notes, and snippets.



Last active Nov 19, 2018
What would you like to do?
Wrapping Long Labels
license: gpl-3.0

This example, using satirical data from The Onion, demonstrates how to wrap long axis labels to fit on multiple lines.

name value
Family in feud with Zuckerbergs .17
Committed 671 birthdays to memory .19
Ex is doing too well .10
High school friends all dead now .15
Discovered how to “like” things mentally .27
Not enough politics .12
<!DOCTYPE html>
<meta charset="utf-8">
.bar {
fill: steelblue;
.bar:hover {
fill: brown;
.title {
font: bold 14px "Helvetica Neue", Helvetica, Arial, sans-serif;
.axis {
font: 10px sans-serif;
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
.x.axis path {
display: none;
<script src="//"></script>
var margin = {top: 80, right: 180, bottom: 80, left: 180},
width = 960 - margin.left - margin.right,
height = 500 - - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1, .3);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
var yAxis = d3.svg.axis()
.ticks(8, "%");
var svg ="body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + + margin.bottom)
.attr("transform", "translate(" + margin.left + "," + + ")");
d3.tsv("data.tsv", type, function(error, data) {
x.domain( { return; }));
y.domain([0, d3.max(data, function(d) { return d.value; })]);
.attr("class", "title")
.attr("x", x(data[0].name))
.attr("y", -26)
.text("Why Are We Leaving Facebook?");
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.selectAll(".tick text")
.call(wrap, x.rangeBand());
.attr("class", "y axis")
.attr("class", "bar")
.attr("x", function(d) { return x(; })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.value); })
.attr("height", function(d) { return height - y(d.value); });
function wrap(text, width) {
text.each(function() {
var text =,
words = text.text().split(/\s+/).reverse(),
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
function type(d) {
d.value = +d.value;
return d;

This comment has been minimized.

Copy link

@jimkang jimkang commented Dec 8, 2013

There's a problem with wrap() if you use it multiple times on the same element. It can be avoided if you use the data as the source of the text rather than textSelection.text(). I tried out this change here:

There's also an extended explanation.


This comment has been minimized.

Copy link

@jml6m jml6m commented Nov 2, 2014

With a small change to the wrap function, you can have this code work on labels for horizontal charts as well. Note that the vertical alignment for multi-line labels will not be centered.


This comment has been minimized.

Copy link

@enactdev enactdev commented Jan 15, 2015

I edited this to make it responsive.


This comment has been minimized.

Copy link

@DougManuel DougManuel commented Nov 21, 2016

wrap() could be a candidate for a general d3js module. It strikes me as a potentially common issue for D3js users -- not only for axis labels, but tooltips, etc.

Not sure exactly what a wrap module would look like as a general d3js module. I modified the code in this gist for my project (irregular histogram) by adding width attribute to the text element and then retrieving that property in wrap var width = parseFloat(text.attr("width")). I also needed to retrieve the x within the function, in addition to y and dy. I expect that these parameters of text. could all be optional function parameters.

(I am also planning on modifying the code in this gist so that there isn't an empty line when first word is wider than width. I'd welcome advice on how best to incorporate this modification into the gist code).


This comment has been minimized.

Copy link

@pistou pistou commented Jul 11, 2017

There is an issue with you wrap function.
If you use it on a non-displayed chart (one that you can toggle for instance), the legend won't be wrapped.

This comes from getComputedTextLength() that will return 0 if the text is not displayed.

The (ugly) solution I found was to clone my text element and use that clone to compute the original legend.

function wrap(text, width) {
    text.each(function() {
        var text =;
        // clone
        let clone = document.createElement('div');
        clone.setAttribute('style', "position:absolute; visibility:hidden; width:0; height:0;");
        let svg = document.createElementNS('', 'svg');
        let elt = text.node().cloneNode(true);
        var cloneText =, // cloned text
            words = cloneText.text().split(/\s+/).reverse(),
            line = [],
            lineNumber = 0,
            lineHeight = 1.4,
            y = cloneText.attr("y"),
            x = cloneText.attr("x"),
            dy = parseFloat(cloneText.attr("dy")),
            tspan = text.text(null).append("tspan").attr("x", x).attr("y", y).attr("dy", dy + "em"),
            cloneTspan = cloneText.text(null).append("tspan").attr("x", x).attr("y", y).attr("dy", dy + "em"); // equivalent of tspan from the clone

        while (word = words.pop()) {
            tspan.text(line.join(" "));
            cloneTspan.text(line.join(" ")); // keep them equivalent
            if (cloneTspan.node().getComputedTextLength() > width) {
                tspan.text(line.join(" "));
                cloneTspan.text(line.join(" ")); // keep them equivalent
                line = [word];
                tspan = text.append("tspan").attr("x", x).attr("y", y).attr("dy", lineNumber * lineHeight + dy + "em").text(word);
                cloneTspan = cloneText.append("tspan").attr("x", x).attr("y", y).attr("dy", lineNumber * lineHeight + dy + "em").text(word); // keep them equivalent
        document.body.removeChild(clone); // remove the clone to not pollute the page

This comment has been minimized.

Copy link

@praveenuics praveenuics commented Oct 22, 2017

Is there any such link where i can find the wrap function in typescript, please. i am working with d3v4 in angular2


This comment has been minimized.

Copy link

@clchow clchow commented Oct 31, 2017

I changed the variable definition for words to use the data for that text element instead of pulling the rendered text, because I need to recall this text wrapper for showing on hover details when I zoom in and out of a timeline. Not sure if there is a better way but it seems to work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment