Search code examples
javascriptangularjsangularjs-directivesliderangularjs-slider

Negative AngularJS Slider with custom Scale


I'm using the custom Scale of AngularJS Slider, as you can see here: JSFIDDLE

Now, what I want to is to start from -1. So I changed the options from:

options: {
  floor: 0,
  ceil: 5,
  step: 1
}

To:

options: {
  floor: -1,
  ceil: 5,
  step: 1
}

But this is the result: JSFIDDLE

As you can see, it starts from '0' value and then overlaps '1' and '-1'. How can I have the correct sequence? (-1, 0, 1, 2, 3, 4, 5)


Solution

  • Keep sign while powing

      customValueToPosition: function(val, minVal, maxVal) {
         val = Math.sign(val) * Math.pow(val,2)
         maxVal = Math.sign(maxVal) * Math.pow(maxVal, 2)
          minVal = Math.sign(minVal) * Math.pow(minVal, 2)
          var range = maxVal - minVal
          return (val - minVal) / range
        },
      customPositionToValue: function(percent, minVal, maxVal) {
        minVal = Math.sign(minVal) * Math.pow(minVal,2);
        maxVal = Math.sign(maxVal) * Math.pow(maxVal,2);
        var value = percent * (maxVal - minVal) + minVal;
        return Math.sqrt(value)
      }
    

    PLUNKER (NOT JSFIDDLE) :)