www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgsurfview/@lineplot/plot2.m
function plot2(obj,data) %LINEPLOT/PLOT2 Plots multiple lines on a single pair of axes. % % obj.plot2(data) % Copyright 2000-2014 The MathWorks, Inc. and Ford Global Technologies, Inc. % set up X-axis x = data.getVariableValues(1); xlim = mbcmakelimits(x); set(get(obj.hAxes,'XLabel'),'String',data.getVariableName(1),'Interpreter','none'); set(obj.hAxes,'XLim',xlim); points=getValues(data,1); % for shorthand % remove points outside boundary if required. if getConstraintOptions(data) % only one option c = getConstraints(data); points(c>=0) = NaN; if ~any(isfinite(points)) set(obj.hAxes,'XLim',[-1 1],'YLim',[-1 1]); obj.hText = text('String','No data to plot',... 'Parent',obj.hAxes,... 'FontSize',18,... 'HorizontalAlignment','center'); return; end end % set y-limits explicity, otherwise they will be changed every time we plot a line, giving % strange results when we try to position the labels. set(obj.hAxes,'YLim',mbcmakelimits(data.getDataRange)); % no label required. the plot title contains the relevant information % get info for second variable. each value of this variable corresponds % to a different line. lines = data.getVariableValues(2); varname = data.getVariableName(2); cmap = hsv(length(lines)); % position information for labels, so that we can avoid collisions % format [ x1 x2 x3;y1 y2 y3] etc. positions=[]; hlines = gobjects(numel(lines),1); for k=1:numel(lines) % plot the lines individually so that we can specify colours hlines(k) = line(x,points(:,k),'Parent',obj.hAxes,'Color',cmap(k,:)); % Create a label for the line str = sprintf('%s=%.6g',varname,lines(k)); % Create a label that is useful for a legend xregGui.setLegendData(hlines(k), sprintf('%s: %s', data.datasetnames{1}, str)); % Set this text as the user data for the line, to be used in cursor mode set(hlines(k),'UserData',str); % label the line. Setting horizontal alignment to 'right' % ensures that it doesn't stick off the edge of the axes. % Setting vertical alignment to 'top' is an attempt to make sure that % the line doesn't pass through the text. th=text(x(end),points(end,k),str,... 'Parent',obj.hAxes,'Color',cmap(k,:),'Interpreter','none', ... 'HorizontalAlignment','right',... 'VerticalAlignment','top'); % TODO % ext=get(th,'Extent'); ext = [0 0 100 80]; % assuming that all other labels are the same size as this one, check for % an overlap index=length(x); if ~isempty(positions) % we don't use the first point, because the text would stick of the % left hand side of the axes. while index>2 xoverlaps=abs( positions(1,:) - ext(1) ) < ext(3); yoverlaps=abs( positions(2,:) - ext(2) ) < ext(4); if ~any( xoverlaps & yoverlaps ) % found a suitable location. break; end % clash. move it to an earlier point in the line. index=index-1; set(th,'Position',[x(index) points(index,k)]); ext=get(th,'Extent'); end % if we have moved to the first point without success % just give up. end positions=[ positions ext([1 2])' ]; % append [ext(1);ext(2)] end obj.hLines = hlines;