cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
scott1588
Level IV

Dynamically change latitude and longitude in Graph Builder with ComboBox pulldown menu

Note: Data and script uploaded below...

 

I have developed the following script that first plots a wide area showing a route taken. A modal window pops up that allows the user to enter a code for a specific location -- in this example a 1 or 2. With this information, the graph will zoom in to that location. All this works fine but it's not exactly what I need. Most importantly, the dialog box disappears once the selection in made and the script ends. Second, I have been trying to get this to work with a ComboBox pulldown menu. I can get the menu itself working but for some reason, I can't seem to get the choice to make a change in the graph.

 

Here is what I have with the modal text box...

 

Names Default To Here( 1 );

dt = Data Table( "Test Data" );

gb = Graph Builder(
	Graph Spacing( 10 ),
	Spacing Borders( 1 ),
	Variables( X( :Longitude ), Y( :Latitude ) ),
	Elements( Points( X, Y, Legend( 3 ) ) ),
	SendToReport(
		Dispatch( {}, "Longitude", ScaleBox, {Min( -104 ), Max( -98.47 )} ),
		Dispatch( {}, "Latitude", ScaleBox, {Min( 29.21 ), Max( 33.30 )} ),		
		Dispatch( {}, "Graph Builder", FrameBox,
			{Background Map( Images( "Street Map Service", "Mapbox Satellite", "" ) )}
		)
	)
);

// Build the modal input window
New Window( "Pick a spot", 
	Modal,
	H List Box(
		V List Box( Text Box( "Location Number:" ), ),
		V List Box( box = Number Edit Box( "Enter location number..." ), )
	),
	Button Box( "OK", 
		// Script to run when "OK" is clicked
		locNum = box << Get;
		Print( "Location Number: " || Char( locNum ) );
	),
	Button Box( "Cancel", 
		// Script to run when "Cancel" is clicked
		Throw()
	)
);


If(
	locNum == 1,
		minLat = 30.99;
		maxLat = 31.06;
		minLong = -101.21;
		maxLong = -101.11;
		run = "1|1";,
	locNum == 2,
		minLat = 31.34;
		maxLat = 31.41;
		minLong = -101.81;
		maxLong = -101.71;
		run = "2|1";
);

gb << Dispatch( {}, "Longitude", ScaleBox, {Min( minLong ), Max( maxLong )} );

gb << Dispatch( {}, "Latitude", ScaleBox, {Min( minLat ), Max( maxLat )} );

The initial map looks like this...

 

scott1588_0-1759561057434.png

 

And the zoomed in view looks like this...

 

scott1588_1-1759561157817.png

 

So my dilemmas are 1) how do I get the ComboBox to work with this, and 2) how can I prevent the script from completing so the user can continue to select different items from the pulldown menu.

 

I have a feeling this has something to do with the Set Function command but my multiple attempts at getting this to work have been fruitless.

 

Any guidance on this would be greatly appreciated.

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Dynamically change latitude and longitude in Graph Builder with ComboBox pulldown menu

I wouldn't use modal window for something like this

Names Default To Here(1);

dt = Data Table("Test Data");

gb = dt << Graph Builder(
	Graph Spacing(10),
	Spacing Borders(1),
	Variables(X(:Longitude), Y(:Latitude)),
	Elements(Points(X, Y, Legend(3))),
	SendToReport(
		Dispatch({}, "Longitude", ScaleBox, {Min(-104), Max(-98.47)}),
		Dispatch({}, "Latitude", ScaleBox, {Min(29.21), Max(33.30)}),
		Dispatch({}, "Graph Builder", FrameBox,
			{Background Map(Images("Street Map Service", "Mapbox Satellite", ""))}
		)
	)
);
wait(0);

