summaryrefslogtreecommitdiff
path: root/ipl/gprogs/histo.icn
blob: 3e94ae9f8f95207629851e521e4cf6093dfd3fe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
############################################################################
#
#	File:     histo.icn
#
#	Subject:  Program to display simple histogram
#
#	Author:   Ralph E. Griswold
#
#	Date:     December 21, 2002
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  This program displays a simple histogram based on numbers provided
#  in standard input.
#
#  The following options are supported:
#
#	-s r	horizontal scale factors, default 1.0
#	-w i	bar width in pixels, default 5
#	-g i	gap between bars, default 1
#	-m	minimal; set width to 1, gap to 0.
#-	n s	name for image file, default "untitled"
#
#  Note: If there is too much input, there may not be resources to
#  open a window, and even if there is, parts may be off-screen.
#
#  The histogram is written to <name>.gif
#
#  The window is dismissed by a user q event.
#
############################################################################
#
#  Requires:  Graphics
#
############################################################################
#
#  Links:  numbers, options, wopen
#
############################################################################

link numbers
link options
link wopen

procedure main(args)
   local height, window_height, y, window_width, numbers, opts, scale
   local number, gap, bar, name

   opts := options(args, "s.w+g+m")

   scale := \opts["s"] | 1
   bar := \opts["w"] | 5
   gap := \opts["g"] | 1
   if \opts["m"] then {
      bar := 1
      gap := 0
      }
   name := \opts["n"] | "untitled"
   
   height := bar + gap

   numbers := []

   while number := read() do {
      number := numeric(number) | stop("*** nonnumeric data")
      number <:= 0		# clamp negative number to 0
      put(numbers, number)
      }

   if *numbers = 0 then stop("*** no data")

   window_height := *numbers * height + gap

   window_width := integer(scale * (max ! numbers) + 10)

   WOpen("canvas=hidden", "label=Histogram",
      "size=" || window_width || "," || window_height) |
         stop("*** cannot open window")

   y := 0

   while FillRectangle(0, y + gap, scale * get(numbers), height - gap) do
      y +:= height

   WAttrib("canvas=normal")

   until WQuit()

   WriteImage(name || ".gif")

   WClose()

   return

end