Previous Page | Next Page

Building a Web Application with SAS Stored Processes

Embedding Graphics


Embedding Graphics in Web Pages

Web pages frequently contain embedded graphic images. For static images, an <IMG> tag is enough to embed the image, as shown in the following example:

 <IMG SRC="mykids.gif">

Dynamically generated images, such as charts that vary over time or due to input parameters, are more complicated. Stored processes can generate graphics in addition to HTML output. The following stored process creates a bar chart followed by a tabular report:

 /* Sales by Region and Product */

 *ProcessBody;
 %stpbegin;

 title "Sales by Region and Product";
 legend1 label=none frame;

 proc gchart data=sashelp.shoes;
    hbar3d region / sumvar=sales
       sum space=.6
       subgroup=product
       shape=cylinder
       patternid=subgroup
       legend=legend1;
    label product='Shoe Style';
    run;

 proc report data=sashelp.shoes;
    column region product sales;
    define region / group;
    define product / group;
    define sales / analysis sum;
    break after region / ol summarize suppress skip;
    run;

 %stpend;

Depending on input parameters, this stored process might produce the following output:

Web Page with Embedded Graphic

[Sample output]

No special code was added to handle the image. ODS and the stored process framework takes care of the details of delivering both the HTML and the image to the Web browser. This code handles different image types through the _GOPT_DEVICE input parameter that is supported by the %STPBEGIN macro. For more information, see Using the %STPBEGIN and %STPEND Macros. The image is delivered to the Web browser in different ways depending on the graphics device. JAVA and ACTIVEX images are generated by embedding an <OBJECT> tag in the generated HTML that contains the attributes and parameters necessary to invoke the viewer and to display the graphic. There is no <IMG> tag in this case. Other commonly used drivers (GIF, JPEG, PNG, ACTXIMG, and JAVAIMG) do use the <IMG> tag. The following code is an HTML fragment that is generated by the previous stored process using the GIF image driver:

 <IMG SRC="/SASStoredProcess/do?_sessionid=
    7CF645EB-6E23-4853-8042-BBEA7F866B55
    &_program=replay&entry;=
    STPWORK.TCAT0001.GCHART.GIF">

The image URL in the <IMG> tag is actually a reference to the SAS Stored Process Web Application that uses the special stored process named REPLAY. The REPLAY stored process takes two parameters, _SESSIONID and ENTRY. _SESSIONID is new, unique value each time the original stored process is executed. ENTRY is the name of a temporary SAS catalog entry that contains the generated image. Image replay uses a special, lightweight version of the stored process sessions feature to hold image files temporarily until the Web browser retrieves them. For more information, see Using Sessions.

You can use the REPLAY stored process to replay entries other than embedded images, such as CSS style sheets, JavaScript include files, PDF files, and HTML or XML files to be displayed in a pop-up window, frame or <IFRAME>. The special macro variable _TMPCAT contains the name of the temporary catalog that is used for REPLAY entries. The variable _REPLAY contains the complete URL that is used to reference the REPLAY stored process (except the actual entry name). The _TMPCAT catalog remains on the server for only a limited time. If the catalog is not accessed within a timeout period (typically 15 minutes), then the catalog and its contents are deleted.


Generating Direct Graphic Output

In some cases, you might want to generate image output directly from a stored process with no HTML container. This might be useful if you want to include a dynamically generated graphic in static HTML pages or HTML generated by an unrelated stored process, as shown in the following example:

 /* Sales by Product - pie chart stored process
  *
  * XPIXELS and YPIXELS input parameters are required */

 /* assume we want a new image generated 
  * each time the image is viewed - 
  * disable browser caching of this image */
 %let old=%sysfunc(stpsrv_header(Pragma, nocache));

 /* need test here in case XPIXELS 
  * or YPIXELS are not defined */

 /* set up graph display options */
 goptions gsfname=_webout gsfmode=replace 
    dev=png xpixels=&XPIXELS; 
    ypixels=&YPIXELS; ;

 /* create a simple pie chart */
 proc gchart data=sashelp.shoes;
    pie3d product/sumvar=sales;
    run;
    quit;

This stored process expects XPIXELS and YPIXELS to be passed as input parameters. A typical IMG tag to invoke this stored process might be similar to the following example:

 <IMG SRC="/SASStoredProcess/do?_program=
    /WebApps/Utilities/Sales+by+Product&XPIXELS;=
	    400&YPIXELS&YPIXELS;=300">

which results in the following output:

Graphic Output

[Sample pie chart]

Note:   Some Web browser versions have a defect that causes the Web browser to ignore the NOCACHE and Expires directives in the HTTP header. This defect causes the Web browser to reuse the image that was previously generated from its cache even if the HTTP header directed that no caching was to occur. This might happen when embedding an image in an HTML page or when directly entering an image URL in the Web browser. The old image might be updated by manually performing a Web browser REFRESH or RELOAD, but it is difficult to work around this problem without manual intervention.  [cautionend]

Previous Page | Next Page | Top of Page