home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / gprogs / histo.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  94 lines

  1. ############################################################################
  2. #
  3. #    File:     histo.icn
  4. #
  5. #    Subject:  Program to display simple histogram
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     November 26, 1996
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  This program displays a simple histogram based on numbers provided
  18. #  in standard input.
  19. #
  20. #  The following options are supported:
  21. #
  22. #    s r    horizontal scale factors, default 1.0
  23. #    w i    bar width in pixels, default 5
  24. #    g i    gap between bars, default 1
  25. #    m    minimal; set width to 1, gap to 0.
  26. #
  27. #  Note: If there is too much input, there may not be resources to
  28. #  open a window, and even if there is, parts may be off-screen.
  29. #
  30. #  The window is dismissed by a user q event.
  31. #
  32. ############################################################################
  33. #
  34. #  Requires:  Graphics
  35. #
  36. ############################################################################
  37. #
  38. #  Links:  numbers, options, wopen
  39. #
  40. ############################################################################
  41.  
  42. link numbers
  43. link options
  44. link wopen
  45.  
  46. procedure main(args)
  47.    local height, window_height, y, window_width, numbers, opts, scale
  48.    local number, gap, bar
  49.  
  50.    opts := options(args, "s.w+g+m")
  51.  
  52.    scale := \opts["s"] | 1
  53.    bar := \opts["w"] | 5
  54.    gap := \opts["g"] | 1
  55.    if \opts["m"] then {
  56.       bar := 1
  57.       gap := 0
  58.       }
  59.    
  60.    height := bar + gap
  61.  
  62.    numbers := []
  63.  
  64.    while number := read() do {
  65.       number := numeric(number) | stop("*** nonnumeric data")
  66.       number <:= 0        # clamp negative number to 0
  67.       put(numbers, number)
  68.       }
  69.  
  70.    if *numbers = 0 then stop("*** no data")
  71.  
  72.    window_height := *numbers * height + gap
  73.  
  74.    window_width := integer(scale * (max ! numbers) + 10)
  75.  
  76.    WOpen("canvas=hidden", "label=Histogram",
  77.       "size=" || window_width || "," || window_height) |
  78.          stop("*** cannot open window")
  79.  
  80.    y := 0
  81.  
  82.    while FillRectangle(0, y + gap, scale * get(numbers), height - gap) do
  83.       y +:= height
  84.  
  85.    WAttrib("canvas=normal")
  86.  
  87.    until WQuit()
  88.  
  89.    WClose()
  90.  
  91.    return
  92.  
  93. end
  94.