procedure barchart: draw barchart procedure setbar: set bar value on barchart procedure rebar: redraw barchart
link barchart
August 14, 1996; Gregg M. Townsend
Requires: Version 9 graphics
This file is in the public domain.
These procedures draw barcharts that can grow dynamically. barchart(win, x, y, dx, dy, sf, n, l, w, b) creates a barchart. setbar(bc, n, v) sets the value of a bar. rebar(bc, sf) redraws a barchart with a new scaling factor. ____________________________________________________________ barchart(win, x, y, dx, dy, sf, n, l, w, b) -- establish a barchart win window x,y position of base of first bar dx,dy distance to base of second bar (either dx or dy should be zero) sf scaling (pixels per unit of value, + or -, need not be integer) n number of bars l,w length (maximum) and width of one bar b logarithmic base, if bars are to be scaled logarithmically barchart() establishes structures for building a barchart. Any of the eight possible orthogonal orientations can be selected depending on the signs of dx, dy, and sf. The absolute value of sf establishes a linear scaling from barchart values to number of pixels. Scaling is handled such that a value of 1 makes the first mark on a bar and then each increment of sf lengthens the bar by one pixel. If a bar would exceed the limit then the entire chart is rescaled so that only half the range is then used. setbar(bc, n, v) - set bar n of barchart bc to represent value v It is assumed that v>0 and that bars never shrink; but they may grow. rebar(bc, sf) - redraw barchart with new scaling factor sf. sf is assumed to be of the same sign as the previous scaling factor. Example: Suppose "scores" is a list of scores ranging from 0 to 100. This code fragment dynamically draws a histogram using 21 bins. The call to barchart() specifies: The lower left-hand corner of the barchart is (10, 190). The next bar is 10 pixels to its right, which would be (20, 190). The bars grow upward, to smaller y values, so the scaling factor is negative; each score will grow its bar by 5 pixels. Each bar grows to a maximum length of 180 pixels; the width is 8. No base is given, so scaling is linear. bc := barchart(win, 10, 190, 10, 0, -5, 21, 180, 8) b := list(21, 0) # histogram bins every n := !scores do { i := n / 5 # bin (and bar) number b[i] +:= 1 # increment bin count setbar(bc, i, b[i]) # update display }