Skip to content

Commit b885901

Browse files
committed
Better explain the calculations for the auto tick location.
1 parent 4ecf6d9 commit b885901

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/components/slider/slider.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,31 @@ export class MdSlider implements AfterContentInit {
244244
* separation constant.
245245
*/
246246
private _updateAutoTickSeparation() {
247-
let width = this._sliderDimensions.width;
247+
// We're looking for the multiple of step for which the separation between is greater than the
248+
// minimum tick separation.
249+
let sliderWidth = this._sliderDimensions.width;
248250

249-
// This calculates which step is far enough away from the beginning of the slider to draw a tick
250-
// at. This value will then be used to draw ticks at every tickStep steps.
251-
let tickStep =
252-
Math.ceil(MIN_AUTO_TICK_SEPARATION * (this.max - this.min) / (width * this.step));
251+
// This is the total "width" of the slider in terms of values.
252+
let valueWidth = this.max - this.min;
253253

254-
// The percentage of the slider where the first tick should go.
255-
let tickPercentage = this.calculatePercentage((this.step * tickStep) + this.min);
254+
// Calculate how many values exist within 1px on the slider.
255+
let valuePerPixel = valueWidth / sliderWidth;
256+
257+
// Calculate how many values exist in the minimum tick separation (px).
258+
let valuePerSeparation = valuePerPixel * MIN_AUTO_TICK_SEPARATION;
259+
260+
// Calculate how many steps exist in this separation. This will be the lowest value you can
261+
// multiply step by to get a separation that is greater than or equal to the minimum tick
262+
// separation.
263+
let stepsPerSeparation = Math.ceil(valuePerSeparation / this.step);
264+
265+
// Get the percentage of the slider for which this tick would be located so we can then draw
266+
// it on the slider.
267+
let tickPercentage = this.calculatePercentage((this.step * stepsPerSeparation) + this.min);
256268

257269
// The pixel value of the tick is the percentage * the width of the slider. Use this to draw
258270
// the ticks on the slider.
259-
this._renderer.drawTicks(width * tickPercentage);
271+
this._renderer.drawTicks(sliderWidth * tickPercentage);
260272
}
261273

262274
/**

0 commit comments

Comments
 (0)