Choose Language Hide Translation Bar
Highlighted
Thierry_S
Level VI

GraphBuilder Loop Script Problem: Added Text Position Changes With Each Iteration (see Script below)

Hi JMP Community,

I put together a relatively simple script where I add dynamically some text for which I adjust the Y position based on the scale of the graph (see below and attached data example). Oddly, when I run through the loop, the Y position of the added text changes with each iteration. Hence, I end up with the added text Y position set for the last graph in the series which is not desirable. Is there a trick to "lock" the added text Y position for each graphs?

 

Names Default to Here (1);

dt = current datatable ();

CoList = dt << get column names (string);

try( close( dt1, nosave ) );
((dt << Tabulate(invisible,
						Add Table(
							Column Table( Grouping Columns( :VISIT ) ),
							Row Table( Grouping Columns( :SHORT TAG ) )
						)
						)) << Make Into Data Table) << Set Name ("TEMP");
						
dt1 = data table ("TEMP");
						
CCount = column (dt1, "BL");

Data Table (dt);

LMin = -1.5;
LMax = 0.5;

For (i= 5, i<=8, i++, 
	
	LMin = Col Mean (Column (i)) -1;
	LMax = Col Mean (Column (i)) +1;
	POS_Y = LMin + 0.25;
		
	gb = Expr (Graph Builder(
						Size( 1061, 524 ),
						Lock Scales( 1 ),
						Variables(
							X( :Name( "DURATION (Week)" ) ),
							Y( column (i) ),
							Group X( :LEAD TAG ),
							Overlay( :SHORT TAG )
						),
						Elements(
							Line( X, Y, Legend( 4 ), Smoothness( 0.5 ) ),
							Points(
								X,
								Y,
								Legend( 5 ),
								Summary Statistic( "Mean" ),
								Error Bars( "Standard Error" ),
								Jitter( "None" ),
								Jitter Limit( 0 )
							)
						),
						SendToReport(
							Dispatch(
								{},
								"DURATION (Week)",
								ScaleBox,
								{Format( "Fixed Dec", 12, 0 ), Min( -1 ), Max( 27 ), Inc( 2 ),
								Minor Ticks( 1 ), Add Ref Line( 14, "Solid", "Black", "", 2 )}
							),
							Dispatch(
								{},
								_TXT_,
								ScaleBox,
								{Format( "Fixed Dec", 12, 1 ), Min( LMin ), Max( LMax ), Inc( 0.5 ),
								Minor Ticks( 1 ), Add Ref Line( 0, "Dotted", "Black", "", 2 ),
								Label Row( {Set Font Size( 12 ), Set Font Style( "Plain" )} )}
							),
							Dispatch(
								{},
								"",
								ScaleBox,
								{Format( "Fixed Dec", 12, 2 ), Min( -0.5 ), Max( 0.5 ), Inc( 0.25 ),
								Minor Ticks( 0 )}
							),
							Dispatch(
								{},
								"X title",
								TextEditBox,
								{Set Font Size( 12 ), Set Font Style( "Bold" )}
							),
							Dispatch(
								{},
								"Y title",
								TextEditBox,
								{Set Font Size( 12 ), Set Font Style( "Bold" )}
							),
							Dispatch(
								{},
								"Graph Builder",
								FrameBox,
								{Marker Size( 6 ), Left( 1 ), Right( 1 ), Top( 0 ), Bottom( 1 ),
								Add Graphics Script(
									2,
									Description( "Script" ),
									Text Size (10);
									Text Color ("Dark Red");
									Text( Center Justified, {21, POS_Y}, N1 );
									Text Color ("Light Red");
									Text( Center Justified, {21, POS_Y-0.1}, N2 );
								), Grid Line Order( 1 ), Reference Line Order( 3 )}
							),
							Dispatch(
								{},
								"Graph Builder",
								FrameBox( 2 ),
								{Marker Size( 6 ), Left( 0 ), Right( 1 ), Top( 0 ), Bottom( 1 ),
								Add Graphics Script(
									2,
									Description( "Script" ),
									Text Size (10);
									Text Color ("Dark Green");
									Text( Center Justified, {21, POS_Y}, N3 );
									Text Color ("Light Green");
									Text( Center Justified, {21, POS_Y-0.1}, N4 );
								), Grid Line Order( 1 ), Reference Line Order( 3 )}
							),
							Dispatch(
								{},
								"Graph Builder",
								FrameBox( 3 ),
								{Marker Size( 6 ), Add Graphics Script(
									2,
									Description( "Script" ),
									Text Size (10);
									Text Color ("Dark Blue");
									Text( Center Justified, {21, POS_Y}, N5 );
									Text Color ("Light Blue");
									Text( Center Justified, {21, POS_Y-0.1}, N6 );
								), Grid Line Order( 1 ), Reference Line Order( 3 )}
							)
						)
					)
				);
		gbe = Eval (Substitute (Name Expr (gb), Expr (_TXT_), CoList [i],
							Expr (N1), 	"UPA > UPA (n=" || char(CCount [1]) || ")",
							Expr (N2), 	"UPA > ADA (n=" || char(CCount [2]) || ")", 
							Expr (N3), 	"ADA > ADA (n=" || char(CCount [3]) || ")", 
							Expr (N4), 	"ADA > UPA (n=" || char(CCount [4]) || ")", 
							Expr (N5), 	"PBO > PBO (n=" || char(CCount [5]) || ")", 
							Expr (N6), 	"PBO > UPA (n=" || char(CCount [6]) || ")"  
							));	
		gbe << journal;
		gbe << close window;
);

