cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
MFVIT
Level IV

Adding a picture as a background of a graph

Dear All,
I wrote the following script. After opening a data table, it takes the data and creates a graph by using the graph builder. Then it loads a picture which has the same name as the opened data table and use that picture as a background for the graph.
In order to make the script work, I had to add to the data table name the file extension .png as part of the name. So I had to name both the picture and the data table by adding .png in their name (the picture file has the extension .png).
For example, if the name of the picture is sample.png, I had to change it into sample.png.png.
 
Of course I do not like this but if I do not make this change jmp says that it can’t find the picture file.
I am sure the problem can be solved in a cleaner way.
Thanks a lot for any help.
 

Names Default To Here(1);
dt=Current Data Table();
gb = dt <<
Graph Builder(
       Variables( X( :x ), Y( :y ), Overlay( :track ) ),
       Elements( Points( X, Y, Legend( 5 ) ) ),
       SendToReport(
              Dispatch(
                     {},
                     "x",
                     ScaleBox,
                     {Min( 0 ), Max( 280 ), Inc( 50 ), Minor Ticks( 1 )}
              ),
              Dispatch(
                     {},
                     "y",
                     ScaleBox,
                     {Min( 0 ), Max( 265 ), Inc( 50 ), Minor Ticks( 1 )}
              ),
              Dispatch( {}, "Graph Builder", FrameBox, {Transparency( 0.05 )} )
       )
);
frame = (gb << report)[FrameBox( 1 )];
 n = dt << Get Name();
 img = newImage(n);
 img<<Rotate(270);
 frame << Add Image( image(img),bounds( top( 265 ), Left( 0 ), bottom( 0 ), Right( 280 ) ))

 

5 REPLIES 5
Thierry_S
Super User

Re: Adding a picture as a background of a graph

Hi,
The issue is that the variable "n" is a string, not an image even after calling the New Image. I have not found a way to convert directly a string into an image but you can create a dummy txtb = Text Box (n), and then get the picture of that object txtimg = tctb << Get Picture, and finally use the txtimg variable as you background.
I hope it helps. Others may have more elegant ways to tackle this.
Best,
TS
Thierry R. Sornasse
MFVIT
Level IV

Re: Adding a picture as a background of a graph

Many thanks!
Jeff_Perkinson
Community Manager Community Manager

Re: Adding a picture as a background of a graph

On Windows, the Get Name() message to a data table returns only the table name, without the ".jmp" appended.

 

Names Default To Here( 1 );
dt = Open( "$DESKTOP/Big Class.jmp" );
Show( dt << get name() );

Log:

//:*/
Names Default To Here( 1 );
dt = Open( "$DESKTOP/Big Class.jmp" );
Show( dt << get name() );
/*:

dt << get name() = "Big Class";

So, you'll need to append the ".png" to get a complete filename for New Image().

 

Here's an example using Big Class (copied to your desktop) and the attached Big Class.png file here.

 

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


gb = dt << Graph Builder(
	Variables( X( :weight ), Y( :height ), Overlay( :sex ) ),
	Elements( Points( X, Y, Legend( 5 ) ), Smoother( X, Y, Legend( 6 ) ) ),
	SendToReport( Dispatch( {}, "Graph Builder", FrameBox, {Transparency( 0.5 )} ) )
);
frame = (gb << report)[FrameBox( 1 )];
n = dt << Get Name();
img = New Image( "$DESKTOP/" || n || ".png" );
frame << Add Image(
	image( img ),
	Bounds(
		Left( 61.2587412587413 ),
		Right( 179.370629370629 ),
		Top( 70.8292080975384 ),
		Bottom( 48.5033161162993 )
	),
	SetSize( {799, 620} )

);

 

NB: On the Mac, <<Get Name() returns the whole filename, including the .jmp. Therefore, on the Mac you'll need to use Substitute Into() to replace the ".jmp" with ".png".

 

Names Default To Here( 1 );
dt = Open( "~/Desktop/Big Class.jmp" );
gb = dt << Graph Builder(
	Variables( X( :weight ), Y( :height ), Overlay( :sex ) ),
	Elements( Points( X, Y, Legend( 5 ) ), Smoother( X, Y, Legend( 6 ) ) ),
	SendToReport( Dispatch( {}, "Graph Builder", FrameBox, {Transparency( 0.5 )} ) )
);
frame = (gb << report)[FrameBox( 1 )];
n = dt << Get Name();
Substitute Into( n, ".jmp", ".png" );
img = New Image( "~/Desktop/" || n );
 //img<<Rotate(270);
frame << Add Image(
	image( img ),
	Bounds( Left( 61.2587412587413 ), Right( 179.370629370629 ), Top( 70.8292080975384 ), Bottom( 48.5033161162993 ) ),
	SetSize( {799, 620} )

);

 

-Jeff
MFVIT
Level IV

Re: Adding a picture as a background of a graph

Many thanks, this worked and I solved my problem.

Do you know how I can get rid of the file extension in a text string? I have a script that reads the names of the picture files in a folder and put their names in a vector. Each name contains also the extension. Is there a simple command to eliminate the last four letters of a text string?

Many thanks,

Marcello.

jthi
Super User

Re: Adding a picture as a background of a graph

Maybe one of these will work for you:

filename = "testname.html";
Substr(filename, 1, Contains(filename, ".")-1);
Left(filename, Length(fileName) - 5);
Word(1, filename, ".");

Check Scripting Index for more information what the functions do

-Jarmo