Example Program and Statement Details

Example Graph

The following graph was generated by the Example Program. The first DRAWTEXT statement shows how to draw multiple lines of text in a specific position within the graph. The second DRAWTEXT statement shows how to create a watermark, which is achieved by applying transparency to text that is rotated within the graph.
Drawing Text in a Graph

Example Program

proc template;
 define statgraph modelfit;
  begingraph;
    entrytitle "Regression Fit Plot";
    layout lattice;
      layout overlay / xaxisopts=(offsetmin=.1);
        drawtext textattrs=(style=italic size=8pt)
          "Band shows 99% Confidence Limit of Mean" /
          anchor=bottomleft width=15 widthunit=percent
          xspace=wallpercent yspace=wallpercent
          x=0 y=10 justify=center ;
        modelband "myclm";
        scatterplot x=height y=weight / primary=true;
        regressionplot x=height y=weight / alpha=.01 clm="myclm";
      endlayout;
    endlayout;
    drawtext textattrs=(color=gray size=52pt) "CONFIDENTIAL" /
      transparency=.75 rotate=-35
      width=110 widthunit=percent justify=center ;
  endgraph;
 end;

proc sgrender data=sashelp.class template=modelfit;
run;

Statement Summary

A DRAWTEXT statement draws a text box that contains one or more lines of text. The text can be formatted, using the TEXTATTRS= prefix option. By default, the text box is drawn in the center of the graph. You can change the default position with the options ANCHOR=, X=, Y=, XSPACE=, and YSPACE=.
For general information about the types of elements that can be drawn with the draw statements, the drawing space and drawing units that they use, and how the drawn elements are anchored, see Key Concepts for Using Draw Statements. For detailed usage information, consult the SAS Graph Template Language: User's Guide.

Required Argument

text-item <…<text-item>>
specifies one or more pieces of text for the text box. Each text-item has the following form:
<prefix-option> "string" | dynamic | character-expression | {text-command}
Each piece of text can have a prefix setting that precedes the piece of text. A piece of text is either a string literal, a dynamic, or a text command. All text-items are concatenated into one string, which might be wrapped, based on the settings for the WIDTH= and WIDTHUNIT= settings. Leading and trailing blanks in the concatenated string are always used.
When used, a prefix option applies to the immediately following piece of text and also to all subsequent text strings and text-commands until another prefix option is specified.
Requirement:
  • string must be enclosed in quotation marks.
  • character-expression must be enclosed in an EVAL function.
  • text-command must be enclosed in braces.
See Managing Text Items for more information and several examples.

Options and Text Commands

