使用顺序标度反演 [英] Inversion with ordinal scale

查看:145
本文介绍了使用顺序标度反演的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找到顺序量表的反转?

Is there any way to find inversion of ordinal scale?

我在x轴上使用字符串值,它使用顺序标度和i对鼠标移动我想查找与x轴的反转,以找到哪个字符串在鼠标位置?

I am using string value on x axis which is using ordinal scale and i on mouse move i want to find inversion with x axis to find which string is there at mouse position?

有没有办法找到这个?

var barLabels = dataset.map(function(datum) {
    return datum.image;
});
console.log(barLabels);
var imageScale = d3.scale.ordinal()
        .domain(barLabels)
        .rangeRoundBands([0, w], 0.1);
// divides bands equally among total width, with 10% spacing.
console.log("imageScale....................");
console.log(imageScale.domain());

 .
 .
var xPos = d3.mouse(this)[0];
xScale.invert(xPos);


推荐答案

我认为没有意义不是顺序缩放的反转方法,但您可以使用ordinal.range()方法计算出来,它将返回每个栏的起始值,以及ordinal.rangeBand )方法的宽度。

I actually think it doesn't make sense that there isn't an invert method for ordinal scales, but you can figure it out using the ordinal.range() method, which will give you back the start values for each bar, and the ordinal.rangeBand() method for their width.

示例:
http: //fiddle.jshell.net/dMpbh/2/

相关代码为

    .on("click", function(d,i) {
        var xPos = d3.mouse(this)[0];
        console.log("Clicked at " + xPos);
        //console.log(imageScale.invert(xPos));
        var leftEdges = imageScale.range();
        var width = imageScale.rangeBand();
        var j;
        for(j=0; xPos > (leftEdges[j] + width); j++) {}
            //do nothing, just increment j until case fails
        console.log("Clicked on " + imageScale.domain()[j]);
    });

这篇关于使用顺序标度反演的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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