(archive 'newLISPer)

January 25, 2006

Quickie PostScript graphics

Filed under: newLISP — newlisper @ 13:17


If you want to generate some quick graphics from a newLISP script, here’s a simple technique that involves just a small bit of PostScript code. (If you’re using Linux or Windows, you’ll need a PostScript interpreter and PDF converter somewhere on your system – I don’t know whether these are installed by default, like they are on MacOS X.)

This little script draws a bar graph in a PostScript file, given a list of height values. The open command automatically converts the PostScript file to PDF format and displays the image in Preview.

(set 'ps-prolog "%!PS-Adobe-2.0
%%Title: newLISP test
%%Creator: newLISP
0.5 setlinewidth
30 30 translate
/str {10 string} def
/Helvetica findfont 8 scalefont setfont
(set 'ps-epilog " showpage
(define (bar-chart point-list)
    " bar-chart '(y1 y2 y3) ..."
    (println ps-prolog)
    (set 'x-coord 10)  ; start x
    (set 'rect-width 15) ; width of bar
    (println (format "%d %d moveto " x-coord (first point-list)))
    (dolist (y point-list)
        ; draw bar
            (format "%d %d %d %f rectstroke " x-coord 0 rect-width y ))
        ; draw label
            (format "%d %f moveto %d str cvs show " x-coord (+ y 5)  (floor y)))
        (inc 'x-coord rect-width))
    (println "stroke")
    (println ps-epilog))
(device (open "/tmp/myfile.ps" "write"))
(bar-chart (random 0 500 30)) ; example
(close (device))
(exec "open /tmp/myfile.ps") ; convert to PDF

A useful newLISP facility here is the device command. This can direct the output of the various println functions to the named device, a file in /tmp. But, you can comment out the device calls while you’re developing the script, then, when the statements look OK, remove the comments and run the file again, printing to the file instead.

(And before you all tell me that there are much better graphing tools in newLISP’s Tcl/Tk Graphical Frontend – I just haven’t had the time to look into this. Also, I used to like hacking PostScript files, so this was fun!)


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: