cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
asj
asj
Level II

Adding annotations programmatically to an existing plot with JSL

Hi all, 
I'm looking to add some metadata "fine print" through JSL to my plot after I have made the plot in the Graph Builder.
Below are two examples of what I'd like to get done with JSL: Metadata 1 text field is in the plot area, Metadata 2 text field is outside it at the bottom of the graph. In the example plot I just added the text fields by the annotate tool in the GUI. However, I have hard time figuring out how to achieve this in JSL.
This is my first post here - thank you in advance for any help!

asj_0-1709629862836.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Adding annotations programmatically to an existing plot with JSL

Here is some hardwired code that does what you specified

txnelson_0-1709635604883.png

Names Default To Here( 1 );
dt = 
 // Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

gb = dt << Graph Builder(
	Size( 528, 456 ),
	Show Control Panel( 0 ),
	Variables( X( :weight ), Y( :height ) ),
	Elements( Points( X, Y, Legend( 3 ) ), Smoother( X, Y, Legend( 4 ) ) )
);

// To add text within the graph one way is to use "Add Graphics Script" 
// to the graph
Report( gb )[Outline Box( 1 ), framebox( 1 )] <<
add graphics script(
	xMin = Report( gb )[axisbox( 1 )] << get min;
	yMin = Report( gb )[axisbox( 2 )] << get min;
	Text Color( "red" );
	Text(
		Right Justified,
		{xMin + 46, yMin + 1},
		"Metadata1 " || Format( Today(), "yyyymmdd" ) || "/OwnerName"
	);
	Text( Right Justified, {xMin + 32, yMin + .1}, "path c:tmp/graph-01.jrp" );
);

// Add Outside of the graph
Report( gb )[Outline Box( 1 )] << Append(
	V List Box(
		Text Box( "Metadata1 " || Format( Today(), "yyyymmdd" ) || "/OwnerName" ),
		Text Box( "path c:tmp/graph-01.jrp" )
	)
);
Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: Adding annotations programmatically to an existing plot with JSL

Here is some hardwired code that does what you specified

txnelson_0-1709635604883.png

Names Default To Here( 1 );
dt = 
 // Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

gb = dt << Graph Builder(
	Size( 528, 456 ),
	Show Control Panel( 0 ),
	Variables( X( :weight ), Y( :height ) ),
	Elements( Points( X, Y, Legend( 3 ) ), Smoother( X, Y, Legend( 4 ) ) )
);

// To add text within the graph one way is to use "Add Graphics Script" 
// to the graph
Report( gb )[Outline Box( 1 ), framebox( 1 )] <<
add graphics script(
	xMin = Report( gb )[axisbox( 1 )] << get min;
	yMin = Report( gb )[axisbox( 2 )] << get min;
	Text Color( "red" );
	Text(
		Right Justified,
		{xMin + 46, yMin + 1},
		"Metadata1 " || Format( Today(), "yyyymmdd" ) || "/OwnerName"
	);
	Text( Right Justified, {xMin + 32, yMin + .1}, "path c:tmp/graph-01.jrp" );
);

// Add Outside of the graph
Report( gb )[Outline Box( 1 )] << Append(
	V List Box(
		Text Box( "Metadata1 " || Format( Today(), "yyyymmdd" ) || "/OwnerName" ),
		Text Box( "path c:tmp/graph-01.jrp" )
	)
);
Jim
asj
asj
Level II

Re: Adding annotations programmatically to an existing plot with JSL

Thank you, Jim, this was fast!

This almost takes me to the goalpost. I am now able to generate the texts like I want.
However, if a save a graph made in this way as a script I get something like this:

Graph Builder(
	Size( 528, 456 ),
	Show Control Panel( 0 ),
	Variables( X( :weight ), Y( :height ) ),
	Elements( Points( X, Y, Legend( 3 ) ), Smoother( X, Y, Legend( 4 ) ) ),
	SendToReport(
		Dispatch(
			{},
			"Graph Builder",
			FrameBox,
			{Add Graphics Script(
				5,
				Description( "" ),
				xMin = Report( gb )[axisbox( 1 )] << get min;
				yMin = Report( gb )[axisbox( 2 )] << get min;
				Text Color( "red" );
				Text(
					Right Justified,
					{xMin + 46, yMin + 1},
					"Metadata1 " || Format( Today(), "yyyymmdd" ) || "/OwnerName"
				);
				Text(
					Right Justified,
					{xMin + 32, yMin + 0.1},
					"path c:tmp/graph-01.jrp"
				);
			)}
		)
	)
)

and if i try to run the script I get an error
>object not subscriptable in access or evaluation of 'Report(gb)[ /*###*/axisbox(1)]' , Report( gb )[/*###*/axisbox( 1 )]
which i think makes sense, since the "gb" is not defined within the script. Is there any way around this problem?

txnelson
Super User

Re: Adding annotations programmatically to an existing plot with JSL

You are missing the variable "gb" which  points to the Graph Builder instance

gb = Graph Builder(
Jim
asj
asj
Level II

Re: Adding annotations programmatically to an existing plot with JSL

Well this is true, but this is how the script is saved by JMP when I for example use the "save script ... to data table".
However I found a solution using Eval and EvalExpr from here:
https://community.jmp.com/t5/Discussions/GraphBuilder-gt-FrameBox-lt-lt-Add-Graphics-Script-V-Line-x...
this way I got it all to work:

Names Default To Here( 1 );
dt = 
 // Open Data Table: Big Class.jmp
// → Data Table( "Big Class" )
Open( "$SAMPLE_DATA/Big Class.jmp" );

gb = dt << Graph Builder(
	Size( 528, 456 ),
	Show Control Panel( 0 ),
	Variables( X( :weight ), Y( :height ) ),
	Elements( Points( X, Y, Legend( 3 ) ), Smoother( X, Y, Legend( 4 ) ) )
);

yMin = Report( gb )[axisbox( 1 )] << get min;
yMin = Report( gb )[axisbox( 2 )] << get min;

//I need the values "hard-coded"" in the new graphics script to be added => hence i
//need to use the Eval(EvalExpr( .. )) trick from
//https://community.jmp.com/t5/Discussions/GraphBuilder-gt-FrameBox-lt-lt-Add-Graphics-Script-V-Line-x-y1/td-p/366991
Eval(EvalExpr(
Report( gb )[Outline Box( 1 ), framebox( 1 )] <<
add graphics script(
	Text Color( "red" );
	Text( {expr(xMin),expr(yMin)}, "path c:tmp/graph-01.jrp" );
)
));
jthi
Super User

Re: Adding annotations programmatically to an existing plot with JSL

You can also make slight modification to Jim's script and use X Origin and Y Origin to set the location

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");

gb = dt << Graph Builder(
	Size(528, 456),
	Show Control Panel(0),
	Variables(X(:weight), Y(:height)),
	Elements(Points(X, Y, Legend(3)), Smoother(X, Y, Legend(4)))
);

Report(gb)[FrameBox(1)] << Add Graphics Script(
	Text Color("Red");
	Text({X Origin(), Y Origin()},
		"Metadata1 " || Format(Today(), "yyyymmdd") || "/OwnerName\!Npath c:tmp/graph-01.jrp"
	);
);
-Jarmo