Close (dt1, NoSave); 
Thierry R. Sornasse
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
gzmorgan0
Super User

Re: GraphBuilder Loop Script Problem: Added Text Position Changes With Each Iteration (see Script below)

Hi @Thierry_S,

 

I was looking over unanswered questions and recognized a few issues with your script. I've made some edits in the attached script. Two are important, others optional.

  • Line 19 replaced Data Table(dt) with Current Data Table(dt) to make dt current.
  • Use syntax Column(dt,i) instead of Column(i) this is not necessary if data table dt is current.  However, I like to use this syntax, because it is more robust and makes the script easier to interpret when multiple tables are being used for the analyses. ex: line 26.
  • Line 26 used statistical function Summarize() instead of Col Mean(). Lines 26-27. Col Mean() finds the average for the entire set of data. Summarize as specified will find the mean (xb) the std dev (sd) and count (nc) for each combination of your grouping and overlay (:SHORT TAG) and time period (Column("DURATION (Week)") ).  So xb, sd, nc are vectors of size 24 (=2*3*4).  The script computes the lowest mean - one std error. Summarize allows for the fact that the mean of the entire group minus 1 might not capture the lowest error bar.  See lines 28-31. I kept a simpler version of just finding the min -1 and max+1 over the 24 individual groups. 
  • Line 35, Eval Expr() is used instead of Expr(). This is important otherwise the text will move.  Note also that Expr() is a wrapper for  POS_Y in the Add Graphics Script() portion of the Graph Builder specification.  ex: 96-105, 111-120 , 126-135.  Eval Expr() is a function that returns a copy of the embedded expression where each Expr(x)  is replaced with the value of x, the argument. So i is replace with 5, or 6 or 7.  This might seem inconsistent, because this is not needed for the axis specification that uses variables LMax and LMin.  The framebox of a graph is special. When a variable is specified, the variable is maintained in the graphics script. This is on purpose to allow a dynamic view of the graph. So in each loop the values for POS_Y are changed and the first graph's framebox variable value is changed to be the the last calculated value.  There is a function Journal( "Freeze All") but it works differently in different versions of JMP.  I find it easier to  replace varaibles with values. By replacing the POS_Y with is value and not a variable, the position of the text will not change with each loop.
  • Note your For() loop stopping criteria should be <8 or <=7. Line 29.
  • At line 139, I split the Eval ( Substitute() ) nested functions into two, separate steps so you might see the value of xp with a show statement. 

 

I hope this helps.

  

View solution in original post

1 REPLY 1
Highlighted
gzmorgan0
Super User

Re: GraphBuilder Loop Script Problem: Added Text Position Changes With Each Iteration (see Script below)

Hi @Thierry_S,

 

I was looking over unanswered questions and recognized a few issues with your script. I've made some edits in the attached script. Two are important, others optional.

  • Line 19 replaced Data Table(dt) with Current Data Table(dt) to make dt current.
  • Use syntax Column(dt,i) instead of Column(i) this is not necessary if data table dt is current.  However, I like to use this syntax, because it is more robust and makes the script easier to interpret when multiple tables are being used for the analyses. ex: line 26.
  • Line 26 used statistical function Summarize() instead of Col Mean(). Lines 26-27. Col Mean() finds the average for the entire set of data. Summarize as specified will find the mean (xb) the std dev (sd) and count (nc) for each combination of your grouping and overlay (:SHORT TAG) and time period (Column("DURATION (Week)") ).  So xb, sd, nc are vectors of size 24 (=2*3*4).  The script computes the lowest mean - one std error. Summarize allows for the fact that the mean of the entire group minus 1 might not capture the lowest error bar.  See lines 28-31. I kept a simpler version of just finding the min -1 and max+1 over the 24 individual groups. 
  • Line 35, Eval Expr() is used instead of Expr(). This is important otherwise the text will move.  Note also that Expr() is a wrapper for  POS_Y in the Add Graphics Script() portion of the Graph Builder specification.  ex: 96-105, 111-120 , 126-135.  Eval Expr() is a function that returns a copy of the embedded expression where each Expr(x)  is replaced with the value of x, the argument. So i is replace with 5, or 6 or 7.  This might seem inconsistent, because this is not needed for the axis specification that uses variables LMax and LMin.  The framebox of a graph is special. When a variable is specified, the variable is maintained in the graphics script. This is on purpose to allow a dynamic view of the graph. So in each loop the values for POS_Y are changed and the first graph's framebox variable value is changed to be the the last calculated value.  There is a function Journal( "Freeze All") but it works differently in different versions of JMP.  I find it easier to  replace varaibles with values. By replacing the POS_Y with is value and not a variable, the position of the text will not change with each loop.
  • Note your For() loop stopping criteria should be <8 or <=7. Line 29.
  • At line 139, I split the Eval ( Substitute() ) nested functions into two, separate steps so you might see the value of xp with a show statement. 

 

I hope this helps.

  

View solution in original post

Article Labels

    There are no labels assigned to this post.