问题描述
有没有办法找到序数尺度的反转?
Is there any way to find inversion of ordinal scale?
我在 x 轴上使用字符串值,它使用序数刻度,我在鼠标移动时我想找到 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?
有什么办法可以找到这个吗?
Is there any way to find this?
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]);
});
这篇关于序数倒置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!