Prefix Option
Description
Specifies the color and font properties of all of the specified text or individual text-items.
Text Command
Description
{SUB}
Specifies that the string or dynamic appears as a subscript.
{SUP}
Specifies that the string or dynamic appears as a superscript.
Specifies a glyph (graphical character) to be displayed using its Unicode specification or keyword equivalent.
Statement Option
Description
Specifies an anchor point for the text box.
Specifies whether a border is drawn around the text box.
Specifies the properties of the border line that is drawn around the text box.
Specifies an amount to offset the anchor point from discrete X values, or discrete Y values, or both.
Specifies a default drawing space and drawing units for this DRAWTEXT statement.
Specifies the alignment of text that wraps within the text box.
Specifies whether the text box appears on top of or behind the graph.
Specifies the amount of extra space that is reserved inside the text box’s border
Specifies the angle of rotation for the text box, measured in degrees.
Specifies the degree of the transparency of the background, text, and border.
Specifies the width of the text box.
Specifies whether the value that is specified for the WIDTH= option is interpreted as a percentage value, a pixel value, or a value that is in the units of the data.
Specifies the anchor point’s X coordinate.
Specifies whether the data value for the X= option is interpreted using the primary X axis scale or to the secondary X (X2) axis scale.
Specifies the drawing space and drawing units for interpreting the value that is specified in the X= option.
Specifies the anchor point’s Y coordinate.
Specifies whether the data value for the Y= option is interpreted using the primary Y axis scale or to the secondary Y (Y2) axis scale.
Specifies the drawing space and drawing units for interpreting the value that is specified in the Y= option.
ANCHOR=CENTER | TOPLEFT | TOP | TOPRIGHT | LEFT | RIGHT | BOTTOMLEFT | BOTTOM | BOTTOMRIGHT
specifies an anchor point for the text box.
Default: CENTER
Discussion: The anchor point can be at the center of the text box or at eight points on the border of the text box bounding box. The following figure shows the anchor points for TOPLEFT and LEFT.
text box anchor positions
The coordinates of the anchor point are set by the X= and Y= options, and by the XSPACE= and YSPACE= options. The XAXIS= and YAXIS= option might affect positioning when the XSPACE= or YSPACE= options are set to DATAPIXEL, DATAPERCENT, or DATAVALUE.
The text box has a fixed width, determined by the WIDTH= and WIDTHUNIT= options. The height of the text box is based on the amount of text specified and the font size. The height of the text grows in a direction that is related to the anchor point. For example, if ANCHOR=TOPLEFT, the text box height extends downward from the anchor point and its width extends to the right. If ANCHOR=CENTER, half the text box width and half the text box height extend equally left and right, as well as top to bottom from the anchor point. If ANCHOR=BOTTOM, the text box height extends upward from the anchor point and the text box width is centered at the anchor point.
BORDER
specifies whether a border is drawn around the text box.
Default: FALSE
Tip: Use the BORDERATTRS= option to control the appearance of the border.
BORDERATTRS= style-element | style-element (line-options) | (line-options)
specifies the properties of the border line that is drawn around the text box. See General Syntax for Attribute Options for the syntax on using a style-element and Line Options for available line-options.
Default: The GraphBorderLines style element.
Interaction: BORDER=TRUE must be set for this option to have any effect.
DISCRETEOFFSET= number
specifies an amount to offset the anchor point from discrete X values, or discrete Y values, or both.
Default: 0 (no offset, the anchor point is centered on discrete X values, or discrete Y values, or both)
Range: -0.5 to +0.5, where .5 represents half the distance between discrete ticks. If the X axis is discrete, a positive offset is to the right. If the Y axis is discrete, a positive offset is up. If REVERSE=TRUE on the X or Y axis, then the offset direction is also reversed.
DRAWSPACE= GRAPHPERCENT | GRAPHPIXEL | LAYOUTPERCENT | LAYOUTPIXEL | WALLPERCENT | WALLPIXEL | DATAPERCENT | DATAPIXEL | DATAVALUE
specifies a default drawing space and drawing units for this DRAWTEXT statement.
Default: LAYOUTPERCENT
Interaction: This statement and all of the draw statements inherit the global DRAWPSACE= setting from the DRAWSPACE= option in the BEGINGRAPH statement. Setting this option changes the setting for only this DRAWTEXT statement.
Interaction: This option sets the default drawing space, but individual settings in the X or Y dimension can be overridden by the options XSPACE=, YSPACE=, or WIDTHUNIT=.
JUSTIFY=LEFT | CENTER | RIGHT
Specifies the alignment of text that wraps within the text box.
Default: LEFT
LEFT
forces the first character of each line to appear at the left margin (distance from the left border plus the left pad amount).
CENTER
forces each line to be centered in the text box between the left and right pad amounts.
RIGHT
forces the last character of each line to appear at the right margin (distance from the right border minus the right pad amount).
Interaction: Text is wrapped based on the width of the specified text, the font size, and the setting in the PAD= option.
LAYER=FRONT | BACK
specifies whether the text box appears on top of (FRONT) or behind (BACK) the graph.
Default: FRONT
Interaction: If this option is set to BACK, the text box is drawn behind background areas, such as a layout or legend background.For elements that are obstructed because they are in the back layer, you can suppress the display of filled areas in the graph. Or, you can use transparency to manage the element visibility. For more information, see About Drawing Layers.
PAD=dimension | (pad-options)
specifies the amount of extra space that is reserved inside the text box’s border.
Default: (LEFT=3 RIGHT=3 TOP=0 BOTTOM=0)
dimension
Specifies a dimension to use for the extra space at the left, right, top, and bottom of the text box border.
(pad-options)
Enables separate settings for the left, right, top, and bottom padding dimensions. Use the pad-options to create non-uniform padding. These options must be enclosed in parentheses. Each option is specified as a name = value pair. Sides not assigned padding are padded with the default amount.
Values without units are in pixels (px). A unit must be provided if other than pixels.
LEFT=dimension specifies the amount of extra space added to the left side.
RIGHT=dimension specifies the amount of extra space added to the right side.
TOP=dimension specifies the amount of extra space added to the top.
BOTTOM=dimension specifies the amount of extra space added to the bottom.
ROTATE=number
specifies the angle of rotation for the text box, measured in degrees. The angle is measured as if a horizontal line extended to the right through the text box anchor point. Positive angles rotate the text box counter clockwise, and negative angles rotate the text box clockwise. The angle specification can exceed 360 degrees in absolute value.
Default: 0. No rotation is performed
{ SUB "string" | dynamic }
text-command that specifies that the string or dynamic is to appear as subscript text.
Default: no default
Example:
drawtext "y = " b{sub "0"} " + b" {sub "1"} "x";
{ SUP "string" | dynamic }
text-command that specifies that the string or dynamic is to appear as superscript text.
Default: no default
Example:
drawtext "R" {sup "2"} " = " {format (6.4) RSQUARED} ;
TEXTATTRS=style-element | style-element (text-options) | (text-options)
prefix-option that specifies the color and font properties of the entire text string or individual text-items. See General Syntax for Attribute Options for the syntax on using a style-element and Text Options for available text-options.
Default: The GraphValueText style element.
Interaction: When multiple TEXTATTRS=( ) prefix options are used, each one cancels the last, resetting all text properties to the default set by the GraphValueText style element. Subsequent text-items to the right are then assigned the text properties specified in the closest TEXTATTRS=( ) setting to their left. Thus, to vary the text properties across text-items, you do not have to override settings from a previous TEXTATTRS=( ) setting. Each TEXTATTRS=( ) specification resets all text properties to the default so that only the new settings are applied to subsequent text-items.
Tip: To ensure that all text has the same text properties, use this prefix option once only and place it before the first text-item.
TRANSPARENCY= number
specifies the degree of the transparency of the background, text, and border.
Default: 0
Range: 0 (opaque) to 1 (entirely transparent)
{ UNICODE "hex-string"x | keyword | dynamic }
text-command that specifies a glyph (character) to be displayed using its Unicode specification or keyword equivalent.
Default: no default
"hex-string"x
A four-byte hexadecimal constant that represents a UNICODE character in the current font. For a complete listing, see http://unicode.org/charts/charindex.html.
keyword
A SAS keyword for a UNICODE character. For a listing of SAS supplied keywords, see Reserved Keywords and Unicode Values..
dynamic
The dynamic must resolve to either "hex-string"x or a keyword for a UNICODE character.
Examples: The following statements show how to use the {UNICODE} text command:
drawtext {unicode alpha} "=" CONF;

