score:35

Accepted answer

the best way i have found to handle this is to perform the rotation of the x-axis tick labels yourself. why? because nvd3 is not handling the rotation and associated translations properly. look at your image, and you'll see that the library allows the rotation to translate the labels out from directly under the columns they represent. it also begins to mishandle the axis.tickpadding() values, and so on.

the first thing that you need to do is to ensure that the chart has enough space for the translated labels, like so:

chart.margin({bottom: 60});

we can then translate and rotate the labels:

var xticks = d3.select('.nv-x.nv-axis > g').selectall('g');
xticks
  .selectall('text')
  .attr('transform', function(d,i,j) { return 'translate (-10, 25) rotate(-90 0,0)' }) ;

the labels now look like this:

rotated / translated labels

score:0

whatever you text rotation is give start/middle/end

d3.select('.nv-x.nv-axis > g > g').selectall('g.tick text').style('text-anchor', 'start');  //start/middle/end

score:0

there is a css alternative.

.nv-x .tick text {
    transform: rotate(-90deg) translatex(-15px) translatey(-7px);
}

score:1

this worked for me. the main reason i'm submitting this is that changing the anchor is nicer than translating the position manually.

var xticks = d3.select('.nv-x.nv-axis > g > g',_this.context.element).selectall('g').selectall('text');
xticks.attr('transform', function() { return 'rotate(' + angle + ' 0,0)' }) ;
xticks.attr('text-anchor',function() {
    var anchor;
    if(angle > 0){ anchor = 'start'; }
    else if(angle < 0){ anchor = 'end'; }
    else { anchor = 'middle'; }
    return anchor;
});

score:1

the following worked for me:

chart.axislist["xaxis"]["rotatelabels"]= -45

score:5

for multiple graph, your can add "'#' + containerid + ' svg " in the d3.select like below:

//rotate x-axis label
var xticks = d3.select('#' + containerid + ' svg .nv-x.nv-axis > g').selectall('g');
xticks
    .selectall('text')
    .attr('transform', function(d,i,j) { return 'translate (-15, 60) rotate(-90 0,0)' });

anyways, thanks for @river answer.


Related Query

More Query from same tag