%B=%(%H,@Link,2)/* the link dataset name */

CheckVar qc.gsize 5/* see if qc.gsize is defined, if not set it to 5 */
CheckVar qc.Ksigma 3

col(1)=ave(%B,qc.gsize)
	/* the ave() also produce the 
	 * following data sets
	 */
col(2)=_Range
col(3)=_SD
	/* Datasets begin with a 
	 * _ means that they are temperary
	 * and will be deleted automatically
	 */
sum(col(1))
Xbar = sum.mean
sum(col(2))
Rbar = sum.mean

%B=%H
	/* we need to save the worksheet name since
	 * the next step brings another window into
	 * activation
	 */
win -B P.%H XbarR
	/* find the plot window named P.%H and opens
	 * it from the XbarR template if not already
	 * there.
	 */

layer -o 2 {
	layer -w %B 1 0 1 0 202;
	/* 
	 * 202 is line/symbol graph
	 */
	 Sigma=Rbar/(QCd2(qc.gsize) * sqrt(qc.gsize));
	 y1 = Xbar - (qc.Ksigma+1) * Sigma * 1.2;
	 y2 = Xbar + (qc.Ksigma+1) * Sigma * 1.2;
};

layer -o 1 {
	/* layer 1 is the bottom layer in this template */
	layer -i203 %B_R;
		/* column graph */
	y1=0;x1=0;x2= 1 + sum.N;
	UCL=QCD4(qc.gsize)*Rbar;
	LCL=QCD3(qc.gsize)*Rbar;
	y2 = 1.5*UCL;
	/* the QCLines macro assume UCL and LCL */
	QCLines Rbar;
};

/* we delay drwaing the labels because
 * we want to have the correct position
 * set by layer 1
 */
layer -o 2 {
	UCL=Xbar + qc.Ksigma * Sigma;
	LCL=Xbar - qc.Ksigma * Sigma;
	QCLines Xbar;
};

