Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the scale to round to the nearest multiple of #?

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?

like image 364
Gajus Avatar asked Aug 25 '12 20:08

Gajus


People also ask

How do you round to the nearest multiple?

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.

How do you find the nearest multiple of 5?

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.

How do you round to the nearest multiple of 5 in Javascript?

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 .


1 Answers

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
like image 118
amd Avatar answered Oct 25 '22 04:10

amd