Accepted answer

i ended up with one of the lars kotthoff's advices.

every time when i call(axis) i also adjust text labels.
here is simplified code:

function renderaxis() {
        .call(axis)                  // draw the standart d3 axis
        .call(adjusttextlabels);     // adjusts text labels on the axis 

function adjusttextlabels(selection) {
    selection.selectall('.major text')
        .attr('transform', 'translate(' + daystopixels(1) / 2 + ',0)');

// calculate the width of the days in the timescale
function daystopixels(days, timescale) {
    var d1 = new date();
    timescale || (timescale = global.timescale);
    return timescale(, days)) - timescale(d1);

btw, here is a calendar demo with i ended up:

enter image description here


i often do this by stacking multiple axes, each with a custom .tickformat().

if i'm placing labels in between dates, i'll often do something like this:

@timedaysaxislabels = d3.svg.axis()
    .ticks(d3.time.hour.utc, 12)  # i want ticks at noon, easiest to just get them ever 12 hours
    .tickformat((d) =>
        # only draw labels at noon, between the date boundaries
        if d.getutchours() == 12
            # draw the label!
            formatter = d3.time.format.utc('%a %d %b')  # "mon 12 apr"
            return formatter(d)
            # not noon, don't draw anything
            return null)

i'll also create a separate axis with no labels at all, and a non-zero .ticksize() to actually draw ticks, but this block above positions date labels in the center of the "column".


          .attr("class", "axis axis-years")
          .attr("transform", "translate(0," + (height + 1) + ")")

     .attr("x", "-1.8em")
     .attr("y", ".00em")
     .attr("transform", function (d) {
         return "rotate(-90)"});


already a few good replies but just to add one more. note the use of text-anchor.

same idea: after your call, select the text, reposition.

.selectall(".tick text")
.style("text-anchor", "start")
.attr("x", axistextadjust)


you can do this by using axis.ticksize(major[[,minor],end]) and .ticksubdivide(). your ticks are set to line up with the major ticks, but if you set the height of these ticks to 0, and set some height for minor ticks, and specify that there is one minor tick between each pair of major ticks, you will end up with tick labels between your ticks. your code would look like this:

var myaxis = d3.svg.axis()
    .ticksize(0, 6, 0);

note that you need to explicitly set an end size. if you only provide two numbers, they will be interpreted as major and end and minor will default to 0.

here's a fiddle.


you might want to consider using d3fc, which has a drop-in replacement for the d3 axis component that supports this feature.

here's an example which substitutes the d3 axis d3.axisbottom, for the d3fc equivalent fc.axisbottom:

const axis = fc.axisbottom(linear)

the tickcenterlabel centres the axis labels as requested.

here's what the axis looks like with tickcenterlabel = false:

enter image description here

and here with the tickcenterlabel = true:

enter image description here

full disclosure - i'm a maintainer and contributor to d3fc


there is no easy (i.e. built-in) way of doing this, but you can still achieve it. there are a few options. the most straightforward one is probably to use the tickformat function to specify a format with a suitable number of spaces in front/after the numbers. this would need to be hand-tuned for each application though.

alternatively, you could select the label elements after they have been drawn and add a suitable transform attribute that shifts them accordingly. again, this would have to be hand-tuned.

your third option is to have two different axes, one for the ticks and one for the labels. the idea is that the axis that provides the ticks has no labels and the other one no ticks. you would need to set the tick values appropriately, but at least you wouldn't have to guess the right offset.

Related Query

More Query from same tag