JavaScript的滑块加权值 [英] javascript slider weighted values

查看:126
本文介绍了JavaScript的滑块加权值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输出取决于其位置0和1之间的值的JavaScript的滑块。欲将该值转换为一个值上的另一个尺度之间说100和1000,但基于在100和1000的一组数据点的分布

I have a JavaScript slider that outputs a value between 0 and 1 depending on its position. I want to convert that value to a value on another scale between say 100 and 1000, but based on the distribution of a set of data points between 100 and 1000.

用例这里是我想要的时候有一个非常密切的一组数字的滑块是变化不敏感。例如...让我们说,在规模的值是:

The use case here is that I want the slider to be less sensitive to changes when there is a very close set of numbers. Eg... let's say the values in the scale are:

100, 200, 300, 500, 1000

的值100-500可能占用,也就是说,前80%的滑块由于其接近分布,因此使得更容易在它们之间进行选择。

The values 100-500 might take up, say, the first 80% of the slider due to their closer distribution, therefore making it easier to select between them.

有明确的数学函数来计算这个,可能涉及标准偏差和系数。任何人都知道它是什么?

There's clearly a mathematical function for calculating this, perhaps involving standard deviation and coefficients. Anyone know what it is?

推荐答案

在大规模使用值之间的线性插值,像

Using linear interpolation between the values on the large scale, something like

var bigValues = [100, 200, 300, 500, 1000];
var input; /* will vary in the range [0.0..1.0] */
var idx; /* index of nearest entry in bigValues below input */
var frac; /* fraction [0.0..1.0) in interval in bigValues */
var output;
/* try some test values for input */
for (var i = 0; i<=20; i++) {
    input = i * 0.05;
    idx = Math.floor(input * (bigValues.length - 1));

    frac = (input - (idx) / (bigValues.length - 1)) * (bigValues.length - 1);
    if (frac == 0) { /* no need to calculate */
        output = bigValues[idx];
    }
    else {
        output = bigValues[idx] + (bigValues[idx+1] - bigValues[idx]) * frac;
    };
    document.write(input + ', ' + output + '<br />');
}

这篇关于JavaScript的滑块加权值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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