Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the value for the final tick on axis in D3?

Tags:

axis

d3.js

I am using d3.time.scale to generate x-axis, and the domain ranges from 1980 to 2013 (year) However, tick value only displays upto 2012, not 2013.

I tried .ticks(), but the final tick value does not display until tick count is 25...

Although I can add the final value by manually increasing the domain upto 2014, is there a way to display the tick value for the last tick?

xAxis = d3.svg.axis()
        .scale(xScale).tickSize(20)

var maxYear = format.parse('2014')

xScale = d3.time.scale().domain([min,max])
    .range([100,width-200])    //domain = [1980 ~ 2013] 


var min = d3.min(data, function(d) {
    return d.Year;              // + operator converts string to number.
})

var max = d3.max(data, function(d) {
    return d.Year;
})

Thank you in advance.

like image 757
user3562812 Avatar asked Oct 23 '14 21:10

user3562812


People also ask

What is d3 tick?

ticks should be a format specifier string; see d3. format. If the format specifier does not have a precision, the scale will chose an appropriate precision based on the interval between ticks. This guarantees that each tick has a distinct label, and that each label is formatted concisely but consistently.

What is scaleLinear in d3?

The d3. scaleLinear() method is used to create a visual scale point. This method is used to transform data values into visual variables.

What is domain and range in d3?

The domain is the complete set of values, so in this case that is all of your temperatures, from 33 to 64. The range is the set of resulting values of a function, in this case the resulting values of scaling your temperatures from 0 to 600.

What is d3 range?

d3. range returns an array of evenly-spaced numbers. In its simplest form, it returns the integers from zero to the specified end minus one. Array(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


1 Answers

Have you tried using .nice()?

https://github.com/d3/d3-scale/blob/master/README.md#time_nice

This should do the job:

xScale = d3.time.scale().domain([min,max])
    .range([100,width-200])    //domain = [1980 ~ 2013]
    .nice() 
like image 72
Mehdi Avatar answered Sep 28 '22 07:09

Mehdi