cb_expr = Expr(
	locNum = cb_option << get;
	show(locNum);
	If(
		locNum == 1,
			minLat = 30.99;
			maxLat = 31.06;
			minLong = -101.21;
			maxLong = -101.11;
			run = "1|1";,
		locNum == 2,
			minLat = 31.34;
			maxLat = 31.41;
			minLong = -101.81;
			maxLong = -101.71;
			run = "2|1";
	);

	gb << Dispatch({}, "Longitude", ScaleBox, {Min(minLong), Max(maxLong)});

	gb << Dispatch({}, "Latitude", ScaleBox, {Min(minLat), Max(maxLat)});
);


// Build the modal input window
nw = New Window("Pick a spot",
	Lineup Box(N Col(2), 
		Text Box("Location Number:"), 
		cb_option = Combo Box({"1", "2"},
			cb_expr
		),
	),
	Button Box("OK",
		cb_expr; // not necessary if you wish to run when combo box is changed
		Print("Location Number: " || Char(locNum));
	),
	Button Box("Cancel", 
		// Script to run when "Cancel" is clicked
		Throw()
	)
);



You could also move your options into associative array so it makes it easier to add more options later

-Jarmo

View solution in original post

3 REPLIES 3
jthi
Super User

Re: Dynamically change latitude and longitude in Graph Builder with ComboBox pulldown menu

I wouldn't use modal window for something like this

Names Default To Here(1);

dt = Data Table("Test Data");

gb = dt << Graph Builder(
	Graph Spacing(10),
	Spacing Borders(1),
	Variables(X(:Longitude), Y(:Latitude)),
	Elements(Points(X, Y, Legend(3))),
	SendToReport(
		Dispatch({}, "Longitude", ScaleBox, {Min(-104), Max(-98.47)}),
		Dispatch({}, "Latitude", ScaleBox, {Min(29.21), Max(33.30)}),
		Dispatch({}, "Graph Builder", FrameBox,
			{Background Map(Images("Street Map Service", "Mapbox Satellite", ""))}
		)
	)
);
wait(0);

cb_expr = Expr(
	locNum = cb_option << get;
	show(locNum);
	If(
		locNum == 1,
			minLat = 30.99;
			maxLat = 31.06;
			minLong = -101.21;
			maxLong = -101.11;
			run = "1|1";,
		locNum == 2,
			minLat = 31.34;
			maxLat = 31.41;
			minLong = -101.81;
			maxLong = -101.71;
			run = "2|1";
	);

	gb << Dispatch({}, "Longitude", ScaleBox, {Min(minLong), Max(maxLong)});

	gb << Dispatch({}, "Latitude", ScaleBox, {Min(minLat), Max(maxLat)});
);


// Build the modal input window
nw = New Window("Pick a spot",
	Lineup Box(N Col(2), 
		Text Box("Location Number:"), 
		cb_option = Combo Box({"1", "2"},
			cb_expr
		),
	),
	Button Box("OK",
		cb_expr; // not necessary if you wish to run when combo box is changed
		Print("Location Number: " || Char(locNum));
	),
	Button Box("Cancel", 
		// Script to run when "Cancel" is clicked
		Throw()
	)
);



You could also move your options into associative array so it makes it easier to add more options later

-Jarmo
scott1588
Level IV

Re: Dynamically change latitude and longitude in Graph Builder with ComboBox pulldown menu

Thanks, @jthi . This is perfect.

 

So the idea is to wrap the ComboBox actions in an Eval() block and then run the block as a script when ComboBox is executed. I think I'm interpreting this correctly.

jthi
Super User

Re: Dynamically change latitude and longitude in Graph Builder with ComboBox pulldown menu

You can move the expression inside combo box BUT it is generally much easier to modify if you separate it, especially if you are using same action in multiple places

View more...
Names Default To Here(1);

dt = Data Table("Test Data");

gb = dt << Graph Builder(
	Graph Spacing(10),
	Spacing Borders(1),
	Variables(X(:Longitude), Y(:Latitude)),
	Elements(Points(X, Y, Legend(3))),
	SendToReport(
		Dispatch({}, "Longitude", ScaleBox, {Min(-104), Max(-98.47)}),
		Dispatch({}, "Latitude", ScaleBox, {Min(29.21), Max(33.30)}),
		Dispatch({}, "Graph Builder", FrameBox,
			{Background Map(Images("Street Map Service", "Mapbox Satellite", ""))}
		)
	)
);
wait(0);

