是否可以在D3.js中找到刻度之间的距离? [英] Is it possible to find the distance between ticks in D3.js?

查看:216
本文介绍了是否可以在D3.js中找到刻度之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出x轴上刻度线之间的距离?我正在使用带有rangeRoundBands的有序刻度,它告诉我它没有刻度功能.

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

(添加了x.domain)

(edit: added x.domain)

推荐答案

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]);

这篇关于是否可以在D3.js中找到刻度之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