leongersen / noUiSlider

noUiSlider is a lightweight, ARIA-accessible JavaScript range slider with multi-touch and keyboard support. It is fully GPU animated: no reflows, so it is fast; even on older devices. It also fits wonderfully in responsive designs and has no dependencies.
https://refreshless.com/nouislider/
MIT License
5.67k stars 660 forks source link

Evenly spaced range of discrete values #1199

Closed xeruf closed 2 years ago

xeruf commented 2 years ago

Hey, I would like to have a slider with a list of evenly-spaced values. Is there a way to implement that easily now without having to calculate percentages for each?

My feature suggestion here would be to have range also accept an array, which would then be evenly spaced automatically.

leongersen commented 2 years ago

Good suggestion. This would look like this:

var values = [20, 50, 60, 80, 90, 110];
var len = values.length;
var range = { min: values[0] };

for (var i = 1; i < len; i++) {
    var key;

    if (i === len - 1) {
        key = 'max';
    } else {
        key = (100 / (len - 1) * i) + '%';
    }

    range[key] = values[i];
}

This could be added as a utility, like noUiSlider.create(slider, { range: noUiSlider.buildRange([20, 50, 60, 80, 90, 110]), .... I'd just want to think about how a step value could be added.

xeruf commented 2 years ago

Wow, thank you! There is one thing I forgot to mention: What if the values are not numbers but could also be strings - would I have to do a mapping via from-to? This was what I meant with discrete values, actually.

leongersen commented 2 years ago

Yes, you'd need to set up format to translate between a numeric range and the string values (if they are not numeric).

leongersen commented 2 years ago

There is now an example on using an array of values in the documentation.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.