// Build the modal input window
nw = New Window("Pick a spot",
	Lineup Box(N Col(2), 
		Text Box("Location Number:"), 
		cb_option = Combo Box({"1", "2"},
			locNum = cb_option << get;
			show(locNum);
			If(
				locNum == 1,
					minLat = 30.99;
					maxLat = 31.06;
					minLong = -101.21;
					maxLong = -101.11;
					run = "1|1";,
				locNum == 2,
					minLat = 31.34;
					maxLat = 31.41;
					minLong = -101.81;
					maxLong = -101.71;
					run = "2|1";
			);

			gb << Dispatch({}, "Longitude", ScaleBox, {Min(minLong), Max(maxLong)});

			gb << Dispatch({}, "Latitude", ScaleBox, {Min(minLat), Max(maxLat)});
		),
	),
	Button Box("OK",
		locNum = cb_option << get;
		show(locNum);
		If(
			locNum == 1,
				minLat = 30.99;
				maxLat = 31.06;
				minLong = -101.21;
				maxLong = -101.11;
				run = "1|1";,
			locNum == 2,
				minLat = 31.34;
				maxLat = 31.41;
				minLong = -101.81;
				maxLong = -101.71;
				run = "2|1";
		);

		gb << Dispatch({}, "Longitude", ScaleBox, {Min(minLong), Max(maxLong)});

		gb << Dispatch({}, "Latitude", ScaleBox, {Min(minLat), Max(maxLat)});
		Print("Location Number: " || Char(locNum));
	),
	Button Box("Cancel", 
		// Script to run when "Cancel" is clicked
		Throw()
	)
);

You can also use << Set Function instead of <<Set Script (<<Set Script isn't usually "visible", but it is the same as optional <script> argument you can use, Combo Box)

Names Default To Here(1);

aa_options = Associative Array(); 
aa_options[1] = [
	"minLat" => 30.99,
	"maxLat" => 31.06,
	"minLong" => -101.21,
	"maxLong" => -101.11,
	"run" => "1|1"
];
aa_options[2] = [
	"minLat" => 31.34,
	"maxLat" => 31.41,
	"minLong" => -101.81,
	"maxLong" => -101.71,
	"run" => "2|1"
];

update_graph = Function({cb_ref, idx}, {Default Local},
	s = aa_options[idx];
	show(s);
	gb << Dispatch({}, "Longitude", ScaleBox, {Min(s["minLong"]), Max(s["maxLong"])});
	gb << Dispatch({}, "Latitude", ScaleBox, {Min(s["minLat"]), Max(s["maxLat"])});	
);



dt = Data Table("Test Data");

gb = dt << Graph Builder(
	Graph Spacing(10),
	Spacing Borders(1),
	Variables(X(:Longitude), Y(:Latitude)),
	Elements(Points(X, Y, Legend(3))),
	SendToReport(
		Dispatch({}, "Longitude", ScaleBox, {Min(-104), Max(-98.47)}),
		Dispatch({}, "Latitude", ScaleBox, {Min(29.21), Max(33.30)}),
		Dispatch({}, "Graph Builder", FrameBox,
			{Background Map(Images("Street Map Service", "Mapbox Satellite", ""))}
		)
	)
);
wait(0);


options = Transform Each({item}, aa_options << get keys, Char(item)); // combo box requires strings
nw = New Window("Pick a spot",
	Lineup Box(N Col(2), 
		Text Box("Location Number:"), 
		cb_option = Combo Box(options, 
			<< Set Function(Function({this, idx},
				update_graph(this, idx);
			))
		)
	),
	Button Box("Cancel", 
		<< Set Function(function({this},
			this << close window;
		))
	)
);
-Jarmo

Recommended Articles