1

I am trying to switch the zoom mode between "x" and "y" based what the selection in a radio button group is. Selecting the "y" mode is not changing the direction of zooming.

Can anyone find what is wrong in the fiddle below? If I were to manually change mode to "y", xaxis.zoomRange to false, and yaxis.zoomRange to null, I can zoom along only the y-axis, but repeating the same steps programmatically does not have the same effect.

http://jsfiddle.net/apandit/que5w852/

HTML:

<div id='plot' style='width:1000px;height:375px;'></div>
<input type='radio' name='zoomDir' value='x' onclick='setZoomDirection(this)' checked>Zoom in X Direction
<input type='radio' name='zoomDir' value='y' onclick='setZoomDirection(this)'>Zoom in Y Direction

JS:

var datasets = [[
                    [0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]
                ],
                [
                    [0,0],[-1,-1],[-2,-2],[-3,-3],[-4,-4],[-5,-5],[-6,-6],[-7,-7],[-8,-8],[-9,-9]
                ]];

var plot = $.plot("#plot",datasets,{
        pan: {
          interactive: true
        },
        zoom: {
          interactive: true,
          mode: "x"
        },
        xaxis: {
            zoomRange: null
        },
        yaxis: {
            zoomRange: false
        }
    });

function setZoomDirection(radioBtn) {
    var options = plot.getOptions();
    var data = plot.getData();

    options.zoom.mode = radioBtn.value;

    if(radioBtn.value == 'y') {
        options.xaxis.zoomRange = false;
        options.yaxis.zoomRange = null;
    }
    else {
        options.yaxis.zoomRange = false;
        options.xaxis.zoomRange = null;
    }

    plot = $.plot("#plot",data,options);
};
apandit
  • 808
  • 1
  • 7
  • 16

1 Answers1

1

1) Your setZoomDirection function was not called because it was in another scope then your inline onclick handlers in the html. I changed it to a jQuery event handler.

2) Internally flot uses options.xaxes[0] instead of options.xaxis. Just replace these lines and you get the experted result:

if (this.value == 'y') {
    options.xaxes[0].zoomRange = false;
    options.yaxes[0].zoomRange = null;
}
else {
    options.yaxes[0].zoomRange = false;
    options.xaxes[0].zoomRange = null;
}

See the updated fiddle for the full example.

Raidri
  • 17,258
  • 9
  • 62
  • 65
  • Thanks Raidri! The second issue was the problem in my code, but both issues are present in my original version of the jsfiddle. I had setZoomDirection defined as "window.setZoomDirection = function(radioBtn) {...}" in my code...don't know why I didn't transfer that over when creating the fiddle. – apandit Apr 07 '15 at 12:09
  • Hadn't thought of debugging in jsfiddle either. For those reading this that don't know how to debug a jsfiddle, check out http://stackoverflow.com/questions/19460046/how-to-debug-the-js-in-jsfiddle/29491545#29491545 – apandit Apr 07 '15 at 12:35