cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
vishwasanj
Level V

saving graphs as PNG or JPEG

Hi,

 

I have made a script which lets the user select an excel file, process it , add or formulates columsn to it, opens graph builder and plots it with local data filter.

My question is:

 

1. How do I save graphs choosing the local data filter option using the script? For example, I have 2 local data filters, say Trimset(10, 100, 101, 110, 111, 1000, 11111) and formula(yes or no) and I need to plot graphs like (All trimset values with formula NO) and (Each of the trimset values with formula YES) .. so a total of 8 graphs.

2. In my graph, I want the statement beneath the graph "Where Trimset is .... and formula is NO " to be included.

3. Save graphs in a folder in png or jpeg files

4.JMP data table and graph builder tables exits without saving.

5. how to make an addin feature to make it a single click operation because I need to do this for 100 or more files?

 

I have been reading all the discussions but I am not able to get the right answer. I really appreciate your help.

Thank you

 

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: saving graphs as PNG or JPEG

Here's a different approach, without using the data filter:

dt = Open( "$sample_data/big class.jmp" );
ages = Data Table( "big class" ) << Summary( Group( :age ), Freq( "None" ), Weight( "None" ) );
sexes = Data Table( "big class" ) << Summary( Group( :sex ), Freq( "None" ), Weight( "None" ) );
deletedirectory("$temp/myPics");
createDirectory("$temp/myPics");
For( iage = 1, iage <= N Rows( ages ), iage++,
	thisAge = ages:age[iage];
	For( isex = 1, isex <= N Rows( sexes ), isex++,
		thisSex = sexes:sex[isex];
		eval(evalexpr(biv = dt << Bivariate(
			Y( :weight ),
			X( :height ),
			Automatic Recalc( 1 ),
			Fit Line( {Line Color( {213, 72, 87} )} ),
			where( age == expr(thisAge) & sex == expr(thisSex) )
		)));
		parentBox = Report( biv ) << parent;
		picture = parentBox << getpicture;
		picture << saveImage( "$temp/myPics/age" || Char( thisAge ) || "sex" || Char( thisSex ) || ".png", "png" );
		// optional: Open( "$temp/age" || Char( thisAge ) || "sex" || Char( thisSex ) || ".png" );
		biv<<closewindow;
	);
);
close(ages,"nosave");
close(sexes,"nosave");
show(filesInDirectory("$temp/myPics"));
Files In Directory("$temp/myPics") = {"age12sexF.png", "age12sexM.png", "age13sexF.png", "age13sexM.png", "age14sexF.png", "age14sexM.png", "age15sexF.png", "age15sexM.png", "age16sexF.png", "age16sexM.png", "age17sexF.png", "age17sexM.png"};

where clause above graphwhere clause above graph

In this case the where clause is above the platform's output, so I used the <<parent method to move up through the displaybox tree to include that outer box as well.

Craige

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: saving graphs as PNG or JPEG

My first point, is that you should not be reading the Discussions to learn how to script JMP.  You should be reading the JSL Scripting Guide.

     Help==>Books==>Scripting Guide

Then if you can't get things to work, then the Discussion Community is a great resource to help you.

Let me take a wack at the questions you ask. 

1.  How do you use a local data filter and then save the output.

Below is a crude script that will show you how to do this in a very unfancy way

dt=Open(<path to your data table>);
gb=Graph Builder(
	Size( 527, 453 ),
	Show Control Panel( 0 ),
	Variables( X( :weight ), Y( :height ) ),
	Elements( Points( X, Y, Legend( 13 ) ), Smoother( X, Y, Legend( 14 ) ) )
);

Thefilter=gb<<Local Data Filter(
	Add Filter(
		columns( :age, :sex ),
		Where( :age == {12, 13, 14} ),
		Where( :sex == "F" ),
		Display( :age, Size( 170, 96 ), List Display )
	)
);

report(gb)<<save picture( "path/to/example.png", "png" );

gb<<remove local data filter;

Thefilter=gb<<Local Data Filter(
	Add Filter(
		columns(  :sex ),
		Where( :sex == "F" ),
		Display( :age, Size( 170, 96 ), List Display )
	)
);

report(gb)<<save picture( "path/to/example.png", "png" );

gb<<remove local data filter;


// Add the remaining filter cases

close(dt,nosave);
gb << close window;

The code for the graph builder and each of the local filters is available under the red triangles.  If you select them and go to "Save Script" you will get the code required for the different cases you have.  Just start up Graph Builder.  Then save the script for the graph builder.  Then open the local filter.  Set it to your first case(where clauses) and then save it's script.  Next, change the local filter to the next case and save it's script....etc.

2. The statement below the graph is saved to the png

3. You just need to specify in the Save Picture function the path.  This can be programatically handled so that it can follow any naming method you want to use.

4. The sample script show the method to do this

5. There is very good documentation in the Scripting Guide on how to make an Addin.  It is a far too lengthy answer to be handled in the Discussion

 

Jim
vishwasanj
Level V

Re: saving graphs as PNG or JPEG

Thank you txnelson for the reply. It was very useful. If I wanted to do a for loop in a data filter, is it possible to do it? That it can automatically go through each condition and plot the graphs. Like you suggested, I will look into the Addin feature in the scripting guide.
txnelson
Super User

Re: saving graphs as PNG or JPEG

Yes, a For loop is available.  JSL is a very complete language, with all of the functionality one would expect from a computer language.

I suggest that you read the first few chapters of the Scripting Guide, so you can get a feeling for all of the components of the language and how to implement them.

If you could give me a little information about what computer languages you work with, it would help me in providing you with JSL examples in terms that you are familiar with.

Jim
Craige_Hales
Super User

Re: saving graphs as PNG or JPEG

Here's a different approach, without using the data filter:

