Yes you can also try


It does the trick of eliminating the decimal numbers, does not effect number of ticks

Here is a good resource for the format of the numbers using D3.


The tick marks created by a d3 axis can be controlled in two ways:

  • Using axis.tickValues(arrayOfValues) you can explicitly set the values that you want to show up on the axis. The ticks are positioned by passing each value to the associated scale, so the values should be within your scale's domain. This works for any type of scale, including ordinal scales, so long as the values you give are appropriate to that scale.

  • Alternately, using axis.ticks(parameters) you can modify the way the scale calculates tick marks. The types of parameters you can use depends on the type of scale you're using -- the values you specify will be passed directly to the scale's .ticks() method, so check the documentation for each scale type. (Parameters will be ignored for ordinal scales, which don't have a ticks() method.)

    For linear scales, the scale.ticks() method accepts a number as a parameter; the scale then generates approximately that many ticks, evenly spaced within the domain with round number values. If you do not specify a tick count, the default is to create approximately 10 ticks, which is why you were getting ticks on 0.5 intervals when your domain was from 0 to 5.

So how do you get the behaviour you want (no decimal tick values)?

  • Using .tickValues(), you would create an array of unique Y-values to be your ticks:

    var yValues ={return d.y;}); 
             //array of all y-values
    yValues = d3.set(yValues).values(); 
             //use a d3.set to eliminate duplicate values
    yAxis.tickValues( yValues );

    Be aware that this approach will use the specified y values even if they aren't evenly spaced. That can be useful (some data visualization books suggest using this approach as an easy way of annotating your graph), but some people may think your graph looks messy or broken.

  • Using .ticks(), you would figure out the extent of your Y domain, and set the number of ticks so that you do not have more tick marks then you have integers available on your domain:

    var yDomain = yScale.domain(); 
    yAxis.ticks( Math.min(10, (yDomain[1] - yDomain[0]) );

    This will create the default (approximately 10) ticks for wide domains, but will create one tick per integer value when the difference between the max and min of your domain is less than 10. (Although the tick count is usually approximate, the scale will always prefer integer values if that matches the tick count specified.)

Related Query

More Query from same tag