Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new function to move sliders from the outside #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions d3.slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ d3.slider = function module() {
axis = false,
margin = 50,
value,
div,
scale;

// Private variables
Expand All @@ -38,7 +39,7 @@ d3.slider = function module() {
value = value || scale.domain()[0];

// DIV container
var div = d3.select(this).classed("d3-slider d3-slider-" + orientation, true);
div = d3.select(this).classed("d3-slider d3-slider-" + orientation, true);

var drag = d3.behavior.drag();

Expand Down Expand Up @@ -135,9 +136,13 @@ d3.slider = function module() {


// Move slider handle on click/drag
function moveHandle(pos) {

var newValue = stepValue(scale.invert(pos / sliderLength));
function moveHandle(pos, val) {
var newValue;

if(typeof val != "undefined")
newValue = val;
else
newValue = stepValue(scale.invert(pos / sliderLength));

if (value !== newValue) {
var oldPos = formatPercent(scale(stepValue(value))),
Expand Down Expand Up @@ -173,12 +178,12 @@ d3.slider = function module() {
}


function onClickHorizontal() {
moveHandle(d3.event.offsetX || d3.event.layerX);
function onClickHorizontal(val) {
moveHandle(d3.event.offsetX || d3.event.layerX, val);
}

function onClickVertical() {
moveHandle(sliderLength - d3.event.offsetY || d3.event.layerY);
function onClickVertical(val) {
moveHandle(sliderLength - d3.event.offsetY || d3.event.layerY, val);
}

function onDragHorizontal() {
Expand Down Expand Up @@ -250,7 +255,15 @@ d3.slider = function module() {
if (!arguments.length) return scale;
scale = _;
return slider;
}
}

slider.slide_to = function(newValue) {
if(newValue > max)
newValue = max;
else if(newValue < min)
newValue = min;
div.on("click")(newValue);
}

d3.rebind(slider, dispatch, "on");

Expand Down
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ <h1>D3.js Slider Examples</h1>
<h2>Default slider</h2>
<code>d3.slider()</code>
<div id="slider1"></div>

<h2>Slider controlled from an external <span id="slider-button"><input type='button' value='button'/></span></h2></h2>
<code>var slider = d3.slider().axis(true);<br>
var pos = 0;<br>
d3.select('#slider-button').on('click', function() { slider.slide_to(++pos); });
</code>
<div id="slider_b"></div>

<h2>Slider with start value</h2>
<code>d3.slider().value(25)</code>
Expand Down Expand Up @@ -76,6 +83,11 @@ <h2>Vertical slider</h2>
<script>

d3.select('#slider1').call(d3.slider());

var slider = d3.slider().axis(true);
var pos = 0;
d3.select('#slider-button').on('click', function() { slider.slide_to(++pos); });
d3.select('#slider_b').call(slider);

d3.select('#slider2').call(d3.slider().value(25));

Expand Down