dt = Open( "$sample_data/big class.jmp" );
ages = Data Table( "big class" ) << Summary( Group( :age ), Freq( "None" ), Weight( "None" ) );
sexes = Data Table( "big class" ) << Summary( Group( :sex ), Freq( "None" ), Weight( "None" ) );
deletedirectory("$temp/myPics");
createDirectory("$temp/myPics");
For( iage = 1, iage <= N Rows( ages ), iage++,
	thisAge = ages:age[iage];
	For( isex = 1, isex <= N Rows( sexes ), isex++,
		thisSex = sexes:sex[isex];
		eval(evalexpr(biv = dt << Bivariate(
			Y( :weight ),
			X( :height ),
			Automatic Recalc( 1 ),
			Fit Line( {Line Color( {213, 72, 87} )} ),
			where( age == expr(thisAge) & sex == expr(thisSex) )
		)));
		parentBox = Report( biv ) << parent;
		picture = parentBox << getpicture;
		picture << saveImage( "$temp/myPics/age" || Char( thisAge ) || "sex" || Char( thisSex ) || ".png", "png" );
		// optional: Open( "$temp/age" || Char( thisAge ) || "sex" || Char( thisSex ) || ".png" );
		biv<<closewindow;
	);
);
close(ages,"nosave");
close(sexes,"nosave");
show(filesInDirectory("$temp/myPics"));
Files In Directory("$temp/myPics") = {"age12sexF.png", "age12sexM.png", "age13sexF.png", "age13sexM.png", "age14sexF.png", "age14sexM.png", "age15sexF.png", "age15sexM.png", "age16sexF.png", "age16sexM.png", "age17sexF.png", "age17sexM.png"};

where clause above graphwhere clause above graph

In this case the where clause is above the platform's output, so I used the <<parent method to move up through the displaybox tree to include that outer box as well.

Craige
hogi
Level XII

Re: saving graphs as PNG or JPEG

This code adds a small menu to any Data Filter - select your columns and click OK to cycle through the entries and auto-save the GraphBuilder plot.
(columns with continuous values are not listed)

 

hogi_0-1691597841172.png

 

 

Names Default to HERE(1);

if(not(namespaceExists("autoAnimate")),New NameSpace("autoAnimate"));

autoAnimate:update = function({},
Try (autoAnimate:DFTopLB = Current Report()["Local Data Filter", List Box( 1 )], stop());
Try(autoAnimate:myDFTPBs = (Current Report()["Local Data Filter", Lineup Ruler Box( 1 ), List Box( 1 )] << Xpath( "//TabPageBox" )),stop());



// collect Data Fitlers with ListBoxes, remove the continuos ones.
autoAnimate:myDFLBs ={};
For Each ( {TPB,i}, autoAnimate:myDFTPBs, tmp=(TPB << xpath( "//ListBoxBox" ));If(NItems(tmp),Insert Into(autoAnimate:myDFLBs,tmp[1]),Remove from(autoAnimate:myDFTPBs,i) )); 

// which columns are used?
autoAnimate:DFCols = Transform Each( {TPB}, autoAnimate:myDFTPBs, Trim( Word( 1, TPB << Get Title, "(" ) ) );

// what are the entries? (for the filename)
autoAnimate:myDFLBItems = autoAnimate:myDFLBs << get items;

// how many entries?
autoAnimate:myDFLBNItems = Transform Each( {items}, autoAnimate:myDFLBItems, N Items( items ) );

// variales for the for loops
chars = {"aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii"};

// append a Display Box:
Try(autoAnimate:animatePane << delete);
autoAnimate:DFTopLB << append( autoAnimate:animatePane = V List Box (Spacer Box( size( 0, 20 ) ),Panel Box( "cycle selected + save", V List Box( colSelection = List Box( autoAnimate:DFCols ), H List Box( Button Box("update",autoAnimate:update()),Spacer Box (size(40,0)),Button Box( "start", autoAnimate:animateNsave() ) )) ) ));
);

autoAnimate:animateNsave = Function( {},

// which DFs are selected?
	selectedCols = colselection << Get Selected Indices;
	
// compose the expression
	expression = "";
	xx=1;
// for loop for every active DF:
	For( i = 1, i <= N Items( autoAnimate:myDFTPBs ), i++,
		expression = expression || If( Contains( selectedCols, i ),
			"For(" || chars[i] || "=1," || chars[i] || "<=" || Char( autoAnimate:myDFLBNItems[i] ) || "," || chars[i] || "++,__LB" || Char( i ) ||
			"__ << clear selection << set selected(" || chars[i] || ");",
			"if(1,"
		)
	);
// save current Graph builder Plot -- how about other reports?
	expression = expression || "wait();(current report()<< XPath(\!"//OutlineBox[@helpKey='Graph Builder']\!"))<<Save Picture(\!"C:\temp\\!"";
	
// compose the correct filename
	For( i = 1, i <= N Items( autoAnimate:myDFTPBs ), i++, expression = expression ||If( Contains( selectedCols, i ), "||\!""||Char (autoAnimate:DFCols[i])||"=\!"||Char("||Char(autoAnimate:myDFLBItems[i])||"["||chars[i]||"])||\!"_\!"","||\!"_\!""));

	expression = expression || "||\!".png\!")";
	For( i = 1, i <= N Items( autoAnimate:myDFTPBs ), i++,
		expression = expression || ")"
	);
	expression = Parse(expression);
	For( i = 1, i <= N Items( autoAnimate:myDFTPBs ),i++,expression = Substitute(Name Expr(expression ),Parse("__LB"|| Char( i ) ||"__"),autoAnimate:myDFLBs[i]));
	//Name Expr(expression )
	Eval(expression);
	Open("C:\temp");
);

autoAnimate:update();