////////////////////////////////////////////////////////////////////////////////
// Filename:    plot.ogs
// Creation:    8/12/1995
// Purpose:     To generate plots from menu or from 2D toolbar
// Modifications:
// JMP v3.85 11/03/95 				Rewrite most parts to be used from either wks or graph
// LAS  v4.0 01/16/96	STORE_OBJ 		Box Chart now allows dots in the worksheet name
// JMP v4.12 07/23/96	LINESERIES_MULTIX	Use multiX setting when adding LineSeries Data
// JMP v4.13 07/27/96	COLUMN_RENAME		Rename UIM Status property from Column to WksCol to avoid starting with 'c'
// CPY v4.986 5/15/97	TERNAY_NO_Y_COL		force plot id to 245 to prevent Y colum in plto
// JMP v4.995 6/06/97	USE_FDLOG_TO_TRACK	Use FDLOG object to use template path and to track user for template path
// SFX 6/4/98 v6.0123 ADD_FLOW_VECTOR_PLOT_TYPE
// CPY 7/28/98 v6.0127 b4098 CNTRL_SEL_COL_NEED_SCRIPT_CHANGE
// ML 8/6/98 v6.0127 SCRIPT_CHANGE_FOR_FLOW_VECTOR
// SFX 10/28/98 v6.0135 FLOATING_BAR_AND_COLUMN_ARE_SWAPED
// RB	11/4/98 v6.0135 RESTORE_FLOATING_BAR_AND_COLUMN
// SDB 2/1/99 v.6.0151 HIST_PROB_UPDATE
// CPY 3/19/99 v6.0176 t5596 MOVE_COMPLEX_PLOT_NOT_FROM_SCRIPT
// CPY v6.0190 4/26/99
// SFX 5/24/99 v6.0205 t6484 ZOOM_PLOT_CAUSES_CRASH
// CPY 10/28/99 t7082 v6.0236 PLOT_TO_TEMPLATE_CHECK_GROUP
// CPY 06/26/2001 t6834 v6.9018 ADD_SMITH_CHART_PLOT_HOOK_UP
// CPY v7.0311 QA70-1736 5/24/02 ARG_COPY_COL_LABEL_OTHER_LAYERS
// JJ 10/29/2001 t9578 SMITH_CHART_IMPROVMENTS
// YuI 9/19/03 v7.5700 SYSTEM_THEME_IS_NOT_APPLIED_TO_SOME_PLOTS
// SDB 12/11/03 QA5694 v7.5780 MODAL_TEMPLATE_LIBRARY
// CPY 2/4/04 v7.5815 QA70-5930 DOUBLE_Y_FAILED_ON_2ND_LAYER_WITH_TEXT_DATASET
// CPY 12/30/05 QA70-8347 REPLACE_TEMPLATE_LIB_WITH_TEMPLATE_ORGANIZER
// CPY 1/13/06 QA70-8347 TEMPLATE_LIB_SIMPLY_PANEL_PLOTS 
// CPY 9/18/06 ID_PLOT_Y_OFFSET_LINES
// CPY 10/24/06 PLOT_SCATTER_MATRIX
// CPY 4/16/07 QA70-9636 REVERTING_DATA_SELECTOR_TO_EXPANDED_OLD
// Echo 7/12/07 ASSOCIATE_DATASET_TO_SHEET
// Hong 08/02/07 v8.0673 DEFAULT_STACK_LINE_LEGEND_TO_DESCENDING
// ML 10/4/2007 QA70-10455 ALLOW_NEW_HISTOGRAM_PLOT_MANAGEMENT_FROM_MATRIX
// Max 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
// ML 11/26/2007 QA70-10624 INTERNALLY_SWAPPED_SERIESPTRS_IN_OBJECTSTRUCT_FOR_POLAR
// CPY 7/23/08 QA70-11758 DOC_DASH_E_LAYER_CHANGED_ACTIVE_LAYER_ON_INACTIVE_PAGE
// CPY 10/21/08 QA70-12248 POLAR_PLOT_CANNOT_USE_UNKNOWN_TYPE
// CPY 12/6/06 QA70-12736 LT_NEED_MORE_CNTRL_ON_ALLOWING_OC_FUNCS
// EJP 2009-03-17 QA80-13289 ADD_3Y_AND_4Y_PLOTS, add 3Ys and 4Ys handling
// EJP 2009-03-18 QA80-13289 ADD_WINDROSE_PLOT
// Folger 04/27/09 QA80-13420 LINESERIES_XF_BASED_PLOTTING
// Folger 04/22/09 QA80-13420 PARETOCHART_XF_BASED_PLOTTING
// EJP 2009-06-18 QA80-13610 PLOT_MENU_REORGANIZE
// Iris 7/27/2009 ADD_PROBABILITY_PLOT_ON_TOOLBAR_AND_MENU
// Kenny 7/28/2009 QA80-13992 ADD_MENU_AND_TOOLBAR_FOR_ACCESSING_XF_PLOTMAXES
/// Max 8/17/09 v8.1096 CHINESE_DISTRIBUTOR_FOUND_LEGEND_NOT_UPDATE_FOR_MULTI_CHARTS
/// Iris 8/17/2009 IMPROVE_PP_PLOT
/// Kenny 9/15/2009 QA81-14182 XF_PLOT_BY_LABEL_ADD_MENU_ACCESSIBILITY
/// Max 9/16/09 QA70-14182 DISABLE_DLG_OPENING_WHEN_GRAPH_WINDOW_IS_ACTIVE
/// Hong 12/19/08 QA80-14865-P1 FIX_RESCALE_FAIL_IF_ACTIVE_WND_CHANGE_AFTER_PLT_IN_LT
// EJP 2010-01-11 QA81-14373 ADD_COLUMN_WITH_LABEL_PLOT
/// Kenny 1/18/2010 QA81-14570 ADD_NEW_GUI_TO_ADD_XY_VALUES_AS_LABELS_TO_PLOT
/// ML 1/27/2010 QA70-14470-P4 KEEP_STACK_UNSTACK_FOR_PLOT_TYPE_STACKED_UNSTACKED_PAIRS
/// CPY 6/10/2010 ORG-282 CANDLESTICK_VOL_CHART
/// CPY 7/21/10 ORG-620 REMOVE_AXIS_TITLE_LEGENT_FROM_STOCK_CHARTS
////////////////////////////////////////////////////////////////////////////////
// Standard Origin Plots
// For Customer Modifications:
//	For customized plotting routine for any of the basic graph types, 
//	replace the code calling the General section with your custom code
//	which you may model after the General section.
////////////////////////////////////////////////////////////////////////////////

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



