Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we change the tick values generated by a linear scale in a d3.js line plot?

I only had 5 values[1,2,3,4,5] as my y - coordinates in the d3.js line plot. But, I end up getting more values [0.5,1,1.5,2,2.5,3,3.5,4,4.5,5] Is there a way to edit the d3.js file or the html file inorder to plot the values as per my requirement?

like image 238
Jitendra Avatar asked May 01 '14 20:05

Jitendra


People also ask

What is scale linear in d3?

d3. scaleLinear constructs creates a scale with a linear relationship between input and output. lin = ƒ(t) By default, the scale's domain and range are both the interval [0,1], and the scale can be expressed as the identity function: y = x.

What are ticks in d3?

d3. ticks generates an array of nicely-rounded numbers inside an interval [start, stop]. The third argument indicates the approximate count of values needed. For example, to cover the extent [0, 10] with about 100 values, d3.ticks will return these ticks: start = 0.

What is tick format?

tickFormat, you create a number format with precision appropriate to the scale's tick values. When a SI-prefix format type is used (type s ), the scale also computes a consistent SI-prefix for all ticks, as shown on the left; this feature is new in 3.4.

What is scale in d3 JS?

In this sense, scales are one of the most fundamental abstractions of data visualization. Scales from the d3-scale module are functions that take as input the actual value of a measurement or property. Their output can in turn be used to encode a relevant representation.


1 Answers

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 = data.map(function(d){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.)

like image 133
AmeliaBR Avatar answered Sep 30 '22 05:09

AmeliaBR