function plotresults = bfitcheckplotresults(checkon,datahandle,fit)
% BFITCHECKPLOTRESULTS Plot the evaluated results for a given data and fit.

%   Copyright 1984-2000 The MathWorks, Inc.
%   $Revision: 1.18 $  $Date: 2000/09/15 14:15:54 $

format = '%0.0f';
% plot the fit 
axesh = get(datahandle,'parent');
figh = get(axesh, 'parent');
% save hold state and units and set it
fignextplot = get(figh,'nextplot');
axesnextplot = get(axesh,'nextplot');
axesunits = get(axesh,'units');
set(figh,'nextplot','add');
set(figh,'nextplot','add');
set(axesh,'units','normalized');

bfitlistenoff(figh)

plotresults = getappdata(datahandle,'_Basic_Fit_EvalResults');    
if ~isempty(plotresults.handle)
    figure(figh)
    delete(plotresults.handle);
end

if checkon
    axesH  = get(datahandle, 'parent');
    color_order = get(axesH,'colororder');
    colorindex = mod(fit,size(color_order,1)) + 1;

    if isequal('diamond',get(datahandle,'marker'))
        marker = 'square';
    else
        marker = 'diamond';
    end
    if ~isempty(plotresults.x) & ~isempty(plotresults.y)
        figure(figh);
        plotresults.handle = line(plotresults.x,plotresults.y,'parent', axesH, ...
            'tag', createname(fit,datahandle),'color',color_order(colorindex,:),...
            'marker',marker,'linestyle','none','LineWidth',2);
        fitappdata.type = 'evalresults';
        fitappdata.index = fit + 1;
        setappdata(plotresults.handle,'_bfit',fitappdata);    
    else
        % No data to plot, so we just assume the user wants to
        % "preset" that checkbox for later.
        % We could put out a status message, but for now we don't.
        plotresults.handle = [];
    end
else
    plotresults.handle = [];
end
setappdata(datahandle, '_Basic_Fit_EvalResults',plotresults);
guistate = getappdata(datahandle,'_Basic_Fit_Gui_State');
guistate.plotresults = checkon;
setappdata(datahandle,'_Basic_Fit_Gui_State', guistate);

% update legend
legendH = bfitcreatelegend(axesh);

% reset plot: hold and units
set(figh,'nextplot',fignextplot);
set(axesh,'nextplot',axesnextplot);
set(axesh,'units',axesunits);

bfitlistenon(figh)

%----------------------------------
function name = createname(fit,datahandle)
% CREATENAME  Create tag name for evalresults line.

switch fit
case 0
    name = sprintf('evaluated: spline');
case 1
    name = sprintf('evaluated: hermite');
case 2
    name = sprintf('evaluated: linear'); 
case 3
    name = sprintf('evaluated: quadratic'); 
case 4
    name = sprintf('evaluated: cubic');
otherwise
    name = sprintf('evaluated: %sth degree',num2str(fit-1));
end