Default D3 Chart
By default, d3.ScaleLinear tries to create 10 ticks using the d3.ticks(start, stop, count) function.
Want to know how the nice function works? . See: Nice function demo
The d3.ticks(start, stop, count) function.
Returns an array of approximately count + 1 uniformly-spaced, nicely-rounded values between
start and stop (inclusive). Each value is a power of ten multiplied by 1, 2 or 5.
The ticks are generated using the following algorithm:
- The raw step size is calculated like:
- Find the magnitude of the step size by taking the base-10 logarithm of the step and
flooring it :
The parentheses around log10(step) mean a floor function
- Adjust the step size to be a multiple of 1, 2, or 5 of the order of magnitude:
Determine the factor
Error = 4.3 -> factor = 5 =
= =
0
= =
9
i1 and i2 are the indices that represent the first (i1) and the last (i2) tick values in the range between start and stop.
- Resulting ticks = 5,10,15,20,25,30,35,40,45
The full code also handles the cases where start is greater then stop. And when power is
negative. See: github.com/d3/d3-array/blob/src/ticks.js