Hardware vs. Software Fonts in Graphs

March, 2008
by Phil Mason

About the Author

If you have ever been disappointed by the quality of text in graphs then you may not have made use of hardware fonts. It's easy to use them and if you're using windows you can select true-type fonts easily. In the following example I produce PNG format graphs, which are Portable Network Graphics (these have some advantages over some other formats which I will cover another time). Note that when using true-type fonts you should put the font name in quotes, but if you use software fonts then you don't use the quotes. e.g. goptions ftext=swiss ;

SAS PROGRAM

filename sw 'c:\software font.png' ; * file to save graph using software font ; filename hw 'c:\hardware font.png' ; * file to save graph using hardware font ; goptions reset=all gsfname=sw dev=png xmax=6 ymax=4; proc gchart data=sashelp.class; vbar sex / sumvar=height ; run; * produce second graph using the Arial Font with text height set to 6% of total ; goptions gsfname=hw ftext='Arial' htext=6pct ; vbar sex / sumvar=height ; run; quit;

Software Font

Software Font

Hardware Font

Hardware Font

For Unix Users

I have found that on UNIX I can use truetype fonts in my graphs only if they are defined to the SAS system. To define the fonts to your system you can use Proc Fontreg, like this...

proc fontreg ; fontpath "/sas/my_fonts" ; /* define all true type fonts to SAS from this directory */ run;

In fact you can even FTP fonts from your windows system to your UNIX machine, and then define them to SAS using Proc Fontreg. Once fonts are defined then they can be used with the SASEMF device which automatically may access all defined fonts, by using this code ...

Options device=sasemf ; /* use sasemf, which always works */

To use fonts with other devices such as PNG, then you must define the font to the device driver, using code like this...

proc gdevice c=gdevice0.devices nofs; copy png from=sashelp.devices newname=png ; modify png charrec=(0,24,80,"arial",'y'); /* list png ;*/ run;quit;

Changing Resolution Of Graphs

You can use several settings in SAS graph to easily choose the resolution of graphs to produce. You may want to produce a low resolution graph to use as a thumbnail, which when clicked on takes you to a higher resolution graph.

The following SAS macro demonstrates how we can choose a size and resolution for a graph by altering a few parameters:

  • Xmax/Ymax - width & height of graphic area in inches, centimetres or points
  • Xpixels/Ypixels - width & height of graphic area in pixels

Horizontal Dots Per Inch = number of dots / number of inches = xpixels / xmax

For documentation see http://v8doc.sas.com/sashtml/gref/zct-xmax.htm

SAS Program

%macro testres(x,y,dpi) ; * x ... number of inches across graphic ; * y ... number of inches across graphic ; * dpi ... resolution in dots per inch ; filename out "c:\test&dpi..png" ; goptions reset=all /* reset everything first */ dev=png /* driver to use */ xmax=&x in ymax=&y in xpixels=%sysevalf(&x*&dpi) ypixels=%sysevalf(&y*&dpi) ftext="SAS Monospace" /* choose a nice font */ htext=3 pct /* make font a good size */ gsfname=out /* where to save graphic */ ; proc gchart data=sashelp.class ; hbar3d age / discrete ; run ; quit ; %mend testres ; %testres(6,4,600) ; %testres(6,4,50) ;

6X4 INCH, 600DPI, 8.6 MEGA-PIXELS, SIZE=1KB
6X4 INCH, 600DPI, 8.6 MEGA-PIXELS, SIZE=1KB

6X4 INCH, 50DPI, 0.1 MEGA-PIXELS, SIZE=29KB
6X4 INCH, 50DPI, 0.1 MEGA-PIXELS, SIZE=29KB