[comp.lang.postscript] How about a Bar Chart Program?

adamb@boulder.Colorado.EDU (Adam 'Buzz' Beguelin) (03/10/88)

I was thinking about writting a postscript program to print bar charts
of data.  Before I do has anyone done this?  It seems like it would
be a handy tool.

If you have a copy of such a public domain program could you post it
or send me a copy.

Thanks.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Adam Beguelin 			Computer Science Department Box 430 
adamb@boulder.Colorado.Edu		     University of Colorado
303/492-3912				      Boulder, CO 80309-430

geof@imagen.UUCP (Geoffrey Cooper) (03/12/88)

In article <4732@sigi.Colorado.EDU>, adamb@boulder.Colorado.EDU (Adam 'Buzz' Beguelin) writes:
> I was thinking about writting a postscript program to print bar charts
> of data.  Before I do has anyone done this?  It seems like it would
> be a handy tool.

Here is one, somewhat rudimentary:

/bar
{
    0 1 index rlineto
        barwidth 0 rlineto
        0 exch neg rlineto
        closepath
    gsave fill grestore
    gsave 0 setgray stroke grestore
    newpath
}
def

/rshow
{
    dup stringwidth exch neg exch rmoveto gsave show grestore
}
def

/cshow
{
    dup stringwidth pop 2 index exch sub 2 div 0 rmoveto show pop
}
def

/bargraph
{
    /nitems exch def
    nitems array astore /data exch def

    % compute number of points total.
    /numpoints data 0 get length def
    data { length dup numpoints lt { /numpoints exch def } { pop } ifelse } forall

    % trim arrays to have that number of points.
    0 1 data length 1 sub
    {
        /i exch def
        data i   data i get 0 numpoints getinterval  put
    }
    for

    % find minimum and maximum range
    /minval 0 def
    /maxval 0 def
    data {
        { dup minval lt { dup /minval exch def } if
          dup maxval gt { dup /maxval exch def } if
          pop } forall
    } forall

    /pointsperval graph_height maxval minval sub abs div def
    /zero_offset minval 0 lt { minval neg } { 0 } ifelse def
    /spacing graph_width numpoints div def
    /barwidth spacing data length 4 mul sub dup 0 lt { pop 1 } if def
    /bar_offset 3 def
    /grayinc 1 data length div def


    % print major axes
    1 setlinewidth
    0 0 moveto 0 graph_height lineto
      0 zero_offset moveto graph_width 0 rlineto stroke
    0 setgray
    /Helvetica findfont 8 scalefont setfont

    % print hash lines
    0 graph_hlabels maxval
    {
        /i exch def
        0 i pointsperval mul moveto
        gsave
            [2 4] 2.1 setdash
            graph_width 0 rlineto stroke
        grestore
        -10 0 rmoveto
        (:) rshow
        i cvi =string cvs rshow
    }
    for

    minval 0 lt
    {
        graph_hlabels neg dup minval
        {
            /i exch def
            0 i pointsperval mul moveto
            gsave
                [2 4] 2.1 setdash
                graph_width 0 rlineto stroke
            grestore
            -10 0 rmoveto
            (:) rshow
            i cvi =string cvs rshow
        }
        for
    }
    if


    0 setlinewidth
    /curoffset 0 def
    data
    {
        /curdata exch def

        0 1 numpoints 1 sub
        {
            /i exch def

            i spacing mul curoffset add   zero_offset   moveto
            curdata i get pointsperval mul bar
        }
        for
        /curoffset curoffset bar_offset add def
        currentgray grayinc add setgray
    }
    forall

    % print horizontal labels
    0 1 numpoints 1 sub
    {
        /i exch def

        i spacing mul  zero_offset 11 sub moveto
        spacing i =string cvs cshow
    }
    for
}
def

-- 
{decwrl,sun,saber}!imagen!geof