Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3.js change x-axis path line style

Tags:

svg

d3.js

In d3.js charts, the x-axis line (black line between the bars and bar labels) sort of looks like this by default: |----------------|, see screenshot below:

Note the double line in the pink box. Very unsightly.

How would I change this to just a straight line (no vertical lines on either end)?

Looking at the generated SVG, this code seems to be determining that style: <path class="domain" d="M0,6V0H824V6"></path>, which is auto-generated by D3.

Thanks!

like image 678
ccnokes Avatar asked May 29 '15 16:05

ccnokes


2 Answers

This is controlled by axis.outerTickSize():

An outer tick size of 0 suppresses the square ends of the domain path, instead producing a straight line.

All you need to do is set axis.outerTickSize(0).

like image 61
Lars Kotthoff Avatar answered Oct 18 '22 16:10

Lars Kotthoff


Lars Kotthoff's answer is still valid for d3 versions prior to 4.x, with version 4 it changed to axis.tickSizeOuter(). Note that tickSize() modifies the outer ticks as well, which means the order of calling tickSize() and tickSizeOuter() is important.

d3.axisBottom(xScale)
    .tickValues(series)
    .tickSize(5)
    .tickSizeOuter(0)
);
like image 34
atomocopter Avatar answered Oct 18 '22 15:10

atomocopter