// Filename:    PlotProf.ogs
// Creation:    1/15/1997
// Purpose:     To generate plots from Extended 2D Plot toolbar
// Modifications:
//	CPY 3/19/99 v6.0176 t5596 MOVE_COMPLEX_PLOT_NOT_FROM_SCRIPT
////////////////////////////////////////////////////////////////////////////////

[General]
	////////////////////////////////////////////////////////////////////////
	// General Plotting of data for Worksheet or Graph
	// Assumes 3 arguments: 1st=Plot Type, 2nd=Template, 3rd=Restore/Exchange Axes
	////////////////////////////////////////////////////////////////////////
	if (exist(%H)==2)
	{
		worksheet -P %1 %2;
		set %C -l %3;
		Legend;
		return 0;
	}
	if (exist(%H)==3)
	{
		layer -b x 0;           //Restore/Exchange Axes 
		layer -i%1 %C;
		set %C -l %3;
		return 0;
	}	

[SimpleGraph]
	////////////////////////////////////////////////////////////////////////
	// General Plotting of data for Worksheet or Graph
	// Assumes 3 arguments: 1st=Plot Type, 2nd=Template, 3rd=Restore/Exchange Axes
	////////////////////////////////////////////////////////////////////////
	if (exist(%H)==2)
	{
		worksheet -P %1 %2;
		Legend;
		return 0;
	}
	if (exist(%H)==3)
	{
		layer -b x %3;           //Restore/Exchange Axes if 0/1 respectively
		layer -i%1 %C;
		return 0;
	}	

[HorzStep]
	run.section(,General, 200 Line 11);

[VertStep]
	run.section(,General, 200 Line 12);

[IndexSize]
	if (exist(%H)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	run.section(,SimpleGraph, 193 bubble 0);
	return 0;

[IndexColor]
	if (exist(%H)!=2)
	{
		type -b $General.BadGraphType;
		return 1;
	}
	run.section(,SimpleGraph, 247 scatter 0);

	///YuI 2/12/2: in case column to be used as colormap is categorical need to index instead
	get %C -cse nColorOfPlot;				//first get color from the plot. It should be 101 + nColumnToTheRight;
	nLinkedColumn = colnum(%C) + nColorOfPlot - 101;	//now nLinkedColumn is column number of linked column
	get %(%E, nLinkedColumn) -dc nCategorical;		//the column may be already categorical
	if( nCategorical == 0 )
	{
		get %(%E, nLinkedColumn) -dt nTextProbability;		//get the probablity of this column to be categorical
		if( nTextProbability >= @TH )				//probabity is higher than threshold
		{
			set %(%E, nLinkedColumn) - dc 2;	//initialize categorical map to ordinal
			set %c -cset 0;				//set linking type to colr indexing (was color mapping);
		}
	}
	else
	    set %c -cset 0;

	delete -v nColorOfPlot;
	delete -v nLinkedColumn;
	delete -v nCategorical;
	delete -v nTextProbability;
	/// end YuI 2/12/2

	return 0;

[IndexColorSize]
///The user can select 2 or 3 columns.  If 2 columns, then it uses the second column for the 
///indexed size and color.  Otherwise it uses the second column for size and third for color.
	NumCols=%(%H,@Y#,2);
	if (exist(%H)!=2)
	{
		type -b $General.BadGraphType;
		return 1;
	}
	if (%(%H,@Y#,2)< 2 || %(%H,@Y#,2) > 3) 
	{
		type -b $General.Only2or3YColumns;
		return 1;
	} 
	if(NumCols==2)
		run.section(,SimpleGraph, 194 bubble 0); //IDM_PLOT_INDEX_SIZE_COLOR_SAME_COLOUMN
	else
		run.section(,SimpleGraph, 248 bubble 0); //IDM_PLOT_INDEX_SIZE_COLOR
	

	///YuI 2/12/2: in case column to be used as colormap is categorical need to index instead
	get %C -cse nColorOfPlot;				//first get color from the plot. It should be 101 + nColumnToTheRight;
	nLinkedColumn = colnum(%C) + nColorOfPlot - 101;	//now nLinkedColumn is column number of linked column
	get %(%E, nLinkedColumn) -dc nCategorical;		//the column may be already categorical
	if( nCategorical == 0 )
	{
		get %(%E, nLinkedColumn) -dt nTextProbability;		//get the probablity of this column to be categorical
		if( nTextProbability >= @TH )				//probabity is higher than threshold
		{
			set %(%E, nLinkedColumn) - dc 2;	//initialize categorical map to ordinal
			set %c -cset 0;				//set linking type to colr indexing (was color mapping);
		}
	}
	else
	    set %c -cset 0;

	delete -v nColorOfPlot;
	delete -v nLinkedColumn;
	delete -v nCategorical;
	delete -v nTextProbability;
	/// end YuI 2/12/2

	return 0;

[Segment2]
	run.section(,General, 202 LineSymb 2);

[Segment3]
	run.section(,General, 202 LineSymb 3);

[Spline]
	run.section(,General, 202 LineSymb 9);

[Panel2Vert]
	run.section(,Panel, 2, Pan2Vert);

[Panel2Horz]
	run.section(,Panel, 2, Pan2Horz);

[Panel4]
	run.section(,Panel, 4, Pan4);

[Panel9]
	run.section(,Panel, 9, Pan9);

[ExpGrow]
	run.section(fit,ExpGrowth);		//The check for the dataset in here
	layer -a;				//Rescale
	doc -uw;clr;

[ExpDec]
	run.section(fit,ExpDecay1);		//The check for the dataset in here
	layer -a;				//Rescale
	doc -uw;clr;

[VertDropLine]
	if (exist(%H)!=2)
	{
		type -b $General.BadGraphType;
		return 1;
	}
	worksheet -p 201 Dropline;		//Plot into Dropline template
	Legend;
	return 0;

[LinearReg]
		run.section(LR,MenuLR);				

[MultReg]
	if (exist(%H)==2)
	{
		worksheet -p 201 scatter;
		legend;
	};

	doc -e DY {
		if (%[%C,>'_']!="line") 
		{				
			run.section(LR,MenuLR);		
		};  	
	};

[Sigmoidal]
	run.section(Fit,Sigmoidal);
	layer -a;
	doc -uw;clr;

[Panel]
	rone=wks.r1;
	rtwo=wks.r2;

	if (exist(%H)!=2)
	{
		win -t plot %2;
		return 1;
	}

	//reject selection if it does not start with X or Y
	if (wks.col$(wks.c1).type!=1 && wks.col$(wks.c1).type!=4)
	{
		type -b $general.selectionXY;
		return 1;
	}

	if(wks.iscolsel()==0 || %(%H,@Y#,2)==0)
	{
		win -t plot %2;
		return 1;
	}

	if (run.section(PlotProf,CheckNumYCols)!=1)
	{
		jj=0;xx=0;
		count=wks.iscolsel();
		create _tempPos -t 1;
		create _tempLayer -t 1;
		tempPos=1;
		loop(ii,1,count)
		{
			jj=wks.c1+%(%H,@O,ii-1);
			//pass valid column types (X,Y,YERR,XERR,L)
			if(mod(wks.col$(jj).type,2)!=0 && wks.col$(jj).type!=4)
			{
				_tempPos[tempPos]=jj;		//save positions in temp dataset
				if(wks.col$(jj).type==1)
				{
					xx+=1;
					//save layer in temp dataset
					_tempLayer[tempPos]=mod(xx-1,%1)+1;
				}
				else
					_tempLayer[tempPos]=mod(xx-1,%1)+1;//save layer in temp dataset
				tempPos+=1;
			}
		}
		%W=%H;
		worksheet -ps;
		win -t plot %2;				//open template
		jj=0;
		loop(ii,1,tempPos-1)
		{
			layer -o _tempLayer[ii] {		
				layer -wd %W _tempPos[ii] rone _tempPos[ii] rtwo ?;
				layer -a;
			}
		}
	};
	del -v rone;del -v rtwo;del -v xx;
	delete -v tempPos;
	del _tempPos;
	del _tempLayer;
	/// YuI 9/19/03 v7.5700 SYSTEM_THEME_IS_NOT_APPLIED_TO_SOME_PLOTS
	ApplySystemThemeToActivePage();
	/// end SYSTEM_THEME_IS_NOT_APPLIED_TO_SOME_PLOTS
	return 0;

[Stack]
	rone=wks.r1;
	rtwo=wks.r2;

	if (exist(%H)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}

	//reject selection if it does not start with X or Y
	if (wks.col$(wks.c1).type!=1 && wks.col$(wks.c1).type!=4)
	{
		type -b $general.selectionXY;
		return 1;
	}

	if(wks.iscolsel()==0 || %(%H,@Y#,2)==0)
	{
		type -b $general.selectionXY;
		return 1;
	}

	%W=%H;					//Save the current worksheet name

	loop(ii,%(%W,@Y+,wks.c1),wks.c2)
	{
		if(wks.col$(ii).type==4)
		{
			type -b $Template.StackMultx;
			return 1;
		}	
	};

	count=wks.iscolsel();
	create _tempPos -t 1;
	create _tempLayer -t 1;
	xx=0;tempPos=1;
	loop(ii,1,count)
	{
		jj=wks.c1+%(%H,@O,ii-1);
		if(mod(wks.col$(jj).type,2)!=0)
		{
			_tempPos[tempPos]=jj;
			if(wks.col$(jj).type==1)
			{
				xx+=1;
				_tempLayer[tempPos]=xx;
			}
			else
				_tempLayer[tempPos]=xx;
			tempPos+=1;
		}
	}

	%W=%H;
	worksheet -ps;
	win -t plot stack;			//Get the stack.OTP template
	layer -wd %W _tempPos[1] rone _tempPos[1] rtwo 200;
	layer.x.showlabels=1;
	%A=xof(%(%W,_tempPos[1]));
	%B=%W!wks.col$(colnum(%A)).label$;
	if("%B"!="") 
		label -s -xb %B;		//Create the X Axis label from Column label
	else
		label -s -xb %[%A,>'_'];	//Create the X Axis label from Column Name
	xb.fsize=16;
	xb.font=1;

	%B=%W!wks.col$(colnum(%(1,@D))).label$;
	if("%B"!="") 
		label -s -yl %B;		//Create the X Axis label from Column label
	else
		label -s -yl %(1,@C);		//Create the X Axis label from Column Name


	loop(ii,2,tempPos-1)
	{
		if(_tempLayer[ii]>_tempLayer[ii-1])
		{
			page -l stack;
			layer.link=1;			//Link to layer 1
			layer.x.link=1;			//X axes linked one to one
			layer -wd %W _tempPos[ii] rone _tempPos[ii] rtwo 200;
			%B=%W!wks.col$(colnum(%(1,@D))).label$;
			if("%B"!="") 
			{
				label -s -yl %B;	//Create the X Axis label from the Column label
			}
			else
			{
				label -s -yl %(1,@C);	//Create the X Axis label from the Column Name
			}
		}
		else
			layer -wd %W _tempPos[ii] rone _tempPos[ii] rtwo ?;
		layer -a;
		layer.x.rescale=1;		//Set to manual, this must come after layer -a
	}

	page.active=1;
	layer -a;				//Rescale layer 1 to update ALL layers
	xGap=10;
	yGap=3;
	Left=25;
	Right=5;
	Bottom=10;
	Top=10;
	page -o p;
	queue {
		YLmin=page.width;
		loop(ii,1,page.nlayers)
		{
			layer -o ii
			{
				YLmin=yl.left<=YLmin?yl.left:YLmin;
			}
		}
		loop(ii,1,page.nlayers)
		{
			layer -o ii
			{
				yl.left=YLmin;
			}
		}
	}
	ArrangeLayers page.nlayers 1;
	del -v xx;
	del -v tempPos;
	del _tempPos;
	del _tempLayer;
	return 0;

[XYError]
	loop(ii,1,wks.iscolsel())
	{
		if(wks.col$(ii).type!=1 && wks.col$(ii).type!=4)
		{
			worksheet -p ? ErrBar;
			return;
		}
	}

	worksheet -p ? ErrBar;

	layer -cp;
	loop(ii,1,count)
	{
		switch(mod(ii,3))
		{
			case 2:
				set %(ii,@D) -o %((ii-1),@D); 		
				break;
			case 0:
				set %(ii,@D) -ox %((ii-2),@D);
				break;
		}
	}
	layer -a;
	return 0;

[YError]
	loop(ii,1,wks.iscolsel())
	{
		if(wks.col$(ii).type!=1 && wks.col$(ii).type!=4)
		{
			worksheet -p ? ErrBar;
			return;
		}
	}

	worksheet -p ? ErrBar;

	layer -cp;
	loop(ii,1,count)
	{
		if(mod(ii,2)==0)
			set %(ii,@D) -o %((ii-1),@D); 		
	}
	layer -a;
	return 0;

[CheckNumYCols]
//Make sure that there is at least one Y column in the worksheet selection
	count=0;
	for(ii=1,jj=wks.c1;ii<=$(wks.c2-wks.c1+1);ii++,jj++)
	{
		if(wks.col$(jj).type==1)
			count++;
	}
	if(count<1) 
	{
		type -b $General.SingleYError;
		return 1;
	}
	else
	{
		return 0;
	}