[Line]
	run.section(,general,200 Line 0);

[Scatter]
	run.section(,general,201 Scatter 0);

[LineSymbol]
	run.section(,general,202 LineSymb 0);

[Column]
	run.section(,general,203 Column 0);

[ColumnLabel]
	/// CPY 3/23/2010 QA81-14570 ADD_COLUMN_LABEL_TO_MENU
	run.section(,general,203 ColumnLabel 0);
	/// End ADD_COLUMN_LABEL_TO_MENU

[StackCol]
	/// ML 1/27/2010 QA70-14470-P4 KEEP_STACK_UNSTACK_FOR_PLOT_TYPE_STACKED_UNSTACKED_PAIRS
	//run.section(,general,213 Column 0);
	run.section(,general,213 StackColumn 0);
	/// end KEEP_STACK_UNSTACK_FOR_PLOT_TYPE_STACKED_UNSTACKED_PAIRS

[Bar]
	run.section(,general,215 Bar 1);

[StackBar]
	/// ML 1/27/2010 QA70-14470-P4 KEEP_STACK_UNSTACK_FOR_PLOT_TYPE_STACKED_UNSTACKED_PAIRS
	//run.section(,general,216 Bar 1);
	run.section(,general,216 StackBar 1);
	/// end KEEP_STACK_UNSTACK_FOR_PLOT_TYPE_STACKED_UNSTACKED_PAIRS

/// ML 1/27/2010 QA70-14470-P4 KEEP_STACK_UNSTACK_FOR_PLOT_TYPE_STACKED_UNSTACKED_PAIRS
//[Area]
//	run.section(,general,214 Area 0);
//
[Area]
	run.section(,general,204 Area 0);

[StackArea]
	run.section(,general,214 StackArea 0);
/// end KEEP_STACK_UNSTACK_FOR_PLOT_TYPE_STACKED_UNSTACKED_PAIRS

[pClamp]
	run.section(,general,200 pCLAMP 0);

[Pie]
	run.section(,general,225 Pie 0);

[PieBW]
	run.section(,general,225 PieBW 0);

[PiecePie]
	worksheet -Plot 225 Pie;
	return 0;

[HiLoClose]
	run.section(,general,205 HLClose 0);

[Barchart]
	if (exist(%H,3)==3)
	{
		layer -b xu %3;
		layer -iu%1 %C;
		set %c -k 1;
		return 0;
	}
	else
	{
		//--CPY 7/21/10 ORG-620 REMOVE_AXIS_TITLE_LEGENT_FROM_STOCK_CHARTS
		//run.section(,general, 221 OHLCBarchart);
		worksheet -P 221 OHLCBarchart;
		//--
	}
	
[Candlestick]
	if (exist(%H,3)==3)
	{	
		layer -b xu %3;
		layer -iu%1 %C;
		set %c -k 0;
		return 0;
	}
	else
	{
		//--CPY 7/21/10 ORG-620 REMOVE_AXIS_TITLE_LEGENT_FROM_STOCK_CHARTS
		//run.section(,general, 221 Candlestick);
		worksheet -P 221 Candlestick;
		//--
	}


//---- CPY 6/10/2010 ORG-282 CANDLESTICK_VOL_CHART
[OpenHiLoCloseVol]
	if (exist(%H,2)==2) worksheet -P 221 OhlcVol;
//---- end CANDLESTICK_VOL_CHART
	
////////////////////////////////////////////////////////////////////////////////
// Floating Column Plots
////////////////////////////////////////////////////////////////////////////////
[FloatColumn]
	run.section(,general,207 FloatCol 0);

////////////////////////////////////////////////////////////////////////////////
// Floatting Bar Plots
////////////////////////////////////////////////////////////////////////////////
[FloatBar]
	//Plot the selected region with at least two y columns as floating bars
	run.section(,general,207 FloatBar 0);

////////////////////////////////////////////////////////////////////////////////
// Vector Plots
////////////////////////////////////////////////////////////////////////////////
[Vector]
	run.section(,general,208 vector 0);

////////////////////////////////////////////////////////////////////////////////
// FlowVector Plots
////////////////////////////////////////////////////////////////////////////////
[FlowVector]
	run.section(,general,218 VectXYXY 0);
	
////////////////////////////////////////////////////////////////////////////////
// Line
////////////////////////////////////////////////////////////////////////////////
[LineSeries]
	//New Line Series code
	//uses new BREAK object
	//JMP 09/15/1995
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}

	NumYs = %(%H,@Y#,2);
	/// Labtalk has this kind of ugly bug that || has higher precedence that && ...
	if ( NumYs != wks.iscolsel() || (NumYs!=2 && NumYs!=3) )
	{
		type -b $general.Only2or3YColumns;
		break 1;
	} 

	///------ Folger 04/27/09 QA80-13420 LINESERIES_XF_BASED_PLOTTING
	//create.wksName$="LineSeries";
	//create.enumwks=1;
	//create.wkslabel$=$Plot.LineSeriesLabel;
	//create.template$="CREATE";
	//create.npts=30;				//Use something common for default - columns will expand as necessary
	//create.enumerate=1;
	//create.numtypes=2;			
	//create.type1=4;				//Only explicitly specify 1st type as X
	//create.type2=1;				//Set TypeN=Y column type
	//create.wks(Series Values);
	//
	//%A=create.data1$;			//X data (Series)
	//%B=create.data2$;			//Y data (Values)
//
	//break.open(Constructing new columns..);
	////---- CPY 5/25/07 QA70-9007 HIDDEN_BOOK_CANNOT_SELECT_AND_PLOT_FROM_LT
	////break.min=beg;break.max=end;
	//break.min=wks.r1;break.max=wks.r2;
	////----
	//count=%(%H,@Y#,2);
	//
	//xx=1;
	//loop(ii,wks.r1,wks.r2)
	//{
		//break.set(ii);
		//kk=0;
		//loop(jj,1,count)
		//{
			//kk=%(%H,@YS,kk+1);
			//%A[xx]=jj;
			//%B[xx]=wcol(kk)[ii];
			//xx+=1;
		//}
	//}			
	//break.close();
//
	//%A=create.wksname$;
	////---- CPY 5/25/07 QA70-9007 HIDDEN_BOOK_CANNOT_SELECT_AND_PLOT_FROM_LT
	//// temp fix, until 9007 is fixed
	///*
	//win -oa %A {
		//worksheet -s;
		//worksheet -s (%A!wks.ncols-1) 0 (%A!wks.ncols) 0;
		//worksheet -P ? LSer$(count);
	//}
	//*/
	//win -t plot LSer3;// change to used this same template always, I see no difference between Lser2 and Lser3.
	//layer -w %A;
	//set %C -l count;// this will take care of template differences.
	////-----
	//delete -v kk;
	//delete -v xx;
	//delete -v NumYs;
	
	worksheet -px ? Lser3 plot_lineser;
	///------ End LINESERIES_XF_BASED_PLOTTING

	return 0;

[FillArea]
	run.section(,general,249 FillArea 0);

[DoseRespFit]
	///This code is to satisfy people who are used to plotting into the old DOSERESP.OTP plot
	if (exist(%H,2)==2)
	{
		worksheet -P 201 scatter;
		layer.x.space=2;                //Set X axis to Log10
		layer -a;                       //Rescale
		dotool -w sr;                   //Activate Sigmoidal Regression tool
		return 0;
	}
	if (exist(%H,3)==3)
	{
		dotool -w sr;
		return 0;
	}
//----- CPY 1/13/06 QA70-8347 TEMPLATE_LIB_SIMPLY_PANEL_PLOTS
[PlotToTemplate]
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	/// EJP 2009-03-17 QA80-13289 ADD_3Y_AND_4Y_PLOTS
	///worksheet -P ? %1;
	worksheet -P ? "%1"; // use quotes to support dash in template file name
	/// end ADD_3Y_AND_4Y_PLOTS
	legend -s;
//------

////////////////////////////////////////////////////////////////////////////////
// Double Y (Line/Symbol)
////////////////////////////////////////////////////////////////////////////////
[DoubleY]
	//CPY 1/13/06 QA70-8347 TEMPLATE_LIB_SIMPLY_PANEL_PLOTS
	// original LT code to make double-Y plot is removed
	run.section(,PlotToTemplate,DOUBLEY);
	return 0;

/// EJP 2009-03-17 QA80-13289 ADD_3Y_AND_4Y_PLOTS, add 3Ys and 4Ys handling
[3Ys_Y-YY]
	run.section(,PlotToTemplate,3Ys_Y-YY);
	return 0;
[3Ys_Y-Y-Y]
	run.section(,PlotToTemplate,3Ys_Y-Y-Y);
	return 0;
[4Ys_Y-YYY]
	run.section(,PlotToTemplate,4Ys_Y-YYY);
	return 0;
[4Ys_YY-YY]
	run.section(,PlotToTemplate,4Ys_YY-YY);
	return 0;
/// end QA80-13289 ADD_3Y_AND_4Y_PLOTS, add 3Ys and 4Ys handling

////////////////////////////////////////////////////////////////////////////////
// Water Fall (XY offset)
////////////////////////////////////////////////////////////////////////////////
[WaterFall]

	//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;

/// EJP 2009-03-18 QA80-13289 ADD_WINDROSE_PLOT
[Windrose]
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	worksheet -P 213 Windrose;
	return 0;
/// end ADD_WINDROSE_PLOT

////////////////////////////////////////////////////////////////////////////////
// Polar Plot
////////////////////////////////////////////////////////////////////////////////
[Polar]
	//Plot selected columns as polar graph
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	//----- CPY 10/21/08 QA70-12248 POLAR_PLOT_CANNOT_USE_UNKNOWN_TYPE
	//worksheet -P ? Polar;
	worksheet -P 192 Polar;
	//-----
	Legend;
	return 0;


/// ML 11/26/2007 QA70-10624 INTERNALLY_SWAPPED_SERIESPTRS_IN_OBJECTSTRUCT_FOR_POLAR
////////////////////////////////////////////////////////////////////////////////
// Polar Plot r(X), theta(Y)
////////////////////////////////////////////////////////////////////////////////
[PolarXrYTheta]
	//Plot selected columns as polar graph
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}

	worksheet -P 186 PolarXrYTheta;
	Legend;
	return 0;
