Skip to content

Commit 22a4ca5

Browse files
committed
feat(ticks): Add legend support.
This feature enables to display legend under each slider tick.
1 parent 2ffe193 commit 22a4ca5

13 files changed

+287
-49
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ The default options are:
187187
minRange: 0,
188188
id: null,
189189
translate: null,
190+
getLegend: null,
190191
stepsArray: null,
191192
draggableRange: false,
192193
draggableRangeOnly: false,
@@ -257,9 +258,22 @@ $scope.slider = {
257258
};
258259
```
259260

261+
**getLegend** - _Function(value, sliderId)_: Use to display legend under ticks. The function will be called with each tick value and returned content will be displayed under the tick as a legend. If the returned value is null, then no legend is displayed under the corresponding tick.
262+
> In order to get enough space to display legends under the slider, you need to add the `with-legend` class to the slider component. The default margin-bottom is then 40px which is enough for legends that are displayed on 2 lines. If you need more, simply override the style for the class.
263+
260264
**id** - _Any (defaults to null)_: If you want to use the same `translate` function for several sliders, just set the `id` to anything you want, and it will be passed to the `translate(value, sliderId)` function as a second argument.
261265

262-
**stepsArray** - _Array_: If you want to display a slider with non linear/number steps. Just pass an array with each slider value and that's it; the floor, ceil and step settings of the slider will be computed automatically. The `rz-slider-model` value will be the index of the selected item in the stepsArray.
266+
**stepsArray** - _Array_: If you want to display a slider with non linear/number steps.
267+
Just pass an array with each slider value and that's it; the floor, ceil and step settings of the slider will be computed automatically. The `rz-slider-model` value will be the index of the selected item in the stepsArray.
268+
269+
`stepsArray` can also be an array of objects like:
270+
271+
```js
272+
[
273+
{value: 'A'}, // the display value will be *A*
274+
{value: 10, legend: 'Legend for 10'} // the display value will be 10 and a legend will be displayed under the corresponding tick.
275+
]
276+
```
263277

264278
**draggableRange** - _Boolean (defaults to false)_: When set to true and using a range slider, the range can be dragged by the selection bar. *Applies to range slider only.*
265279

demo/demo.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
116116
$scope.slider_floor_ceil_rtl = {
117117
value: 12,
118118
options: {
119-
floor: 10,
120-
ceil: 100,
121-
step: 5,
122-
rightToLeft: true
119+
floor: 10,
120+
ceil: 100,
121+
step: 5,
122+
rightToLeft: true
123123
}
124124
}
125125

@@ -257,6 +257,27 @@ app.controller('MainCtrl', function($scope, $rootScope, $timeout, $modal) {
257257
}
258258
};
259259

260+
//Slider with ticks values and legend
261+
$scope.slider_ticks_legend = {
262+
value: 5,
263+
options: {
264+
ceil: 10,
265+
floor: 0,
266+
showTicksValues: true,
267+
stepsArray: [
268+
{value: 1, legend: 'Very poor'},
269+
{value: 2},
270+
{value: 3, legend: 'Fair'},
271+
{value: 4},
272+
{value: 5, legend: 'Average'},
273+
{value: 6},
274+
{value: 7, legend: 'Good'},
275+
{value: 8},
276+
{value: 9, legend: 'Excellent'}
277+
]
278+
}
279+
};
280+
260281
//Slider with draggable range
261282
$scope.slider_draggable_range = {
262283
minValue: 1,

demo/index.html

+9
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ <h2>Slider with ticks and values at intermediate positions</h2>
195195
rz-slider-model="slider_ticks_values_at.value"
196196
rz-slider-options="slider_ticks_values_at.options"
197197
></rzslider>
198+
<br><br>
199+
</article>
200+
201+
<article>
202+
<h2>Slider with ticks values and legend</h2>
203+
<rzslider class="with-legend"
204+
rz-slider-model="slider_ticks_legend.value"
205+
rz-slider-options="slider_ticks_legend.options"
206+
></rzslider>
198207
</article>
199208

200209
<article>

dist/rzslider.css

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v2.12.0 -
22
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-04-22 */
4+
2016-04-24 */
55
rzslider {
66
position: relative;
77
display: inline-block;
@@ -15,6 +15,10 @@ rzslider {
1515
user-select: none;
1616
}
1717

18+
rzslider.with-legend {
19+
margin-bottom: 40px;
20+
}
21+
1822
rzslider[disabled] {
1923
cursor: not-allowed;
2024
}
@@ -159,6 +163,14 @@ rzslider .rz-ticks .rz-tick .rz-tick-value {
159163
transform: translate(-50%, 0);
160164
}
161165

166+
rzslider .rz-ticks .rz-tick .rz-tick-legend {
167+
position: absolute;
168+
top: 24px;
169+
max-width: 50px;
170+
white-space: normal;
171+
transform: translate(-50%, 0);
172+
}
173+
162174
rzslider .rz-ticks.rz-ticks-values-under .rz-tick-value {
163175
top: initial;
164176
bottom: -40px;
@@ -234,6 +246,14 @@ rzslider.rz-vertical .rz-ticks .rz-tick .rz-tick-value {
234246
transform: translate(0, -28%);
235247
}
236248

249+
rzslider.rz-vertical .rz-ticks .rz-tick .rz-tick-legend {
250+
top: initial;
251+
right: 24px;
252+
max-width: none;
253+
white-space: nowrap;
254+
transform: translate(0, -28%);
255+
}
256+
237257
rzslider.rz-vertical .rz-ticks.rz-ticks-values-under .rz-tick-value {
238258
right: 12px;
239259
bottom: initial;

dist/rzslider.js

+46-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*! angularjs-slider - v2.12.0 -
22
(c) Rafal Zajac <[email protected]>, Valentin Hervieu <[email protected]>, Jussi Saarivirta <[email protected]>, Angelin Sirbu <[email protected]> -
33
https://github.com/angular-slider/angularjs-slider -
4-
2016-04-22 */
4+
2016-04-24 */
55
/*jslint unparam: true */
66
/*global angular: false, console: false, define, module */
77
(function(root, factory) {
@@ -34,6 +34,7 @@
3434
minRange: 0,
3535
id: null,
3636
translate: null,
37+
getLegend: null,
3738
stepsArray: null,
3839
draggableRange: false,
3940
draggableRangeOnly: false,
@@ -424,37 +425,58 @@
424425

425426
this.options.showTicks = this.options.showTicks || this.options.showTicksValues;
426427
this.scope.showTicks = this.options.showTicks; //scope is used in the template
427-
if(angular.isNumber(this.options.showTicks))
428+
if (angular.isNumber(this.options.showTicks))
428429
this.intermediateTicks = true;
429430

430431
this.options.showSelectionBar = this.options.showSelectionBar || this.options.showSelectionBarEnd
431432
|| this.options.showSelectionBarFromValue !== null;
432433

433434
if (this.options.stepsArray) {
434-
this.options.floor = 0;
435-
this.options.ceil = this.options.stepsArray.length - 1;
436-
this.options.step = 1;
437-
if (this.options.translate) {
435+
this.parseStepsArray();
436+
} else {
437+
if (this.options.translate)
438438
this.customTrFn = this.options.translate;
439-
}
440-
else {
439+
else
441440
this.customTrFn = function(value) {
442-
return this.options.stepsArray[value];
441+
return String(value);
443442
};
443+
444+
if (this.options.getLegend) {
445+
this.getLegend = this.options.getLegend;
444446
}
445-
} else if (this.options.translate)
446-
this.customTrFn = this.options.translate;
447-
else
448-
this.customTrFn = function(value) {
449-
return String(value);
450-
};
447+
}
451448

452449
if (this.options.vertical) {
453450
this.positionProperty = 'bottom';
454451
this.dimensionProperty = 'height';
455452
}
456453
},
457454

455+
parseStepsArray: function() {
456+
this.options.floor = 0;
457+
this.options.ceil = this.options.stepsArray.length - 1;
458+
this.options.step = 1;
459+
460+
if (this.options.translate) {
461+
this.customTrFn = this.options.translate;
462+
}
463+
else {
464+
this.customTrFn = function(index) {
465+
var step = this.options.stepsArray[index];
466+
if (angular.isObject(step))
467+
return step.value;
468+
return step;
469+
};
470+
}
471+
472+
this.getLegend = function(index) {
473+
var step = this.options.stepsArray[index];
474+
if (angular.isObject(step))
475+
return step.legend;
476+
return null;
477+
};
478+
},
479+
458480
/**
459481
* Resets slider
460482
*
@@ -558,7 +580,7 @@
558580
else
559581
this.selBar.removeClass('rz-draggable');
560582

561-
if(this.intermediateTicks && this.options.showTicksValues)
583+
if (this.intermediateTicks && this.options.showTicksValues)
562584
this.ticks.addClass('rz-ticks-values-under');
563585
},
564586

@@ -768,7 +790,7 @@
768790
updateTicksScale: function() {
769791
if (!this.options.showTicks) return;
770792
var step = this.step;
771-
if(this.intermediateTicks)
793+
if (this.intermediateTicks)
772794
step = this.options.showTicks;
773795
var ticksCount = Math.round((this.maxValue - this.minValue) / step) + 1;
774796
this.scope.ticks = [];
@@ -793,6 +815,11 @@
793815
tick.valueTooltipPlacement = this.options.vertical ? 'right' : 'top';
794816
}
795817
}
818+
if (this.getLegend) {
819+
var legend = this.getLegend(value, this.options.id);
820+
if (legend)
821+
tick.legend = legend;
822+
}
796823
if (!this.options.rightToLeft) {
797824
this.scope.ticks.push(tick);
798825
} else {
@@ -1458,7 +1485,7 @@
14581485
newValue = ceilValue;
14591486
} else {
14601487
newValue = this.offsetToValue(newOffset);
1461-
if(fromTick && angular.isNumber(this.options.showTicks))
1488+
if (fromTick && angular.isNumber(this.options.showTicks))
14621489
newValue = this.roundStep(newValue, this.options.showTicks);
14631490
else
14641491
newValue = this.roundStep(newValue);
@@ -1911,7 +1938,7 @@
19111938
'use strict';
19121939

19131940
$templateCache.put('rzSliderTpl.html',
1914-
"<span class=rz-bar-wrapper><span class=rz-bar></span></span> <span class=rz-bar-wrapper><span class=\"rz-bar rz-selection\" ng-style=barStyle></span></span> <span class=\"rz-pointer rz-pointer-min\" ng-style=minPointerStyle></span> <span class=\"rz-pointer rz-pointer-max\" ng-style=maxPointerStyle></span> <span class=\"rz-bubble rz-limit\"></span> <span class=\"rz-bubble rz-limit\"></span> <span class=rz-bubble></span> <span class=rz-bubble></span> <span class=rz-bubble></span><ul ng-show=showTicks class=rz-ticks><li ng-repeat=\"t in ticks track by $index\" class=rz-tick ng-class=\"{'rz-selected': t.selected}\" ng-style=t.style ng-attr-uib-tooltip=\"{{ t.tooltip }}\" ng-attr-tooltip-placement={{t.tooltipPlacement}} ng-attr-tooltip-append-to-body=\"{{ t.tooltip ? true : undefined}}\"><span ng-if=\"t.value != null\" class=rz-tick-value ng-attr-uib-tooltip=\"{{ t.valueTooltip }}\" ng-attr-tooltip-placement={{t.valueTooltipPlacement}}>{{ t.value }}</span></li></ul>"
1941+
"<span class=rz-bar-wrapper><span class=rz-bar></span></span> <span class=rz-bar-wrapper><span class=\"rz-bar rz-selection\" ng-style=barStyle></span></span> <span class=\"rz-pointer rz-pointer-min\" ng-style=minPointerStyle></span> <span class=\"rz-pointer rz-pointer-max\" ng-style=maxPointerStyle></span> <span class=\"rz-bubble rz-limit\"></span> <span class=\"rz-bubble rz-limit\"></span> <span class=rz-bubble></span> <span class=rz-bubble></span> <span class=rz-bubble></span><ul ng-show=showTicks class=rz-ticks><li ng-repeat=\"t in ticks track by $index\" class=rz-tick ng-class=\"{'rz-selected': t.selected}\" ng-style=t.style ng-attr-uib-tooltip=\"{{ t.tooltip }}\" ng-attr-tooltip-placement={{t.tooltipPlacement}} ng-attr-tooltip-append-to-body=\"{{ t.tooltip ? true : undefined}}\"><span ng-if=\"t.value != null\" class=rz-tick-value ng-attr-uib-tooltip=\"{{ t.valueTooltip }}\" ng-attr-tooltip-placement={{t.valueTooltipPlacement}}>{{ t.value }}</span> <span ng-if=\"t.legend != null\" class=rz-tick-legend>{{ t.legend }}</span></li></ul>"
19151942
);
19161943

19171944
}]);

dist/rzslider.min.css

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rzslider.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rzSliderTpl.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
ng-attr-uib-tooltip="{{ t.tooltip }}" ng-attr-tooltip-placement="{{t.tooltipPlacement}}"
1616
ng-attr-tooltip-append-to-body="{{ t.tooltip ? true : undefined}}">
1717
<span ng-if="t.value != null" class="rz-tick-value"
18-
ng-attr-uib-tooltip="{{ t.valueTooltip }}"
19-
ng-attr-tooltip-placement="{{t.valueTooltipPlacement}}">{{ t.value }}</span>
18+
ng-attr-uib-tooltip="{{ t.valueTooltip }}"
19+
ng-attr-tooltip-placement="{{t.valueTooltipPlacement}}">{{ t.value }}</span>
20+
<span ng-if="t.legend != null" class="rz-tick-legend">{{ t.legend }}</span>
2021
</li>
2122
</ul>

0 commit comments

Comments
 (0)