x.scale = d3.time.scale().domain(x.extent).range([0, dimensions.graph.width]);
This code uses x.extent
([Wed Aug 01 2012 00:00:00 GMT+0300 (EEST), Tue Aug 07 2012 00:00:00 GMT+0300 (EEST)]
) and graph width (1000) to generate x
value scale. However, I need this value to be rounded to the nearest multiple of 25 (Math.round(x/25)*25
).
By doing this I want to achieve exact width ticks that are now defined as:
x.axis = d3.svg.axis()
.ticks(d3.time.days, 1)
.tickSize(10, 5, 0)
.scale(x.scale);
How to extend x.scale
to round to the nearest multiple of 25?
If the given number is between the two multiples of ten, it is rounded up to the higher multiple. So 5 is rounded up to 10. Note: To round off a number to the nearest tens, we round off it to the nearest multiple of ten.
If you need to round a number to the nearest multiple of 5, you can use the MROUND function and supply 5 for number of digits. The value in B6 is 17 and the result is 15 since 15 is the nearest multiple of 5 to 17.
To round a number to the nearest 5, call the Math. round() function, passing it the number divided by 5 and multiply the result by 5 .
The correct answer is interpolateRound
, from the docs
Returns an interpolator between the two numbers a and b; the interpolator is similar to
interpolateNumber
, except it will round the resulting value to the nearest integer.
var xScale = d3.scaleLinear()
.domain([0, 100])
.range([0, width])
.interpolate(d3.interpolateRound); // <-- round interpolation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With