What is the logic behind d3.js nice() ticks

Razzildinho picture Razzildinho · Jan 21, 2016 · Viewed 13.4k times · Source

I have generated some charts in d3.js. I use the following code to calculate the values to put in my y axis which works like a charm.

var s = d3.scale.linear().domain([minValue, maxValue]);
var ticks = s.nice().ticks(numberOfPoints);

However I now have to write python code using pycairo which generates clones of the d3.js charts on the server side.

My question is does anybody know the logic used in the above code, or something that can give similar results, so that I can replicate it in python to get nice values to use in my y axis?

Answer

Lars Kotthoff picture Lars Kotthoff · Jan 21, 2016

D3 is open source, so you can look up the implementation. It basically boils down to rounding the extreme values:

domain[i0] = nice.floor(x0);
domain[i1] = nice.ceil(x1);