function draw(this, Data, NormalRefresh)
%DRAW  Draws histogram curves.
%
%  DRAW(VIEW,DATA) maps the response data in DATA to the curves in VIEW.

%  Author(s):  
%  Copyright 1986-2002 The MathWorks, Inc.
%  $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:38:50 $

AxGrid = this.AxesGrid;

%% Input and output sizes
[Ny, Nu] = size(this.Curves);

%% Spec curves
for ct = 1:Ny*Nu
   [MSG,X,Y,XX,YY] = makebars(Data.XData,Data.YData(:,ct));
   % REVISIT: remove conversion to double (UDD bug where XOR mode ignored)
   set(double(this.Curves(ct)),'XData',XX, ...
       'YData', YY);
end

%% Get the parent axes (note all curves must lie in the same axes)
ax = get(this.Curves(1),'Parent');

%% Trim selecyed intervals
this.SelectedInterval = localTrimIntervals(this.SelectedInterval,Data.XData);
%% Draw selection rectangles
%% Draw time selection rectangles. Each SelectionPatch need only be
%% drawn once in each axes since they all occur at the same times. Need to
%% detect this and avoid drawing overlapping rectangles. Also avoids xor
%% mode toggling the visibility of overlapping patches
ax = cell2mat(get(this.SelectionPatch,{'Parent'}));
[junk,I] = unique(ax);
for k=1:length(I)
    if length(this.SelectedInterval)>0
        ylim = get(get(this.SelectionPatch(I(k)),'Parent'),'yLim');
        xdata = [this.SelectedInterval this.SelectedInterval(:,end:-1:1)]';
        ydata = [ylim(1);ylim(1);ylim(2);ylim(2)]*ones(1,length(this.SelectedInterval));
        zdata = [-10;-10;-10;-10]*ones(1,length(this.SelectedInterval));
        set(this.SelectionPatch(I(k)),...
          'Zdata',zdata,...
          'Xdata',xdata,...
          'Ydata',ydata,...
          'LineStyle','none',...
          'HandleVisibility','off','HitTest','on');
    else
         set(this.SelectionPatch(I(k)),...
          'Zdata',[NaN;NaN;NaN;NaN],...
          'Xdata',[NaN;NaN;NaN;NaN],...
          'Ydata',[NaN;NaN;NaN;NaN],...
          'LineStyle','none',...
          'HandleVisibility','off','HitTest','on');
    end
end
nullind = setdiff(1:length(ax),I);
for k=1:length(nullind)
          set(this.SelectionPatch(nullind(k)),...
          'Zdata',[NaN;NaN;NaN;NaN],...
          'Xdata',[NaN;NaN;NaN;NaN],...
          'Ydata',[NaN;NaN;NaN;NaN],...
          'LineStyle','none',...
          'HandleVisibility','on','HitTest','on');
end


function xdata = localTrimIntervals(SelectedInterval,bins)

%% Local function to map the selected intervals to contain a whole number
%% of histogram bars


xdata = [];
for j=1:size(SelectedInterval,1)
    
   L = 1;
   R = 2;
   if SelectedInterval(j,2)<SelectedInterval(j,1)
       L = 2;
       R = 1;
   end
   
   bin_edges = [bins(1)-0.5*(bins(2)-bins(1)); 0.5*(bins(1:end-1)+bins(2:end)); ...
       bins(end)+0.5*(bins(end)-bins(end-1))];
   [junk,I] = min(abs(SelectedInterval(j,L)-bin_edges));
   xdata(j,L) = bin_edges(I(1));
   [junk,I] = min(abs(SelectedInterval(j,R)-bin_edges));
   xdata(j,R) = bin_edges(I(1));    
end
       
