A Plotting Routine

This section develops a plotting routine that uses many of the object graphics features discussed here and in previous sections.

Example Code
The code for this example is contained in the file obj_plot.pro, located in the examples/doc/objects subdirectory of the IDL distribution. Run the example procedure by entering obj_plot at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT obj_plot.pro.

The OBJ_PLOT routine will create a window object, and display within it a view of a single model object, which will contain a plot object, x- and y-axis objects, and an x-axis title object. It will use the Times Roman font for the axis title.

In creating the procedure, we allow the user to specify the data to be plotted, and we define keyword variables which can return the object references for the view, model, window, axis, and plot objects. This allows the user to manipulate the object tree after it has been created. We also specify the _EXTRA keyword, which allows the user to include other keyword parameters in the call. OBJ_PLOT itself passes any extra keyword parameters only to the plot object, but a more complex program could pass keyword parameters to any of the objects created. The following lines begin the procedure.

Note
See A Function for Coordinate Conversion for a discussion of the NORM_COORD function used in this example. Also, SET_VIEW is discussed in Finding an Appropriate View Volume. (The files set_view.pro and norm_coord.pro are included in the examples/doc/utilities subdirectory of the IDL distribution. NORM_COORD is also defined in the obj_plot.pro file.)

Now, the OBJ_PLOT routine can be called with only the data parameter, if you choose. For example, the statement

OBJ_PLOT, FINDGEN(10) 

creates and displays the object hierarchy with a simple plot line. However, if you do not retrieve the window, view, and other object references via the keywords, there is no way you can interactively modify the plot.

A better way to call OBJ_PLOT would be:

OBJ_PLOT, FINDGEN(10), WINDOW=win, VIEW=view, PLOT=plot, 
CONTAINER=cont 

This statement creates the same object hierarchy, but returns the object references for the window, view, and plot objects in named variables. Having access the object references allows you to do things like change the color of the plot:

plot->SetProperty, COLOR=[255,255,255] 
window->Draw, view 

enlarge the viewplane rectangle by 10 percent:

view->GetProperty, VIEWPLANE_RECT=vr 
vr2 = [vr[0]-(vr[0]*0.1), vr[1]-(vr[1]*0.1), $ 
       vr[2]+(vr[2]*0.1), vr[2]+(vr[2]*0.1)] 
view->SetProperty, VIEWPLANE_RECT = vr2 
window->Draw, view 

or just clean it up:

OBJ_DESTROY, cont 

Note that when using the OBJ_DESTROY procedure, any object added to the specified object (using the Add method) are also destroyed, recursively. We use a container object to collect all of the objects, including attribute objects and text object that are not explicitly added to the object tree, which allows you to destroy the entire collection with a single call to OBJ_DESTROY.

Improvements to the OBJ_PLOT Routine

A number of improvements to the OBJ_PLOT routine are left as exercises for the programmer: