Skip to content

Commit e86204e

Browse files
Valentin HervieuValentinH
authored andcommitted
feat(logScale): Implement a logScale option to display sliders with logarithmic scale
1 parent edf62b0 commit e86204e

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

demo/demo.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
158158
}
159159
};
160160

161+
//Slider config with logarithmic scale
162+
$scope.slider_log = {
163+
value: 1,
164+
options: {
165+
floor: 1,
166+
ceil: 100,
167+
logScale: true
168+
}
169+
};
170+
161171
//Right to left slider with floor, ceil and step
162172
$scope.slider_floor_ceil_rtl = {
163173
value: 12,

demo/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ <h2>Slider with custom floor/ceil/step</h2>
127127
></rzslider>
128128
</article>
129129

130+
<article>
131+
<h2>Slider with logarithmic scale</h2>
132+
<rzslider
133+
rz-slider-model="slider_log.value"
134+
rz-slider-options="slider_log.options"
135+
></rzslider>
136+
</article>
137+
130138
<article>
131139
<h2>Right to left slider with custom floor/ceil/step</h2>
132140
<rzslider

src/rzslider.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
getTickColor: null,
6666
getPointerColor: null,
6767
keyboardSupport: true,
68+
logScale: false,
6869
scale: 1,
6970
enforceStep: true,
7071
enforceRange: false,
@@ -835,6 +836,8 @@
835836
this.precision = +this.options.precision;
836837

837838
this.minValue = this.options.floor;
839+
if (this.options.logScale && this.minValue === 0)
840+
throw new Error("Can't use floor=0 with logarithmic scale");
838841

839842
if (this.options.enforceStep) {
840843
this.lowValue = this.roundStep(this.lowValue);
@@ -1411,10 +1414,19 @@
14111414
* @returns {number}
14121415
*/
14131416
valueToOffset: function(val) {
1414-
if (this.options.rightToLeft) {
1415-
return (this.maxValue - this.sanitizeValue(val)) * this.maxPos / this.valueRange || 0;
1417+
var sanitizedValue = this.sanitizeValue(val);
1418+
if (!this.options.logScale) {
1419+
if (this.options.rightToLeft) {
1420+
return (this.maxValue - sanitizedValue) * this.maxPos / this.valueRange || 0;
1421+
}
1422+
return (sanitizedValue - this.minValue) * this.maxPos / this.valueRange || 0;
1423+
}
1424+
else {
1425+
var minLog = Math.log(this.minValue),
1426+
maxLog = Math.log(this.maxValue),
1427+
scale = (maxLog - minLog) / (this.maxPos);
1428+
return (Math.log(sanitizedValue) - minLog) / scale || 0;
14161429
}
1417-
return (this.sanitizeValue(val) - this.minValue) * this.maxPos / this.valueRange || 0;
14181430
},
14191431

14201432
/**
@@ -1434,10 +1446,18 @@
14341446
* @returns {number}
14351447
*/
14361448
offsetToValue: function(offset) {
1437-
if (this.options.rightToLeft) {
1438-
return (1 - (offset / this.maxPos)) * this.valueRange + this.minValue;
1449+
if (!this.options.logScale) {
1450+
if (this.options.rightToLeft) {
1451+
return (1 - (offset / this.maxPos)) * this.valueRange + this.minValue;
1452+
}
1453+
return (offset / this.maxPos) * this.valueRange + this.minValue;
1454+
}
1455+
else {
1456+
var minLog = Math.log(this.minValue),
1457+
maxLog = Math.log(this.maxValue),
1458+
scale = (maxLog - minLog) / (this.maxPos);
1459+
return Math.exp(minLog + scale * offset);
14391460
}
1440-
return (offset / this.maxPos) * this.valueRange + this.minValue;
14411461
},
14421462

14431463
// Events

0 commit comments

Comments
 (0)