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");
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With