Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the ticks on x-axis

Tags:

d3.js

I am trying to figure out d3.js. While defining the axis, How can I get custom labeling on x-axis. For example, the default labeling I get is:

|------|------|------|------|------|------| 20    30     40     50     60     70     80 

Whereas, I want something like:

|------|------|------|------|------|------| .... 20    26      32    38     44     50     56 

I am currently learning it and working on code (slightly modified) from official examples supplied:

var xAxis = d3.svg.axis().scale(x).tickPadding(7).orient("bottom"); var yAxis = d3.svg.axis().scale(y).tickPadding(5).orient("left"); 
like image 241
WeaklyTyped Avatar asked Oct 27 '12 12:10

WeaklyTyped


People also ask

How do you set the x-axis ticks?

Specify x-Axis Tick Values for Specific AxesCall the nexttile function to create the axes objects ax1 and ax2 . Plot random data into each axes. Then set the x-axis tick values for the lower plot by passing ax2 as the first input argument to the xticks function.

How do you change the x-axis ticks in Python?

To plot the line chart, use the plot() method. To rotate the ticks at the x-axis, use the plt. xticks() method and pass the rotation argument to it.

How do I move the axis ticks in Excel?

On the Format tab, in the Current Selection group, click Format Selection. In the Axis Options panel, under Tick Marks, do one or more of the following: To change the display of major tick marks, in the Major tick mark type box, click the tick mark position that you want.


1 Answers

The default ticks for quantitative scales are multiples of 2, 5 and 10. You appear to want multiples of 6; though unusual, this could make sense depending on the nature of the underlying data. So, while you can use axis.ticks to increase or decrease the tick count, it will always return multiples of 2, 5 and 10 — not 6.

If you want multiples of 6, you can use axis.tickValues to specify the tick values explicitly. This is typically used in conjunction with d3.range. For example:

xAxis.tickValues(d3.range(20, 80, 4)); 

If you want to compute the values dynamically, use the x-scale’s domain to retrieve the lowest and highest value. Also keep in mind that the upper bound of the range is exclusive, so in the above example the largest tick value is 76. You can make the upper bound inclusive by making the stop value of the range slightly bigger (e.g., 81).

like image 100
mbostock Avatar answered Sep 21 '22 01:09

mbostock