BookmarkSubscribe
Choose Language Hide Translation Bar
Highlighted
Aziza
Community Trekker

Avoid repetitions in the script, Graph Builder in particular

Hi, 

 

I have a script to reproduce Graph Builder plots. Three script files, each should reproduce graph builder outputs for 15 different variables. Basically, I used the Graph Builder to plot the data the way I want, saved the script behind it and repeated it for the next 14 variables. As the result, I have smth like this:

//the first script file
dt=currentdatatable();
obj1=dt<<GraphBuilder();
obj2=dt<<GraphBuilder();
...
obj15=dt<<GraphBuilder();
// Concern: repeation of big parts in each objects's code

I want to keep the Graph Bulder features settings for all objects (15 in total), but avoid repeatitions within the code (some examples below). Is there any way I can define such parts of the code at the begining of the script (or in a seperate file (because I have other script files using the same parts of the code) and call it while running the code) and use those as the variables within the code to make the code to look simpler, shorter? This way is efficient also if I want to make some changes. The other question: any suggestions on how I could improve my scripting skills? 

 

Thank you very much.

 

Examples:

Dispatch(	{},
			"400",
			LegendBox,
			{Set Title( "" ), font( "Segoe UI", 11, "Plain" ),
			Legend Position(
				{8, [1, 5, 9, 3, 6, 11], 9, [0, 4, 8], 10, [2, 7, 10]}
			), Position( {1, 5, 9, 3, 6, 11, 0, 4, 8, 2, 7, 10} )}
		)

or

