Javascript 反转刻度功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26172311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:27:23  来源:igfitidea点击:

Invert scale function

javascriptd3.js

提问by ngungo

This is interesting to me. Look at the following D3 code:

这对我来说很有趣。看下面的D3代码:

var scale = d3.scale.linear()
    .domain([100, 500])
    .range([10, 350]);

scale(100);  //Returns 10
scale(300);  //Returns 180
scale(500);  //Returns 350

Is there a function that inverse of scale? For example,

是否有与比例尺成反比的函数?例如,

inverseScale(10);   //Returns 100
inverseScale(180);  //Returns 300
inverseScale(350);  //Returns 500

回答by OrionMelt

Yes, there is, and it's aptly named invert.

是的,有,而且它的名字恰如其分invert

console.log(scale.invert(10));   //Returns 100
console.log(scale.invert(180));  //Returns 300
console.log(scale.invert(350));  //Returns 500