Skip to content

Commit f949181

Browse files
author
Valentin Hervieu
committed
feat(logScale): Implement a logScale option to display sliders with logarithmic scale
1 parent a495e53 commit f949181

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

demo/demo.js

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

115+
//Slider config with logarithmic scale
116+
$scope.slider_log = {
117+
value: 1,
118+
options: {
119+
floor: 1,
120+
ceil: 100,
121+
logScale: true
122+
}
123+
};
124+
115125
//Slider config with callbacks
116126
$scope.slider_callbacks = {
117127
value: 100,

demo/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ <h2>Slider with custom floor/ceil/step</h2>
102102
></rzslider>
103103
</article>
104104

105+
<article>
106+
<h2>Slider with logarithmic scale</h2>
107+
<rzslider
108+
rz-slider-model="slider_log.value"
109+
rz-slider-options="slider_log.options"
110+
></rzslider>
111+
</article>
112+
105113
<article>
106114
<h2>Slider with callbacks on start, change and end</h2>
107115
<p>Value linked on start: {{ otherData.start }}</p>

dist/rzslider.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v2.9.0 -
22
(c) Rafal Zajac <rzajac@gmail.com>, Valentin Hervieu <valentin@hervieu.me>, Jussi Saarivirta <jusasi@gmail.com>, Angelin Sirbu <angelin.sirbu@gmail.com> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-02-24 */
4+
2016-02-25 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -52,6 +52,7 @@
5252
getSelectionBarColor: null,
5353
getPointerColor: null,
5454
keyboardSupport: true,
55+
logScale: false,
5556
scale: 1,
5657
enforceStep: true,
5758
enforceRange: false,
@@ -643,6 +644,8 @@
643644
this.precision = +this.options.precision;
644645

645646
this.minValue = this.options.floor;
647+
if (this.options.logScale && this.minValue === 0)
648+
throw new Error("Can't use floor=0 with logarithmic scale");
646649

647650
if (this.options.enforceStep) {
648651
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
@@ -1027,7 +1030,7 @@
10271030
* correct parameters
10281031
*/
10291032
getPointerColor: function(pointerType) {
1030-
if ( pointerType === 'max' ) {
1033+
if (pointerType === 'max') {
10311034
return this.options.getPointerColor(this.scope.rzSliderHigh, pointerType);
10321035
}
10331036
return this.options.getPointerColor(this.scope.rzSliderModel, pointerType);
@@ -1166,7 +1169,15 @@
11661169
* @returns {number}
11671170
*/
11681171
valueToOffset: function(val) {
1169-
return (this.sanitizeValue(val) - this.minValue) * this.maxPos / this.valueRange || 0;
1172+
var sanitizedValue = this.sanitizeValue(val);
1173+
if (!this.options.logScale)
1174+
return (sanitizedValue - this.minValue) * this.maxPos / this.valueRange || 0;
1175+
else {
1176+
var minLog = Math.log(this.minValue),
1177+
maxLog = Math.log(this.maxValue),
1178+
scale = (maxLog - minLog) / (this.maxPos);
1179+
return (Math.log(sanitizedValue) - minLog) / scale || 0;
1180+
}
11701181
},
11711182

11721183
/**
@@ -1186,7 +1197,14 @@
11861197
* @returns {number}
11871198
*/
11881199
offsetToValue: function(offset) {
1189-
return (offset / this.maxPos) * this.valueRange + this.minValue;
1200+
if (!this.options.logScale)
1201+
return (offset / this.maxPos) * this.valueRange + this.minValue;
1202+
else {
1203+
var minLog = Math.log(this.minValue),
1204+
maxLog = Math.log(this.maxValue),
1205+
scale = (maxLog - minLog) / (this.maxPos);
1206+
return Math.exp(minLog + scale * offset);
1207+
}
11901208
},
11911209

11921210
// Events

dist/rzslider.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rzslider.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
getSelectionBarColor: null,
5757
getPointerColor: null,
5858
keyboardSupport: true,
59+
logScale: false,
5960
scale: 1,
6061
enforceStep: true,
6162
enforceRange: false,
@@ -647,6 +648,8 @@
647648
this.precision = +this.options.precision;
648649

649650
this.minValue = this.options.floor;
651+
if (this.options.logScale && this.minValue === 0)
652+
throw new Error("Can't use floor=0 with logarithmic scale");
650653

651654
if (this.options.enforceStep) {
652655
this.scope.rzSliderModel = this.roundStep(this.scope.rzSliderModel);
@@ -1031,7 +1034,7 @@
10311034
* correct parameters
10321035
*/
10331036
getPointerColor: function(pointerType) {
1034-
if ( pointerType === 'max' ) {
1037+
if (pointerType === 'max') {
10351038
return this.options.getPointerColor(this.scope.rzSliderHigh, pointerType);
10361039
}
10371040
return this.options.getPointerColor(this.scope.rzSliderModel, pointerType);
@@ -1170,7 +1173,15 @@
11701173
* @returns {number}
11711174
*/
11721175
valueToOffset: function(val) {
1173-
return (this.sanitizeValue(val) - this.minValue) * this.maxPos / this.valueRange || 0;
1176+
var sanitizedValue = this.sanitizeValue(val);
1177+
if (!this.options.logScale)
1178+
return (sanitizedValue - this.minValue) * this.maxPos / this.valueRange || 0;
1179+
else {
1180+
var minLog = Math.log(this.minValue),
1181+
maxLog = Math.log(this.maxValue),
1182+
scale = (maxLog - minLog) / (this.maxPos);
1183+
return (Math.log(sanitizedValue) - minLog) / scale || 0;
1184+
}
11741185
},
11751186

11761187
/**
@@ -1190,7 +1201,14 @@
11901201
* @returns {number}
11911202
*/
11921203
offsetToValue: function(offset) {
1193-
return (offset / this.maxPos) * this.valueRange + this.minValue;
1204+
if (!this.options.logScale)
1205+
return (offset / this.maxPos) * this.valueRange + this.minValue;
1206+
else {
1207+
var minLog = Math.log(this.minValue),
1208+
maxLog = Math.log(this.maxValue),
1209+
scale = (maxLog - minLog) / (this.maxPos);
1210+
return Math.exp(minLog + scale * offset);
1211+
}
11941212
},
11951213

11961214
// Events

0 commit comments

Comments
 (0)