drawtext {unicode "03B1"x} "=" CONF;
This text command attempts to access a UNICODE value in the current font. Not all fonts support accessing characters via their UNICODE value. Some fonts support only a limited set of UNICODE values. If the UNICODE value is not accessible, the command might be ignored or an unprintable character might be substituted.
WIDTH= positive-number
specifies the width of the text box.
Default: 10
Interaction: The WIDTHUNIT= option determines the units of positive-number, which is PERCENT by default.
WIDTHUNIT=PERCENT | PIXEL | DATA
specifies whether the value that is specified for the WIDTH= option is interpreted as a percentage value, a pixel value, or a value that is in the units of the data.
Default: PERCENT
Interaction: This setting combines with the XSPACE= setting to interpret the width that is set in the WIDTH= argument.
X=constant | scalar-expression
specifies the anchor point’s X coordinate.
Default: 50
Interaction: The DRAWSPACE= option determines the default interpretation of the units for this setting. You can override the default with the XSPACE= option.
Interaction: If XSPACE=DATAVALUE, this option's value is interpreted using the XAXIS= option.
XAXIS= X | X2
specifies whether the value that is specified for the X= option is interpreted using the primary X axis scale or to the secondary X (X2) axis scale.
Default: X
Interaction: This option has effect only if XSPACE=DATAVALUE.
XSPACE= GRAPHPERCENT | GRAPHPIXEL | LAYOUTPERCENT | LAYOUTPIXEL | WALLPERCENT | WALLPIXEL | DATAPERCENT | DATAPIXEL | DATAVALUE
specifies the drawing space and drawing units for interpreting the value that is specified in the X= option.
Default: The setting that is in effect for the DRAWSPACE= option.
Y=constant | scalar-expression
specifies the anchor point’s Y coordinate.
Default: 50
Interaction: The DRAWSPACE= option determines the default interpretation of the units for this setting. You can override the default with the YSPACE= option.
Interaction: If YSPACE=DATAVALUE, this option's value is interpreted using the YAXIS= option.
YAXIS= Y | Y2
specifies whether the value that is specified for the Y= option is interpreted using the primary Y axis scale or to the secondary Y (Y2) axis scale.
Default: Y
Interaction: This option has effect only if YSPACE=DATAVALUE.
YSPACE= GRAPHPERCENT | GRAPHPIXEL | LAYOUTPERCENT | LAYOUTPIXEL | WALLPERCENT | WALLPIXEL | DATAPERCENT | DATAPIXEL | DATAVALUE
specifies the drawing space and drawing units for interpreting the value that is specified in the Y= option.
Default: The setting that is in effect for the DRAWSPACE= option.