// 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
/// Max 5/11/07 STACK_PLOT_USING_XF
/// Max 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
///	CPY 5/17/08 QA70-11557 XYERR_PLOT_BUTTON_SHOULD_NOT_ADD_PLOT_TO_GRAPH
/// Max 9/16/09 QA70-14182 DISABLE_DLG_OPENING_WHEN_GRAPH_WINDOW_IS_ACTIVE
	/// ML 1/20/2010 QA70-12287-P2 SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
/// Folger 07/16/10 ORG-489-P2 UNKNOWN_COLUMN_SHOWN_AFTER_MAKE_COLOR_MAP_PLOT_FROM_EXCEL
////////////////////////////////////////////////////////////////////////////////

[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)==2)
	{
		worksheet -P %1 %2;
		set %C -l %3;
		Legend;
		return 0;
	}
	if (exist(%H,3)==3)
	{
		layer -b xu 0;          //Restore/Exchange Axes, add u for undo plot. 
		layer -iu%1 %C;			// add u for undo plot.
		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)==2)
	{
		worksheet -P %1 %2;
		Legend;
		return 0;
	}
	if (exist(%H,3)==3)
	{
		layer -b xu %3;         //Restore/Exchange Axes if 0/1 respectively, add u for undo plot.
		layer -iu%1 %C;			// add u for undo plot.
		return 0;
	}	

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

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

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

[IndexColor]
	if (exist(%H,2)!=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 >= @CATH )				//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);
		}
		///------ Folger 07/16/10 ORG-489-P2 UNKNOWN_COLUMN_SHOWN_AFTER_MAKE_COLOR_MAP_PLOT_FROM_EXCEL
		layer -o [book2]sheet1 { page -us; }
		///------ End UNKNOWN_COLUMN_SHOWN_AFTER_MAKE_COLOR_MAP_PLOT_FROM_EXCEL
	}
	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.
	if (exist(%H,2)!=2)
	{
		type -b $General.BadGraphType;
		return 1;
	}
	NumYCols = %(%H,@Y#,2);
	if ( NumYCols < 2 || NumYCols > 3 ) 
	{
		type -b $General.Only2or3YColumns;
		return 1;
	} 
	if(NumYCols==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 >= @CATH )				//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)!=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)==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]
	// CPY 1/13/06 QA70-8347 TEMPLATE_LIB_SIMPLY_PANEL_PLOTS 
	// old codes removed
	if (exist(%H,2)!=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;
	}
	run.section(plot, PlotToTemplate, %2);
	return 0;

[Stack]
	/// Max 9/16/09 QA70-14182 DISABLE_DLG_OPENING_WHEN_GRAPH_WINDOW_IS_ACTIVE
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	///---
	/// Max 5/11/07 STACK_PLOT_USING_XF
	/// Max 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
	//plotstack -d;
	run -xf plotstack -d;
	/// END LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
	//rone=wks.r1;
	//rtwo=wks.r2;
//
	//if (exist(%H,2)!=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;
	/// END STACK_PLOT_USING_XF
//----- CPY 5/17/08 QA70-11557 XYERR_PLOT_BUTTON_SHOULD_NOT_ADD_PLOT_TO_GRAPH
[ChkPlotWksErrBar]
	if(exist(%H,2)==2)
	{
		loop(ii,1,wks.iscolsel())
		{
			if(wks.col$(ii).type!=1 && wks.col$(ii).type!=4)
			{
				worksheet -p ? ErrBar;
				return;
			}
		}
	
		worksheet -p ? ErrBar;
	}
//------ end XYERR_PLOT_BUTTON_SHOULD_NOT_ADD_PLOT_TO_GRAPH
	
[XYError]
	//----- CPY 5/17/08 QA70-11557 XYERR_PLOT_BUTTON_SHOULD_NOT_ADD_PLOT_TO_GRAPH
	/*
	loop(ii,1,wks.iscolsel())
	{
		if(wks.col$(ii).type!=1 && wks.col$(ii).type!=4)
		{
			worksheet -p ? ErrBar;
			return;
		}
	}

	worksheet -p ? ErrBar;
	*/
	run.section(,ChkPlotWksErrBar);
	//------ end XYERR_PLOT_BUTTON_SHOULD_NOT_ADD_PLOT_TO_GRAPH

	/// ML 1/20/2010 QA70-12287-P2 SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	if(exist(%H,3)==3)
	{
	/// end SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	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;
	/// ML 1/20/2010 QA70-12287-P2 SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	}
	/// end SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	return 0;

[YError]
	//----- CPY 5/17/08 QA70-11557 XYERR_PLOT_BUTTON_SHOULD_NOT_ADD_PLOT_TO_GRAPH
	/*
	loop(ii,1,wks.iscolsel())
	{
		if(wks.col$(ii).type!=1 && wks.col$(ii).type!=4)
		{
			worksheet -p ? ErrBar;
			return;
		}
	}

	worksheet -p ? ErrBar;
	*/
	run.section(,ChkPlotWksErrBar);
	//------ end XYERR_PLOT_BUTTON_SHOULD_NOT_ADD_PLOT_TO_GRAPH
	
	/// ML 1/20/2010 QA70-12287-P2 SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	if(exist(%H,3)==3)
	{
	/// end SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	layer -cp;
	loop(ii,1,count)
	{
		if(mod(ii,2)==0)
			set %(ii,@D) -o %((ii-1),@D); 		
	}
	layer -a;
	/// ML 1/20/2010 QA70-12287-P2 SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	}
	/// end SETUP_ERRORBAR_PLOTS_ONLY_IF_GRAPH
	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;
	}

///Kit 5/28/2010 ORG-123 S1 ADD_Y_AND_Z_VALUE_COLOR_MAPPING_BUTTON
[Waterfall_Y_Value_CL_MAP]
//Plot the selected columns as a water fall line graph
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}

	worksheet -P ? Waterfal;
	set %C -LGC 0;
	return 0;

[Waterfall_Z_Value_CL_MAP]
//Plot the selected columns as a water fall line graph
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}

	worksheet -P ? Waterfal;
	set %C -LGC 3;
	return 0;
///End ADD_Y_AND_Z_VALUE_COLOR_MAPPING_BUTTON


