Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way to find out the distance between the tick marks on the x axis? I'm using the ordinal scale with rangeRoundBands with tells me it doesn't have a tick function.

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

It generates the axis fine (can't post an image) but I can't figure out how to get the distance

(edit: added x.domain)

like image 363
AnthonyDeaver Avatar asked Jun 20 '13 19:06

AnthonyDeaver


2 Answers

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]);
like image 97
techjacker Avatar answered Oct 03 '22 01:10

techjacker


(2019) We can expand techjacker solution to cover non-linear scales, instead of calculate only the distance between two ticks, you can have an array with all distances between ticks.

// ticksDistance is constant for a specific x_scale
const getTicksDistance = (scale) => {
      const ticks = scale.ticks();
      const spaces = []
      for(let i=0; i < ticks.length - 1; i++){
        spaces.push(scale(ticks[i+1]) - scale(ticks[i]))
      }
      return spaces;
};
//you have to recalculate when x_scale or ticks change
const ticksSpacingPow = getTicksDistance(x_scale_pow);

ticksSpacingPow is array with all distances

The example below, draws an ellipse on half of the distance between the ticks.

// normal 
svg.append("g")
    .attr("class", "x-axis")
    .attr("transform", "translate(0,20)")
    .call(x_axis_pow)
    .selectAll('.tick')
    .append('ellipse')
    .attr('fill', 'red')
    .attr('rx', '2px')
    .attr('ry', '10px')
    .attr('cx', (d,i)=> ticksSpacingPow[i]/2)

ps. Using the latest D3v5

const x_scale_pow = d3.scalePow().exponent(2)
    .domain([0,20000])
    .range([0, 960]);
    
const x_axis_pow = d3.axisBottom()
    .scale(x_scale_pow)
    .ticks(10)
    
// ticksDistance is constant for a specific x_scale
const getTicksDistance = (scale) => {
      const ticks = scale.ticks();
      const spaces = []
      for(let i=0; i < ticks.length - 1; i++){
        spaces.push(scale(ticks[i+1]) - scale(ticks[i]))
      }
      return spaces;
};

//you have to recalculate when x_scale or ticks change
const ticksSpacingPow = getTicksDistance(x_scale_pow);

const svg = d3.select("body").append("svg")
    .attr("width", "500px")
    .attr("height","350px")
    .style("width", "100%")
    .style("height", "auto");

// normal 
svg.append("g")
    .attr("class", "x-axis")
    .attr("transform", "translate(0,20)")
    .call(x_axis_pow)
    .selectAll('.tick')
    .append('ellipse')
    .attr('fill', 'red')
    .attr('rx', '2px')
    .attr('ry', '10px')
    .attr('cx', (d,i)=> ticksSpacingPow[i]/2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
like image 30
calmar Avatar answered Oct 03 '22 01:10

calmar