Is it possible to find the distance between ticks in D3.js?

Is there any way to know the distance between the marks on the x axis? I use ordinal scale with rangeRoundBands, and says it has no tick function.

var x= d3.scale.ordinal().rangePoints([_margin.left, cWidth]); x.domain(['Dec','Jan']); var testTicks = x.ticks(2); 

It generates an axis perfectly (cannot accommodate the image), but I cannot figure out how to get the distance

(edit: added by x.domain)

+6
source share
1 answer
 var data = [45, 31, 23], // whatever your data is graphHeight = 400, // however many ticks you want to set numberTicksY = 4, // set y scale // (hardcoded domain in this example to min and max of data vals > you should use d3.max real life) y = d3.scale.linear().range(graphHeight, 0]).domain(23, 45), yAxis = d3.svg.axis().scale(y).orient("left").ticks(numberTicksY), // eg returns -> [20, 30, 40, 50] tickArr = y.ticks(numberTicksY), // use last 2 ticks (cld have used first 2 if wanted) with y scale fn to determine positions tickDistance = y(tickArr[tickArr.length - 1]) - y(tickArr[tickArr.length - 2]); 
+7
source

Source: https://habr.com/ru/post/947777/


All Articles