/// end INTERNALLY_SWAPPED_SERIESPTRS_IN_OBJECTSTRUCT_FOR_POLAR


//---------  CPY 06/26/2001 t6834 v6.9018 ADD_SMITH_CHART_PLOT_HOOK_UP
////////////////////////////////////////////////////////////////////////////////
// Smith Chart Plot
////////////////////////////////////////////////////////////////////////////////
[SmithChart]
	//Plot selected columns as Smith Chart graph
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}	

	worksheet -P ? SmithCht;
	
	layer -b ps 1; // check to convert to Smith Chart layer if not already so
	Legend;
	
	
	return 0;	
//---------	end CPY 06/26/2001

////////////////////////////////////////////////////////////////////////////////
// Ternary Plot
////////////////////////////////////////////////////////////////////////////////
[Ternary]
	//Plot selected columns as polar graph
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	if (%(%H,@Z#,2)!=1)
	{
		type -b $general.Only1ZColumn;
		break 1;
	}

	//CPY 5/15/97 TERNAY_NO_Y_COL, explicit id will prevent Y columns added to layer
	//worksheet -P ? Ternary;  
	worksheet -P 245 Ternary;
	Legend;
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Box Chart
////////////////////////////////////////////////////////////////////////////////
[BoxChart]
	//Create a Box chart for the selected columns
	//JMP modified 08/30/1995
	//Main code taken from old STAT.TXT file

	if( @och == 0 )	/// YuI 11/02/04 V7.5158 QA70-7122 OC_DATA_PLOT_CREATION
	{
		if(run.section(,CheckBinCreate)==0)
			type -c $Histogram.PrevCreated;
	
		if (exist(%H,2)!=2)
		{
			type -b $general.BadGraphType;
			return 1;
		}
	
		if (%(%H,@Y#,2) < 1) 
		{
			type -b $general.singleYError;
			break 1;
		}
	}
	else
	{
		/// YuI 12/21/07 QA7-10854 MOVE_HISTOGRAM_TO_VC
		// obsolete
		/*
		if( run.LoadOC(OriginLab\DataPlot.cpp) != 0 )
		{
			type "DataPlot.cpp not found or can not compile";
			return 0;
		}
		*/
		/// end MOVE_HISTOGRAM_TO_VC
		/// YuI 01/08/08 qa70-10828 PLOTTING_HISTOGRAM_SHOULD_BE_PREVENTED_WHEN_GRAPH_ACTIVE
		if (exist(%H,2)!=2)
		{
			type -b $general.BadGraphType;
			return 1;
		}
		/// end PLOTTING_HISTOGRAM_SHOULD_BE_PREVENTED_WHEN_GRAPH_ACTIVE
	}
	worksheet -p 206 Box;
	if( @och == 0 )
	{
		get %C -nhwc A;
		win -o %A {win -h 1;}
		/// Max 8/17/09 v8.1096 CHINESE_DISTRIBUTOR_FOUND_LEGEND_NOT_UPDATE_FOR_MULTI_CHARTS
		// legend;
		/// ---
	}
	/// Max 8/17/09 v8.1096 CHINESE_DISTRIBUTOR_FOUND_LEGEND_NOT_UPDATE_FOR_MULTI_CHARTS
	legend;
	/// ---
	return 0;

[UpdateBoxChart]

	%W=store.wksname$;//capture worksheet name stored in the storage object
	for (j=1,i=store.selc1;i<=store.selc2;j+=1,i+=1) 
	{
		sum(%(%W,i));
		%(%H,1,j)       = %(%W,@Col,i);
		col(mean)[j]    = sum.mean;
		col(max)[j]     = sum.max;
		col(min)[j]     = sum.min;
		col(sd)[j]      = sum.sd;
		dummy={1,99};
		dummy=percentile(%(%W,i),dummy);
		col(P1)[j]      = dummy[1];
		col(P99)[j]     = dummy[2];
	}
	del dummy;
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Histogram from Matrix window
////////////////////////////////////////////////////////////////////////////////
[MatrixHistogramOldCode]
	matrix -id; // check if data is available, if not, convert to data first
	worksheet -p 219 MHisto;
	get %C -nhwc A;
	get %C -se v1;// get internal data size = 1,2,4,8,
	if(v1==1) // byte data, must be image histogram, 0-255
	{
		if(x1 < 0)
			x1 = 0;
		if(x2 > 255)
			x2 = 255;
	}
	win -o %A {win -h 1;};
	legend;
//------ CPY 7/19/06 QA70-8839 MAT_HISTOGRAM
[MatrixHistogram]
	int bIsImage, nColorBits;
	matrix -pg Image bIsImage;
	matrix -pg Bits nColorBits;
	/// YuI 07/31/08 QA70-11923 IMAGE_HISTOGRAM_PROBLEM
	if( bIsImage && nColorBits !=24 )
	{
		matrix -ig; // this will convert it to grayscale
		imgHistogram c:=0; // single channel
		return 0;	
	}
	/// end IMAGE_HISTOGRAM_PROBLEM
	if(bIsImage && nColorBits ==24)
	{
		imgHistogram;
	}
	else
	{
		//	plotHisto2;
		/// ML 10/4/2007 QA70-10455 ALLOW_NEW_HISTOGRAM_PLOT_MANAGEMENT_FROM_MATRIX
		if (bIsImage)
			matrix -ig;
		/// end ALLOW_NEW_HISTOGRAM_PLOT_MANAGEMENT_FROM_MATRIX
		return 1;
	}

	return 0;
//------
////////////////////////////////////////////////////////////////////////////////
// Histogram
////////////////////////////////////////////////////////////////////////////////
[Histogram]
	//------------ CPY 10/11/01 v6.9103 t9576 HISTOGRAME_FROM_MATRIX
	if(exist(%H,5)==5) // from a matrix
	{
		if( run.section(,MatrixHistogram) == 0 )
			return 0;
	}
	//------------
	if(@och == 0) /// YuI 11/02/04 V7.5158 QA70-7122 OC_DATA_PLOT_CREATION
	{
		/// GCJ 7/21/98 v6.0125 t1320 DRAW_HISTOGRAM_PLOT
		if(run.section(,CheckBinCreate)==0)
			type -c $Histogram.PrevCreated;
	
	
		if (exist(%H,2)!=2)
		{
			type -b $general.BadGraphType;
			return 1;
		}
	
		
		if(wks.iscolsel()!=%(%H,@Y#,2)) 
		{
			type -b $general.RangeYColumns;
			break 1;
		}
	}	
	else
	{
		/// YuI 12/21/07 QA7-10854 MOVE_HISTOGRAM_TO_VC
		// obsolete, moved to VC
		/*
		if( run.LoadOC(OriginLab\DataPlot.cpp) != 0 )
		{
			type "DataPlot.cpp not found or can not compile";
			return 0;
		}
		*/
		/// end MOVE_HISTOGRAM_TO_VC
		/// YuI 01/08/08 qa70-10828 PLOTTING_HISTOGRAM_SHOULD_BE_PREVENTED_WHEN_GRAPH_ACTIVE
		if ( exist(%H,2)!=2 && exist(%H,5) != 5 )
		{
			type -b $general.BadGraphType;
			return 1;
		}
		/// end PLOTTING_HISTOGRAM_SHOULD_BE_PREVENTED_WHEN_GRAPH_ACTIVE
	}
	
	worksheet -p 219 Hist;
	
	if( @och == 0 )
	{
		get %C -nhwc A;
		win -o %A {win -h 1;};
	}
	
	legend;
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Stacked Histogram
////////////////////////////////////////////////////////////////////////////////
[StackHist]
	/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	// obsolete
	//	if(run.section(,CheckBinCreate)==0)
	//		type -c $Histogram.PrevCreated;
	/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	
	/// SDB 2/3/99 v6.0156 
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}

	if(wks.iscolsel()==0 || %(%H,@Y#,2)==0)
	{
		/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
		//	type -b $general.selectionXY;
		type -b $general.RangeYColumns;
		/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
		return 1;
	}

	if(wks.iscolsel()!=%(%H,@Y#,2)) 
	{
		type -b $general.RangeYColumns;
		break 1;
	}

	%W=%H;					//Save the current worksheet name
	%Z="";ii=0;
	count=%(%W,@Y#,2);
	loop(jj,1,count)
	{
		ii=%(%W,@YS,ii+1);
		%Z=%Z $(ii);
	};

	win -t plot Hist;				//Get the Origin.OTP template

	layer -i219 %(%W,%[%Z,#1]);			//Include the first Y column
	
	/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	// obsolete
	//	get %(%W,%[%Z,#1]) -nhwc A;
	//	win -o %A {win -h 1;};
	/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM

	loop(ii,2,count)
	{
		page -l Hist;
		layer -i219 %(%W,%[%Z,#ii]);		//Include Y column
		/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
		// obsolete
		//	get %(%W,%[%Z,#ii]) -nhwc A;
		//	win -o %A {win -h 1;};
		/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
		label -r XB;				//Remove the X axis label for all but the first layer
	};

	xGap=10;
	yGap=5;
	Left=25;
	Right=5;
	Bottom=10;
	Top=10;
	page -o p;

	ArrangeLayers page.nlayers 1;
	//CPY 7/23/08 QA70-11758 DOC_DASH_E_LAYER_CHANGED_ACTIVE_LAYER_ON_INACTIVE_PAGE, I changed from doc -e L to doc -e LW as it should not touch any other graph
	/// Hong 12/19/08 QA80-14865-P1 FIX_RESCALE_FAIL_IF_ACTIVE_WND_CHANGE_AFTER_PLT_IN_LT
	//queue {
	/// end FIX_RESCALE_FAIL_IF_ACTIVE_WND_CHANGE_AFTER_PLT_IN_LT
		doc -e LW {
			rescale;
		};
	/// Hong 12/19/08 QA80-14865-P1 FIX_RESCALE_FAIL_IF_ACTIVE_WND_CHANGE_AFTER_PLT_IN_LT
	//};
	/// end FIX_RESCALE_FAIL_IF_ACTIVE_WND_CHANGE_AFTER_PLT_IN_LT
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Histogram + Probabilities
////////////////////////////////////////////////////////////////////////////////
[HistProb]
	//Creates a histogram from the selected data and performs a cumulative count of the data
	/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	// obsolete
	//	if(run.section(,CheckBinCreate)==0)
	//		type -c $Histogram.PrevCreated;
	/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM

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

	if (%(%H,@Y#,2)!=1||wks.iscolsel()!=1)
	{
		type -b $general.Only1YColumn;
		return 1;
	}
	
	sum(%(%H,%(%H,@YS,1)));
	///SDB 2/1/99 REDIRECT_TO_RESULTS_LOG
	SaveRedirection=type.Redirection(16,3);	// SDB 1/7/99 REPORT_TO_OUTPUTLOG
	type.BeginResults();		/// RKM 12/10/98 v6.0141 REPORT_TO_OUTPUTLOG

	type $histogram.Title;                                  //Histogram & Probabilities
	type;                                                   //Blank Line
	type $histogram.ParaHeader1;
	separator 5;
	type "$(sum.mean)\t$(sum.sd)\t$(sum.max)\t$(sum.min)\t$(sum.n)";

	type.EndResults();
	type.Redirection=SaveRedirection; // restore previous redirection.
	delete -v SaveRedirection;
	doc -cp HistProb;
	%B=%(%H,%(%H,@YS,1));
	win -t Plot HistCumu HistProb$(count+1);
	/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	//	queue {
	//		layer -o 1 {rescale;}
	//}
	/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	layer -o 1 {
		layer -i219 %B;
		
		/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
		// obsolete
		/*
		/// YuI 08/29/06 
		//	there us problem with timing (when %c is set)
		// we may not use it right after the layer -i219
		//	get %C -nhwc A;
		get %B -nhwc A;
		/// end YuI
		
		win -o %A {win -h 1;};
		*/
		/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	};
	
	/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	//	layer -o 2 {
	//		layer -i201 %(%A,4);
	//		rescale;
	//	};
	PlotCumulativeProb();
	/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	return 0;

[CheckBinCreate]
	/// YuI 06/17/08 QA70-11717 STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	// obsolete
	//	get %C -nhw A;
	/// end STACKED_HISTOGRAM_AND_HIST_PROBABILITIES_PROBLEM
	return 0;	

////////////////////////////////////////////////////////////////////////////////
// QC (X bar R) Chart
////////////////////////////////////////////////////////////////////////////////
[QCchart]
	//Plots the selected data into the Quality Control template
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}

	if (exist(%C)!=1) 
	{
		type -b $general.selectionError;
		break 1;
	} 

	if (wks.iscolsel()!=(wks.c2-wks.c1+1)) 
	{
		type -b $general.ContiguousError;
		break 1;
	} 

	%W=%H;
	%B=%(%H,wks.c1);
	wksheet$=%(wks.name$);///Echo 7/12/07 ASSOCIATE_DATASET_TO_SHEET	
	getenumwks QC;
	qc.worksheet$=%W;
	

	//JMP COLUMN_RENAME
	//qc.column1=%W!wks.c1;
	qc.WksCol1=%W!wks.c1;
	qc.WksCol2=%W!wks.c2;

	qc.plot$="";
	worksheet -Link %B;
	if(qc.WksCol2 - qc.WksCol1 >= 1) 
	{
		qc.groupsize=qc.WksCol2-qc.WksCol1 + 1;
		type -c $plot.QCHeader1;
	}
	else 
		getn -s $plot.qcchartSubgroup qc.groupsize $plot.qcchartTitle;
	qc.run();
	return 0;

////////////////////////////////////////////////////////////////////////////////
// Template
////////////////////////////////////////////////////////////////////////////////
[Template]
	//Pick a template file and plot all selected columns into it
	//----- CPY 12/30/05 QA70-8347 REPLACE_TEMPLATE_LIB_WITH_TEMPLATE_ORGANIZER
	// old codes with temp lib removed, see VSS for original code
	///---- CPY 12/6/06 QA70-12736 LT_NEED_MORE_CNTRL_ON_ALLOWING_OC_FUNCS
	/*
	if(run.LoadOC(Originlab\TemplateOrganizer) == 0)
		OpenTemplateOrganizer 1;
	else
		type -b "Failed to compile TemplateOrganizer";
	*/
	if( 0 == Run.Section(File, LoadOC, "Originlab\TemplateOrganizer") )
		run -oc OpenTemplateOrganizer 1;
	//-----
		
////////////////////////////////////////////////////////////////////////////////
// Bring up the Zoom template and Initialize it
////////////////////////////////////////////////////////////////////////////////
//
// CPY v6.0190 4/26/99 change plot type from 201 into 200, as we should use line plots
//
[zoom]
	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;
	}

	/// SFX 5/24/99 v6.0205 t6484 ZOOM_PLOT_CAUSES_CRASH
	worksheet -ps;
	/// end ZOOM_PLOT_CAUSES_CRASH

	count=wks.iscolsel();
	create _tempPos -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
			tempPos+=1;
		}
	};

	%W=%H;
	win -t plot zoom1;
	loop(ii,1,tempPos-1)
	{
		/// SFX 5/24/99 v6.0205 t6484 ZOOM_PLOT_CAUSES_CRASH
		//layer -s 2;layer -w %W _tempPos[ii] rone _tempPos[ii] rtwo 200;
		//layer -s 1;layer -w %W _tempPos[ii] rone _tempPos[ii] rtwo 200;
		layer -s 2;layer -wd %W _tempPos[ii] rone _tempPos[ii] rtwo 200;
		layer -s 1;layer -wd %W _tempPos[ii] rone _tempPos[ii] rtwo 200;
		/// end ZOOM_PLOT_CAUSES_CRASH
	}
	page.active=2;
	layer -g;
	page.active=1;
	layer -g;

	del -v rone;del -v rtwo;del -v tempPos;
	del _tempPos;

	queue
	{
		limit %C;
		zoomrect.x=limit.xmin;
		zoomrect.y=%C(limit.xmin);
		zoomrect.run();
	};
	return 0;

//---- CPY 10/21/04 QA70-6954 CONTOUR_TRIANGULATION_PLOT
[TriContourPlot]
	//run.section(,general,243 TriContour 0);
	worksheet -p 243 TriContour;
	Spectrum;

//---- end  QA70-6954

//------ CPY 1/3/06 QA70-2306 POLAR_CONTOUR_PLOT
[PolarContourPlot]
	worksheet -p 243 PolarContour;
	Spectrum;
//------

//------ Fisher 04/28/09 QA80-2935 ADD_TERNARY_CONTOUR_PLOT
[TernaryContourPlot]
	worksheet -p 185 TernaryContour;
	Spectrum;
//------

/// ML 11/26/2007 QA70-10624 INTERNALLY_SWAPPED_SERIESPTRS_IN_OBJECTSTRUCT_FOR_POLAR
[PolarContourPlotXrYThetaZ]
	worksheet -pw 243 PolarContour;
	Spectrum;
/// end INTERNALLY_SWAPPED_SERIESPTRS_IN_OBJECTSTRUCT_FOR_POLAR


	
//----- CPY 9/18/06 ID_PLOT_Y_OFFSET_LINES
[OffsetYs]
	if (exist(%H,2)==2)
	{
		worksheet -P 200 OffsetStackY;
		/// Hong 08/02/07 v8.0673 DEFAULT_STACK_LINE_LEGEND_TO_DESCENDING
		//Legend;
		legendupdate update:=0 order:=0;
		/// end DEFAULT_STACK_LINE_LEGEND_TO_DESCENDING
	}
	else if(exist(%H,3) !=3 )
		return 0;

	// set to offset Y mode
	layer -b stack -2; // -1 to toggle, -2 to ensure stack offset, default to Auto
	return 0;
//-----

//----- CPY 10/24/06 PLOT_SCATTER_MATRIX
[ScatterMatrix]
	/// 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 10/25/07 QA70-10581 LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
	//plotMatrix -d;
	run -xf plotMatrix -d;
	/// END LANGUAGE_SWITCH_CRASH_IN_J_LEAD_TO_NEW_LT_WAY_TO_RUN_XF
//-----



//---- CPY 4/16/07 QA70-9636 REVERTING_DATA_SELECTOR_TO_EXPANDED_OLD
[ClearDataMarkers]
	mark -r;
//----
[CreateDataMarkers]
	mark -p;
	
/// Iris 01/11/2010 QA81-5862 ADD_PARETO_CHART_BINNED_DATA	
[ParetoChartBinnedData]
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	worksheet -px ? ParetoBin plot_paretobin -d;
///End ADD_PARETO_CHART_BINNED_DATA

///------ Folger 04/22/09 QA80-13420 PARETOCHART_XF_BASED_PLOTTING
/// Iris 01/11/2010 QA81-5862 ADD_PARETO_CHART_BINNED_DATA
//[ParetoChart]
[ParetoChartRawData]
///End ADD_PARETO_CHART_BINNED_DATA
	/// 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;
	}
	///---
	/// Iris 01/11/2010 QA81-5862 ADD_PARETO_CHART_BINNED_DATA
	//worksheet -px ? Pareto plot_pareto -d;
	worksheet -px ? ParetoRaw plot_paretoraw;
	///End ADD_PARETO_CHART_BINNED_DATA
///------ End PARETOCHART_XF_BASED_PLOTTING

/// EJP 2009-06-18 QA80-13610 PLOT_MENU_REORGANIZE
[ScatterCentral]
	run.section(,general,201 ScatterCentral 0);

[WindRoseRaw]
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	
	NumCols = wks.iscolsel();
	if( (NumCols== 2 && (wks.col$(wks.c1).type == 4 && wks.col$(wks.c2).type == 1) )
		|| (NumCols == 1 && wks.col$(wks.c1).type == 1) ) 
	{
		/// Iris 7/9/2009 CHANGE_PLOT_WINDROSE_XF_TO_PX_WAY
		//run -xf plot_windrose -d;
		worksheet -px ? WindRoseRaw plot_windrose -d;
		return 0;
		///end CHANGE_PLOT_WINDROSE_XF_TO_PX_WAY
	}
	else
	{
		type -b $general.selectionXY;
		return 1;
	}
		
/// end PLOT_MENU_REORGANIZE

/// Iris 7/27/2009 ADD_PROBABILITY_PLOT_ON_TOOLBAR_AND_MENU
[PPPlot]
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	
	NumYs = %(%H,@Y#,2);
	if (NumYs != wks.iscolsel() || NumYs!=1) 
	{
		type -b $general.Only1YColumn;
		break 1;
	} 	
	/// Iris 8/17/2009 IMPROVE_PP_PLOT
	//worksheet -px ? PP plot_prob option:=0;
	/// Iris 11/10/2009 QA81-14607-S1 SET_GRAPH_SEETINGS_BY_TEMPLATE_REPLACE_CODE
	//worksheet -px ? PP plot_prob option:=0 -d;
	// in plot_prob xf will reload template on distr type
	worksheet -px ? PPNormal plot_prob option:=0 -d;
	///end SET_GRAPH_SEETINGS_BY_TEMPLATE_REPLACE_CODE
	return 0;
	///end IMPROVE_PP_PLOT
	
[QQPlot]
	if (exist(%H,2)!=2)
	{
		type -b $general.BadGraphType;
		return 1;
	}
	
	NumYs = %(%H,@Y#,2);
	if (NumYs != wks.iscolsel() || NumYs!=1) 
	{
		type -b $general.Only1YColumn;
		break 1;
	} 
	/// Iris 8/17/2009 IMPROVE_PP_PLOT
	//worksheet -px ? QQ plot_prob option:=1;
	worksheet -px ? QQ plot_prob option:=1 -d;
	return 0;
	///end IMPROVE_PP_PLOT
	
[WeibullPlot]
	NumYs = %(%H,@Y#,2);
	if (NumYs != wks.iscolsel() || NumYs!=1) 
	{
		type -b $general.Only1YColumn;
		break 1;
	} 
	worksheet -px ? Weibull plot_weibull;
///end ADD_PROBABILITY_PLOT_ON_TOOLBAR_AND_MENU

/// Kenny 7/28/2009 QA80-13992 ADD_MENU_AND_TOOLBAR_FOR_ACCESSING_XF_PLOTMAXES
[MultipleAxes]
	/// 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;
	}
	///---
	run -xf plotmyaxes -d;
	return 0;
/// End QA80-13992 ADD_MENU_AND_TOOLBAR_FOR_ACCESSING_XF_PLOTMAXES

/// Kenny 9/15/2009 QA81-14182 XF_PLOT_BY_LABEL_ADD_MENU_ACCESSIBILITY
[PlotByLabel]
	/// 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;
	}
	///---
	run -xf plotbylabel -d;
	return 0;
/// End QA81-14182 XF_PLOT_BY_LABEL_ADD_MENU_ACCESSIBILITY