MasterMaps / d3-slider

D3.js slider
http://mastermaps.com
BSD 3-Clause "New" or "Revised" License
248 stars 117 forks source link

Is it possible to move the slider programmatically? #29

Open gupta-ankit opened 9 years ago

gupta-ankit commented 9 years ago

Currently I can create a slider like this

var slider = d3.slider().min(0).max(10);
d3.select("#slider").call(slider)

This creates a slider. Is there a way to update the position of the slider via javascript. For example, executing slider.value(5) should update the position of the slider.

wahlatlas commented 9 years ago

Yes, it is (basically your syntax, maybe naming the variable "slider" is an issue?). This works for me:

var mySlider = d3.slider()
.min(0)
.max(10)
.value(7)
.on("slide", function(evt, value) {
    doSomething(value)
}); 

d3.select("#div-where-the-slider-will-be-attached-to")
.call(mySlider);

mySlider.value(newValue);

Do you work with the latest version of the slider? This functionality became available on 29 Sep 2014 through https://github.com/turban/d3.slider/commit/bb33d878b627d4c20c13212b75fd50d1ca1abcad

VD17593 commented 9 years ago

I experienced also a problem with the getter/setter function for the value. When the value is not changed at creation (as in the example above), or when the value is set to 0, the setter doesn't work. The problem (I think) in the code below is that the condition if (value) { will return false if the value is zero.

slider.value = function(_) {
  if (!arguments.length) return value;
  if (value) {
    moveHandle(stepValue(_));
  };
  value = _;
  return slider;
};

Therefore I would suggest to use another condition like:

if (typeof value !== 'undefined') {

or

if (value>=0) {

Regards. Nice plugin by the way.

citrusvanilla commented 6 years ago

man thank the lord i found this @VD17593 thanks for the spot.. confirmed same behavior for me trying to set a value to something other than zero... new to JS so a rude way to find out that 0 == false