Dispatch(
			{},
			"400",
			ScaleBox,
			{Legend Model(
				8,
				Level Name( 0, "Variable 3 Averages", Item ID( "3", 1 ) ),
				Level Name( 1, "Variable 5 Averages", Item ID( "5", 1 ) ),
				Level Name( 2, "Variable 6 Averages", Item ID( "6", 1 ) ),
				Level Name( 3, "Variable 3 Standard Deviation", Item ID( "3", 2 ) ),
				Level Name( 4, "Variable 5 Standard Deviation", Item ID( "5", 2 ) ),
				Level Name( 5, "Variable 6 Standard Deviation", Item ID( "6", 2 ) ),
				Base( 0, 0, 0, Item ID( "3", 1 ) ),
				Base( 1, 0, 1, Item ID( "5", 1 ) ),
				Base( 2, 0, 2, Item ID( "6", 1 ) ),
				Base( 3, 0, 0, Item ID( "3", 2 ) ),
				Base( 4, 0, 0, Item ID( "5", 2 ) ),
				Base( 5, 0, 0, Item ID( "6", 2 ) ),
				Properties(
					0,
					{Line Color( 53 ), Line Width( 4 ), Marker( "FilledCircle" ),
					Marker Size( 6 ), gradient(
						{Scale Values( [0 1] ), Width( 9 ), N Labels( 6 )}
					)},
					Item ID( "3", 1 )
				),
				Properties(
					1,
					{Line Color( 56 ), Line Width( 4 ), Marker( "FilledCircle" ),
					Marker Size( 6 ), gradient(
						{Scale Values( [0 1] ), Width( 9 ), N Labels( 6 )}
					)},
					Item ID( "5", 1 )
				),
				Properties(
					2,
					{Line Color( 38 ), Line Width( 4 ), Marker( "FilledCircle" ),
					Marker Size( 6 ), gradient(
						{Scale Values( [0 1] ), Width( 9 ), N Labels( 6 )}
					)},
					Item ID( "6", 1 )
				),
				Properties(
					3,
					{Line Color( 53 ), Line Width( 4 )},
					Item ID( "3", 2 )
				),
				Properties(
					4,
					{Line Color( 56 ), Line Width( 4 )},
					Item ID( "5", 2 )
				),
				Properties(
					5,
					{Line Color( 38 ), Line Width( 4 )},
					Item ID( "6", 2 )
				),
				Properties(
					-1,
					{Line Color( 13 ), Line Width( 4 ), Marker( "FilledCircle" ),
					Marker Size( 6 ), gradient(
						{Scale Values( [0 1] ), Width( 9 ), N Labels( 6 )}
					)},
					Item ID( "4", 1 )
				),
				Properties(
					-1,
					{Line Color( 13 ), Line Width( 4 )},
					Item ID( "4", 2 )
				),
				Base( -1, 0, 1, Item ID( "5", 1 ) ),
				Level Name( -1, "Average", Item ID( "Mean", 1 ) ),
				Base( -1, 0, 0, Item ID( "Standard Deviation", 1 ) ),
				Properties(
					-1,
					{Line Color( 3 ), Marker( "FilledCircle" ), Marker Size( 6 ),
					gradient( {Scale Values( [0 1] ), Width( 9 ), N Labels( 6 )} )},
					Item ID( "Mean", 1 )
				),
				Properties(
					-1,
					{Line Color( 1 ), Line Width( 4 )},
					Item ID( "Standard Deviation", 1 )
				),
				Base( -1, 0, 0, Item ID( "95% confidence interval", 1 ) ),
				Properties(
					-1,
					{Line Color( 32 ), Line Width( 2 ),
					gradient( {Scale Values( [0 1] ), Width( 9 ), N Labels( 6 )} )},
					Item ID( "95% confidence interval", 1 )
				)
			), Legend Model(
				9,
				Level Name( 0, "Variable 3 Individual Values", Item ID( "3", 1 ) ),
				Level Name( 1, "Variable 5 Individual Values", Item ID( "5", 1 ) ),
				Level Name( 2, "Variable 6 Individual Values", Item ID( "6", 1 ) ),
				Base( 0, 0, 0, Item ID( "3", 1 ) ),
				Base( 1, 0, 1, Item ID( "5", 1 ) ),
				Base( 2, 0, 2, Item ID( "6", 1 ) ),
				Properties(
					0,
					{Line Color( 37 ), Line Width( 4 )},
					Item ID( "3", 1 )
				),
				Properties(
					1,
					{Line Color( 40 ), Line Width( 4 )},
					Item ID( "5", 1 )
				),
				Properties(
					2,
					{Line Color( 38 ), Line Width( 4 )},
					Item ID( "6", 1 )
				),
				Level Name( -1, "Variable 4 Individual Values", Item ID( "4", 1 ) ),
				Base( -1, 0, 0, Item ID( "4", 1 ) ),
				Properties(
					-1,
					{Line Color( 42 ), Line Width( 4 )},
					Item ID( "4", 1 )
				),
				Level Name( -1, "Individual Values", Item ID( "Main-Variable", 1 ) ),
				Properties( -1, {Line Color( 5 )}, Item ID( "Main-Variable", 1 ) ),
				Properties(
					-1,
					{Line Color( 5 ), gradient(
						{Scale Values( [0 1] ), Width( 9 ), N Labels( 6 )}
					)},
					Item ID( "Main-Variable", 1 )
				)
			), Legend Model(
				10,
				Level Name( 0, "Variable 3 Smooth", Item ID( "3", 1 ) ),
				Level Name( 1, "Variable 5 Smooth", Item ID( "5", 1 ) ),
				Level Name( 2, "Variable 6 Smooth", Item ID( "6", 1 ) ),
				Properties(
					0,
					{Line Color( 53 ), Line Width( 4 )},
					Item ID( "3", 1 )
				),
				Properties(
					1,
					{Line Color( 56 ), Line Width( 4 )},
					Item ID( "5", 1 )
				),
				Properties(
					2,
					{Line Color( 38 ), Line Width( 4 )},
					Item ID( "6", 1 )
				),
				Level Name( -1, "Variable 4 Smooth", Item ID( "4", 1 ) ),
				Properties(
					-1,
					{Line Color( 13 ), Line Width( 4 )},
					Item ID( "4", 1 )
				),
				Properties( -1, {Line Width( 4 )}, Item ID( "Smooth", 1 ) )
			)}
		)
0 Kudos
2 REPLIES 2
phil_kay
Staff

Re: Avoid repetitions in the script, Graph Builder in particular

Being able to write your own functions might be useful for you. See this from @Craige_Hales in the JSL Cookbook:

https://community.jmp.com/t5/JSL-Cookbook/Write-Your-Own-Functions/ta-p/49052

 

pmroz
Super User

Re: Avoid repetitions in the script, Graph Builder in particular

This is one approach that might help:

Put common code into separate strings.  If there are a lot of double quotes use the \[ and ]\ constructs for readability.

Assemble the graph builder code (A || B || C etc.), and execute using eval(parse()).

 

Here's a simple example with one common string for two graphs.

dt = open("$sample_data\Airline Delays.jmp");

common_str = 
"dt << Graph Builder(
	Show Control Panel( 0 ),
	Variables( X( :Elapsed Time ), Y( :Distance ), Overlay( :Airline ) ),";

gb1_str = evalinsert(
"gb1 = ^common_str^
	Elements( Area( X, Y, Legend( 21 ) ) )
)");

gb2_str = evalinsert(
"gb2 = ^common_str^
	Elements( Points( X, Y, Legend( 11 ) ) )
)");

eval(parse(gb1_str));
eval(parse(gb2_str));