Commit f23909fe authored by Valentin Hervieu's avatar Valentin Hervieu Committed by Valentin Hervieu

feat(logScale): Implement a logScale option to display sliders with logarithmic scale

parent 37a4f901
...@@ -137,6 +137,16 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) { ...@@ -137,6 +137,16 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
} }
}; };
//Slider config with logarithmic scale
$scope.slider_log = {
value: 1,
options: {
floor: 1,
ceil: 100,
logScale: true
}
};
//Right to left slider with floor, ceil and step //Right to left slider with floor, ceil and step
$scope.slider_floor_ceil_rtl = { $scope.slider_floor_ceil_rtl = {
value: 12, value: 12,
......
...@@ -119,6 +119,14 @@ ...@@ -119,6 +119,14 @@
></rzslider> ></rzslider>
</article> </article>
<article>
<h2>Slider with logarithmic scale</h2>
<rzslider
rz-slider-model="slider_log.value"
rz-slider-options="slider_log.options"
></rzslider>
</article>
<article> <article>
<h2>Right to left slider with custom floor/ceil/step</h2> <h2>Right to left slider with custom floor/ceil/step</h2>
<rzslider <rzslider
......
...@@ -66,6 +66,7 @@ ...@@ -66,6 +66,7 @@
getTickColor: null, getTickColor: null,
getPointerColor: null, getPointerColor: null,
keyboardSupport: true, keyboardSupport: true,
logScale: false,
scale: 1, scale: 1,
enforceStep: true, enforceStep: true,
enforceRange: false, enforceRange: false,
...@@ -836,6 +837,8 @@ ...@@ -836,6 +837,8 @@
this.precision = +this.options.precision; this.precision = +this.options.precision;
this.minValue = this.options.floor; this.minValue = this.options.floor;
if (this.options.logScale && this.minValue === 0)
throw new Error("Can't use floor=0 with logarithmic scale");
if (this.options.enforceStep) { if (this.options.enforceStep) {
this.lowValue = this.roundStep(this.lowValue); this.lowValue = this.roundStep(this.lowValue);
...@@ -1429,10 +1432,19 @@ ...@@ -1429,10 +1432,19 @@
* @returns {number} * @returns {number}
*/ */
valueToOffset: function(val) { valueToOffset: function(val) {
if (this.options.rightToLeft) { var sanitizedValue = this.sanitizeValue(val);
return (this.maxValue - this.sanitizeValue(val)) * this.maxPos / this.valueRange || 0; if (!this.options.logScale) {
if (this.options.rightToLeft) {
return (this.maxValue - sanitizedValue) * this.maxPos / this.valueRange || 0;
}
return (sanitizedValue - this.minValue) * this.maxPos / this.valueRange || 0;
}
else {
var minLog = Math.log(this.minValue),
maxLog = Math.log(this.maxValue),
scale = (maxLog - minLog) / (this.maxPos);
return (Math.log(sanitizedValue) - minLog) / scale || 0;
} }
return (this.sanitizeValue(val) - this.minValue) * this.maxPos / this.valueRange || 0;
}, },
/** /**
...@@ -1452,10 +1464,18 @@ ...@@ -1452,10 +1464,18 @@
* @returns {number} * @returns {number}
*/ */
offsetToValue: function(offset) { offsetToValue: function(offset) {
if (this.options.rightToLeft) { if (!this.options.logScale) {
return (1 - (offset / this.maxPos)) * this.valueRange + this.minValue; if (this.options.rightToLeft) {
return (1 - (offset / this.maxPos)) * this.valueRange + this.minValue;
}
return (offset / this.maxPos) * this.valueRange + this.minValue;
}
else {
var minLog = Math.log(this.minValue),
maxLog = Math.log(this.maxValue),
scale = (maxLog - minLog) / (this.maxPos);
return Math.exp(minLog + scale * offset);
} }
return (offset / this.maxPos) * this.valueRange + this.minValue;
}, },
// Events // Events
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment