Generate Code

You can generate much of the code needed to recreate the content of a graphics window. The examples on this page include adding lines to the generated code, and converting the code to an IDL function.

Adding to the Generated Code

As certain things are currently not supported by the GenerateCode method, the code to achieve these things must be added after the code is generated. For example, generate a test contour plot with the following:

c = CONTOUR(/test)

Then add a COLORBAR to the graphic using the Insert Colorbar operation on the toolbar. Because the COLORBAR is currently not supported, it will not appear in the generated output after you press the Generate Code button. If the Generate Code with the Save Data option is used, the generated code will looking something like the following:

; Code generated by IDL

restore, "c:\mycode.sav"

z0 = data['z0']

x0 = data['x0']

y0 = data['y0']

contour0 = CONTOUR(z0, x0, y0) ; <- Data required

In order to have the COLORBAR appear when running the code, you must modify it manually. Add the following line immediately following the call to CONTOUR:

cb = COLORBAR(TARGET=contour0)

Now when the code is run, the COLORBAR will appear in the window.

Note: be sure to set the TARGET keyword to the desired visualization.

Converting the code to a function

Use the Generate Code utility to create a function that you can call to apply properties to any future visualizations. An example of this would be a SCATTERPLOT, but with different default symbols and colors than the defaults. To achieve this, start with a basic SCATTERPLOT, using the /TEST option as the real data will be used later.

s = SCATTERPLOT(/TEST)

Using the toolbar in the graphics window, the symbol, the fill color, the line color and the symbol size can be easily changed to the desired defaults. Now, when the Generated Code method is run (no need to use the Save Data option), the resulting code will look something like the following:

; Code generated by IDL

scatterplot0 = SCATTERPLOT(x, y, $ ; <- Data required

SYM_COLOR=[253,61,41], $

SYMBOL=24, $

SYM_SIZE=2.0000000, $

SYM_FILLED=1, $

SYM_FILL_COLOR=[4,14,222])

To use this code as a function, you would need to add two lines of code. First add a signature line to the top of the file, ensuring that the input variables are the same as those in the call to the graphic, e.g., x and y. A line of code such as the following:

function myscatterplot, x, y

Second, since this is a function, it must return something. Just before the end of the file, insert the following to return a reference to the graphic that you just created:

return, scatterplot0

The entire code should now look something like this:

function myscatterplot, x,y

 

; Code generated by IDL

scatterplot0 = SCATTERPLOT(x, y, $ ; <- Data required

SYM_COLOR=[253,61,41], $

SYMBOL=24, $

SYM_SIZE=2.0000000, $

SYM_FILLED=1, $

SYM_FILL_COLOR=[4,14,222])

 

return, scatterplot0

 

end

Now you can call this new function from IDL (instead of the original SCATTERPLOT) and all the "new" default properties will be applied. Note that since this returns a reference to the underlying SCATTERPLOT, the returned value can be used in the future exactly as in a regular SCATTERPLOT. Example:

x = randomu(seed, 100)

y = randomu(seed, 100)

s = myscatterplot(x, y)

s.title = 'Random data'

See Also