BookmarkSubscribeRSS Feed
Highlighted
dqr2

Community Trekker

Joined:

Jun 19, 2015

Modifying the "Elements" statements in the Graph Builder output by scripting

Hi,

 

I attach a script which generates a small data table with an attached JSL script called "Grid of Color-Coded Scatter Plots", which if you run it should produce a Graph Builder output showing a (2x2) grid of scatter plots in each of which all the points are given different colors and/or symbols.

 

The problem I have is that I'm trying to write a script that will allow me to exchange the variables in such a chart for a different set of columns within the same data set, and/or the number of variables being charted on either or both of the X and Y axes (e.g. the example currently shows a (2x2) grid, but I might want to change that into a (3x2) grid by adding a third X data column onto the X axis).

 

I know how to apply the "<< Add Variable" and "<< Remove Variable" properties to gb[GraphBuilderBox(1)], where gb is the Graph Builder object, to swap one set of variables in the chart for another set. What I haven't been able to work out is how to modify the corresponding Elements statements. There's an example in the Scripting Index which applies to the GraphBuilderBox display box, but the "<< Add Element" example there relates to adding a fitted line - and I can't see how to relate this to my problem, which involves adding elements with "Position" and "Points" parameters.

 

Can anyone point me at some examples of the syntax that I might be able to adapt?

Many thanks.

 

New Table( "JMP Script Demo",
	Add Rows( 4 ),
	Compress File When Saved( 1 ),
	New Script("Grid of Color-Coded Scatter Plots",
		Graph Builder(
			Size( 713, 469 ),
			Show Control Panel( 0 ),
			Show Legend( 0 ),
			Grid Color( "White" ),
			Graph Spacing( 10 ),
			Spacing Borders( 1 ),
			Variables(
				X( :X1 ),
				X( :X2 ),
				Y( :Y3A ),
				Y( :Y3B, Position( 1 ) ),
				Y( :Y3C, Position( 1 ) ),
				Y( :Y3D, Position( 1 ) ),
				Y( :Y2A ),
				Y( :Y2B, Position( 2 ) ),
				Y( :Y2C, Position( 2 ) ),
				Y( :Y2D, Position( 2 ) )
			),
			Elements( Position( 1, 1 ), Points( X, Y( 1 ), Y( 2 ), Y( 3 ), Y( 4 ), Legend( 73 ) ) ),
			Elements( Position( 1, 2 ), Points( X, Y( 1 ), Y( 2 ), Y( 3 ), Y( 4 ), Legend( 78 ) ) ),
			Elements( Position( 2, 1 ), Points( X, Y( 1 ), Y( 2 ), Y( 3 ), Y( 4 ), Legend( 77 ) ) ),
			Elements( Position( 2, 2 ), Points( X, Y( 1 ), Y( 2 ), Y( 3 ), Y( 4 ), Legend( 79 ) ) ),
			SendToReport(
				Dispatch( {}, "X1", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
				Dispatch( {}, "X2", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
				Dispatch( {}, "Y3A", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
				Dispatch( {}, "Y2A", ScaleBox, {Label Row( Show Major Grid( 1 ) )} ),
				Dispatch(
					{},
					"400",
					ScaleBox,
					{Legend Model(
						73,
						Properties( 0, {Line Color( 3 ), Marker( "Filled Diamond" )} ),
						Properties( 1, {Line Color( 36 ), Marker( "FilledCircle" )} ),
						Properties( 2, {Line Color( 5 ), Marker( "Square" )} ),
						Properties( 3, {Line Color( 8 ), Marker( "FilledCircle" )} )
					), Legend Model(
						78,
						Properties( 0, {Line Color( 3 ), Marker( "Filled Diamond" )} ),
						Properties( 1, {Line Color( 36 ), Marker( "FilledCircle" )} ),
						Properties( 2, {Line Color( 5 ), Marker( "Square" )} ),
						Properties( 3, {Line Color( 8 ), Marker( "FilledCircle" )} )
					), Legend Model(
						77,
						Base( 0, 0, 0 ),
						Base( 1, 0, 1 ),
						Base( 2, 0, 2 ),
						Base( 3, 0, 3 ),
						Properties( 0, {Line Color( 3 ), Marker( "Filled Diamond" )} ),
						Properties( 1, {Line Color( 36 ), Marker( "FilledCircle" )} ),
						Properties( 2, {Line Color( 5 ), Marker( "Square" )} ),
						Properties( 3, {Line Color( 8 ), Marker( "FilledCircle" )} )
					), Legend Model(
						79,
						Properties( 0, {Line Color( 3 ), Marker( "Filled Diamond" )} ),
						Properties( 1, {Line Color( 36 ), Marker( "FilledCircle" )} ),
						Properties( 2, {Line Color( 5 ), Marker( "Square" )} ),
						Properties( 3, {Line Color( 8 ), Marker( "FilledCircle" )} )
					)}
				),
				Dispatch( {}, "graph title", TextEditBox, {Set Text( "Grid of Color-Coded Scatter Plots" )} ),
				Dispatch( {}, "Y title", TextEditBox, {Set Text( "Y3A, Y3B, Y3C & Y3D" )} ),
				Dispatch( {}, "Y 1 title", TextEditBox, {Set Text( "Y2A, Y2B, Y2C & Y2D" )} )
			)
		)
	),
	New Column( "Sample",
		Character( 24 ),
		"Nominal",
		Set Values( {"Sample A", "Sample B", "Sample C", "Sample D"} )
	),
	New Column( "X1",
		Numeric,
		"Continuous",
		Format( "Fixed Dec", 10, 2 ),
		Set Values( [6.14, 5.14, 6.64, 6.72] )
	),
	New Column( "X2",
		Numeric,
		"Continuous",
		Format( "Fixed Dec", 10, 2 ),
		Set Values( [4.62, 4.52, 4.66, 4.84] )
	),
	New Column( "X3",
		Numeric,
		"Continuous",
		Format( "Fixed Dec", 10, 2 ),
		Set Values( [2.17, 2.52, 3.48, 2.25] )
	),
	New Column( "Y1A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [6.14, ., ., .] ) ),
	New Column( "Y1B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., 5.14, ., .] ) ),
	New Column( "Y1C", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., ., 6.64, .] ) ),
	New Column( "Y1D", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., ., ., 6.72] ) ),
	New Column( "Y2A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [4.62, ., ., .] ) ),
	New Column( "Y2B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., 4.52, ., .] ) ),
	New Column( "Y2C", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., ., 4.66, .] ) ),
	New Column( "Y2D", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., ., ., 4.84] ) ),
	New Column( "Y3A", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [2.17, ., ., .] ) ),
	New Column( "Y3B", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., 2.52, ., .] ) ),
	New Column( "Y3C", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., ., 3.48, .] ) ),
	New Column( "Y3D", Numeric, "Continuous", Format( "Best", 12 ), Set Values( [., ., ., 2.25] ) ),
	Set Row States( [8, 8, 8, 8] ),
	Set Label Columns( :Sample )
)

 

1 ACCEPTED SOLUTION

Accepted Solutions
ih

Community Trekker

Joined:

Sep 30, 2016

Solution

Re: Modifying the "Elements" statements in the Graph Builder output by scripting

I am not sure what other implications this has, but you can add and remove columns and rows without modifying the Elements directly, just add variables to new positions:

gb = Graph Builder(
	Size( 713, 469 ),
	Show Control Panel( 0 ),
	Show Legend( 0 ),
	Grid Color( "White" ),
	Graph Spacing( 10 ),
	Spacing Borders( 1 ),
	Variables(
		X( :X1 ),
		X( :X2 ),
		Y( :Y3A, Position( 1 ) ),
		Y( :Y3B, Position( 2 ) ),
	)
);

gbb = Report( gb )[Graph Builder Box( 1 )];
gbb << Add Variable( { :Y3C, Role( "Y" ), Position( 3 ) } );
gbb << Remove Variable( {:Y3A, Role( "Y" ), Position( 1 ) } )

 

Alternately, you could just replace the graph with a new one having the correct number of elements:

New Window( "Graph",
	lb = H list box( 
		gb = Graph Builder(
			Size( 713, 469 ),
			Show Control Panel( 0 ),
			Show Legend( 0 ),
			Grid Color( "White" ),
			Graph Spacing( 10 ),
			Spacing Borders( 1 ),
			Variables(
				X( :X1 ),
				X( :X2 ),
				Y( :Y3A, Position( 1 ) ),
				Y( :Y3B, Position( 2 ) ),
			)
		)
	);
);

Try( gb << Delete );
lb << Append(
	gb = Graph Builder(
		Size( 713, 469 ),
		Show Control Panel( 0 ),
		Show Legend( 0 ),
		Grid Color( "White" ),
		Graph Spacing( 10 ),
		Spacing Borders( 1 ),
		Variables(
			X( :X1 ),
			X( :X2 ),
			Y( :Y3A, Position( 1 ) ),
			Y( :Y3C, Position( 2 ) ),
		)
	)
);
3 REPLIES
ih

Community Trekker

Joined:

Sep 30, 2016

Solution

Re: Modifying the "Elements" statements in the Graph Builder output by scripting

I am not sure what other implications this has, but you can add and remove columns and rows without modifying the Elements directly, just add variables to new positions:

gb = Graph Builder(
	Size( 713, 469 ),
	Show Control Panel( 0 ),
	Show Legend( 0 ),
	Grid Color( "White" ),
	Graph Spacing( 10 ),
	Spacing Borders( 1 ),
	Variables(
		X( :X1 ),
		X( :X2 ),
		Y( :Y3A, Position( 1 ) ),
		Y( :Y3B, Position( 2 ) ),
	)
);

gbb = Report( gb )[Graph Builder Box( 1 )];
gbb << Add Variable( { :Y3C, Role( "Y" ), Position( 3 ) } );
gbb << Remove Variable( {:Y3A, Role( "Y" ), Position( 1 ) } )

 

Alternately, you could just replace the graph with a new one having the correct number of elements:

New Window( "Graph",
	lb = H list box( 
		gb = Graph Builder(
			Size( 713, 469 ),
			Show Control Panel( 0 ),
			Show Legend( 0 ),
			Grid Color( "White" ),
			Graph Spacing( 10 ),
			Spacing Borders( 1 ),
			Variables(
				X( :X1 ),
				X( :X2 ),
				Y( :Y3A, Position( 1 ) ),
				Y( :Y3B, Position( 2 ) ),
			)
		)
	);
);

Try( gb << Delete );
lb << Append(
	gb = Graph Builder(
		Size( 713, 469 ),
		Show Control Panel( 0 ),
		Show Legend( 0 ),
		Grid Color( "White" ),
		Graph Spacing( 10 ),
		Spacing Borders( 1 ),
		Variables(
			X( :X1 ),
			X( :X2 ),
			Y( :Y3A, Position( 1 ) ),
			Y( :Y3C, Position( 2 ) ),
		)
	)
);
dqr2

Community Trekker

Joined:

Jun 19, 2015

Re: Modifying the "Elements" statements in the Graph Builder output by scripting

Hi,

Yes - I've discovered myself while experimenting that adding and/or removing columns seems to impact indirectly on the "Elements" statement, in that when I inspect the Graph Builder script after having made some such changes, the "Elements" statement appears to have been automatically (and apparently correctly) updated.

However, I found that I was subsequently unable to apply the necessary "<< Send to Report( Dispatch( ... ) )" commands to the Graph Builder that would change the colors and symbols to the ones I actually wanted. They just didn't work: I don't know why not (and indeed, I'm not yet 100% certain that I didn't make any errors when I was trying to do that), and this was partly what prompted me first to try to find some examples of how the Elements should be specified.

To your second point, I considered simply deleting the graph and replacing it - and I may yet fall back on that option if I can't resolve it any other way. The main reason I didn't particularly want to go down that route was that the Graph Builder output is incorporated into a customized dashboard of my own, and if I then start deleting and replacing components of that dashboard the scripting could potentially get a bit messy.  Having said that, your example suggests that it might be less messy than I'd previously thought.

Many thanks for your help and suggestions; if I do manage to sort out the "Element" statements I'll post an update of my example to show how it was done.

ih

Community Trekker

Joined:

Sep 30, 2016

Re: Modifying the "Elements" statements in the Graph Builder output by scripting

I use the 'replace' option frequently.  Here is how I normally deploy it, notice how the script to create the graph only appears once:

Names default to here( 1 );

dt = Open( "$SAMPLE_DATA/iris.jmp" );

UpdateGraph = Function( { x_low },
	Try( gb << Delete );
	
	lb << Append(
		gb = Graph Builder(
			Size( 500, 500 ),
			Show Control Panel( 0 ),
			Variables( X( :Sepal length ), Y( :Petal length ) ),
			Elements( Points( X, Y, Legend( 3 ) ) ),
			SendToReport(
				Dispatch(
					{},
					"Sepal length",
					ScaleBox,
					{Min( x_low ), Max( x_low + 4 ), Inc( 0.5 ),
					Minor Ticks( 0 )}
				)
			)
		)
	)
);

win = New Window( "Swapping Graphs",
	V List Box(
		Button Box("Swap Graph", UpdateGraph( random uniform( 2, 6 ) ), << Padding( 10 ) ),
		lb = H List Box()
	);
);

UpdateGraph( 4 );