Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Slider logarithmic scale

I'm using this jQuery UI code use for a logarithmic slider:

var minVal = 10;
var maxVal = 100;
$("#slider").slider({
    range: true,
    min: minVal,
    max: maxVal / 2,
    values: [minVal, maxVal],
    slide: function(event, ui) {
        $("#amount_min").val(Number(expon(ui.values[0], minVal, maxVal)).toFixed(0));
        $("#amount_max").val(Number(expon(ui.values[1], minVal, maxVal)).toFixed(0));
    }
});   

The expon function is:

function expon(val, min,max) {
    var minv = Math.log(min);
    var maxv = Math.log(max);
    max = max / 2;

    // calculate adjustment factor
    var scale = (maxv - minv) / (max - min);

    return Math.exp(minv + scale * (val - min));
}

#amount_min and #amount_max are HTML input elements. The code above works just fine to get values from the slider and to put them to the input elements.

But now I need the function opposite to expon() — to change the slider when I change the values of the inputs. Can anybody help me with this?

like image 834
Taras Bulgakov Avatar asked Nov 06 '11 03:11

Taras Bulgakov


2 Answers

I found the solution here: Logarithmic slider

I've used your very helpful code for a project... this is what I have:

var gMinPrice = 100;
var gMaxPrice = 1000; //in my project these are dynamic

function expon(val){
    var minv = Math.log(gMinPrice);
    var maxv = Math.log(gMaxPrice);
    var scale = (maxv-minv) / (gMaxPrice-gMinPrice);
    return Math.exp(minv + scale*(val-gMinPrice));

}
function logposition(val){
    var minv = Math.log(gMinPrice);
    var maxv = Math.log(gMaxPrice);
    var scale = (maxv-minv) / (gMaxPrice-gMinPrice);
    return (Math.log(val)-minv) / scale + gMinPrice;
}

I then change my slider positions using this code:

var gBudgetBottom, gBudgetTop;
$('#priceSlider').slider({ 
    change: function(event, ui){ // inside your slider instance
        gBudgetBottom = Number(expon(ui.values[0])).toFixed(0);
        gBudgetTop = Number(expon(ui.values[1])).toFixed(0);
    }
}

$('#priceSlider').slider({ values: [Number(logposition(gBudgetBottom)).toFixed(0), Number(logposition(gBudgetTop)).toFixed(0)] });

I hope that works for you :)

like image 105
ness-EE Avatar answered Sep 18 '22 05:09

ness-EE


This is what i did:

var resStepValues = [0, .01, .02, .03, .04, .05, 1, 1.5, 2, 3, 4, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 250, 300, 500, 750, 1000, 1500];

var slider = $("#resolution_slider").slider({
    animate: true,
    min: 0,
    max: 29,
    range: 'min',
    values: [0, 29],
    slide: function (event, ui) {

        if (ui.values[0] > ui.values[1]) {
            $("#txtMaxRes").val(resStepValues[ui.values[1]]);
            $("#txtMinRes").val(resStepValues[ui.values[0]]);

        } else {
            $("#txtMaxRes").val(resStepValues[ui.values[0]]);
            $("#txtMinRes").val(resStepValues[ui.values[1]]);
        }
    }

});
like image 35
capdragon Avatar answered Sep 21 '22 05:09

capdragon