How to make the scale rounded to the nearest multiple?

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 the scale x values. 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, which 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 rounding to the nearest multiple of 25?

+4
source share
2 answers

Are you looking for nice function?

 x.scale = d3.time.scale().domain(x.extent) .range([0, dimensions.graph.width]).nice(); 
+1
source

Perhaps I misunderstand, but could you just transform the result, as you suggested after applying the zoom function? i.e:

 function roundScale(origVal) { return Math.round(x.scale(origVal)/25) * 25;} 

then use this value to set the attributes:

 .attr("x", function(d) { return roundScale(d.value); } 
0
source

All Articles