www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/mv_zoom.m

    function varargout = mv_zoom(ax, mode)
%MV_ZOOM  Mouse-based zooming for axes
%
%  MV_ZOOM(MODE) initiates zooming by draggin the mouse.  MODE is either
%  one of the strings 'xlim' or 'ylim, or a cell array containing both:
%  {'xlim', 'ylim'}.  This mode setting controls whether zooming is
%  horizontal-only, vertical-only or in both directions.

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.


if nargin==1
    if ischar(ax) || iscellstr(ax)
        mode = ax;
        ax = gca;
    else
        mode = {'xlim' 'ylim'};
    end
elseif nargin == 0
    mode = {'xlim' 'ylim'};
    ax = gca;
end

if ischar(mode) 
    switch mode
        case 'reset'
            % Command option that resets the zoom state to as though mv_zoom had
            % never been called
            
            % Clear the restore option data.
            zlab = get(ax,'ZLabel');
            set(zlab, 'UserData', []);
        case 'on'
            varargout{1} = ~isempty(get(ax,'ZLabel'));
    end
    
else
    seltype = get(ancestor(ax, 'figure'), 'SelectionType');
    switch seltype
        case 'open'
            i_restore(ax);
        case 'extend'
            i_zoom(ax, mode);
    end
end

%----------------------------------------------------------
function i_zoom(hAx, mode)

hFig = ancestor(hAx, 'figure');

% First check to see if there are any text objects...
tH=findobj(hAx,'Type','text','Clipping','off');

% and clip all of them
set(tH,'Clipping','on');

% get the axes position and
apos=getpixelposition(hAx, true);
xlim=get(hAx,'XLim');
xlen=xlim(2)-xlim(1);
ylim=get(hAx,'YLim');
ylen=ylim(2)-ylim(1);

% get ratios to convert pt to pixel units.
xratio=xlen/apos(3);
yratio=ylen/apos(4);

% check for a shift in x or y
xshift=(0-xlim(1));
yshift=(0-ylim(1));

% get pt in pixels from bottom left of figure.
pt = get(hFig, 'CurrentPoint');

% draw the rubber band box
frect=rbbox([pt(1) pt(2) 0 0 ]);
frect(1:2) = mbcgui.util.convertLocation(frect,hAx);

newxlim=sort(([frect(1) frect(1)+frect(3)]).*xratio)-xshift;
newylim=sort(([frect(2) frect(2)+frect(4)]).*yratio)-yshift;

if diff(newxlim)>0 && diff(newylim)>0
    % update the axes limits
    zlab=get(hAx,'ZLabel');
    ud=get(zlab,'UserData');
    if isempty(ud)
        ud.XLimMode= get(hAx,'XLimMode');
        ud.YLimMode= get(hAx,'YLimMode');
        ud.xlim=xlim;
        ud.ylim=ylim;
    end
    if any(strcmpi('xlim', mode))
        set(hAx,'XLim',newxlim);
    end
    if any(strcmpi('ylim', mode))
        set(hAx,'YLim',newylim);
    end
    % store the original values
    ud.Text=tH;
    set(zlab,'UserData',ud);
end


%----------------------------------------------------------
function i_restore(hAx)
ud=get(get(hAx,'ZLabel'),'UserData');
if isempty(ud)
    return
end
set(hAx,'XLim',sort(ud.xlim),...
    'YLim',sort(ud.ylim),'XLimMode',ud.XLimMode,'YLimMode',ud.YLimMode);
set(get(hAx,'ZLabel'),'UserData',[]);

% Check to see if there are any text objects...
tH = ud.Text;
set(tH(isgraphics(tH)),'Clipping','off');