; This program is written by Dr. Alex Yu for illustrating that 
; the appearance of a histogram is tied to bandwidth (number of bin) 
; and range. 


(setq list1 (get-string-dialog 
               "Enter a set of scores within the parentheis:" 
               :initial "(    )"))
(setq listx (with-input-from-string (s list1) (read s)))
  (loop
   (cond
     ((not (listp listx))
      (setq list1 (get-string-dialog 
                   "Incorrect, enter numbers%
within the bracket like this: (1 2 3 4 5)" 
                   :initial "(      )"))
      (setq listx (with-input-from-string (s list1) (read s))))
     (t (return t))))


(def h (histogram listx))
(send h :location 2 18)
(send h :size 404 200)


;Slider 

(setf num-bins-slider 
      (sequence-slider-dialog (iseq 5 35)
                              :points 31
                              :title "num bins"
                              :action #'(lambda (x) 
                              (send h :num-bins x))))
  (send num-bins-slider :location 3 239)
 

(setf tic-mark-slider (sequence-slider-dialog
                   (iseq 5 30)
                 :show nil
                 :title "tic-mark"
                 :points 36
                 :action #'(lambda (x) 
                             (send h :x-axis t t x))))
  (send tic-mark-slider :location 206 239)
  


(defun change-range ()
  (setq list2 (get-string-dialog 
               "Enter the beginning and the end
of the range within the parentheis:" 
               :initial "(    )"))
  (setq listy (with-input-from-string (s list2) (read s)))
  (loop
   (cond
     ((not (listp listy))
      (setq list2 (get-string-dialog 
                   "Incorrect, enter two numbers%
within the bracket like this: (1 10)" 
                   :initial "(      )"))
      (setq listy (with-input-from-string (s list2) (read s))))
     (t (return t))))

	(send h :range 0 (first listy) (second listy))
  (send h :redraw))

;append items into the pull-down menu "histogram"
(setf pull-menu (send menu-proto :new "Change"))
    
    
(setf item2
      (send menu-item-proto :new "Quit Program"
            :action #'(lambda ()
                        (send pull-menu :remove)
                        (send tic-mark-slider :remove)
                        (send num-bins-slider :remove)
                        (send h :remove)))) 

(setf item1
      (send menu-item-proto :new "Change Range"
            :action #'(lambda ()
                        (change-range)))) 

(setf item3
      (send menu-item-proto :new "Add Data"
            :action #'(lambda ()
                        (add-data)))) 
(setf item4
      (send menu-item-proto :new "New Data"
            :action #'(lambda ()
                        (new-data)))) 
    
 
(send pull-menu :append-items item1 item2 item3 item4)
(send pull-menu :install)


(defun add-data ()
  (setq list1 (get-string-dialog 
               "Enter scores within the parentheis: " 
               :initial "(            )"))
  (setq listy (with-input-from-string (s list1) (read s)))
  (loop
   (cond
     ((not (listp listy))
      (setq list1 (get-string-dialog 
                   "Incorrect, enter numbers within the bracket like this: (15 12 13 14)" 
                   :initial "(            )"))
      (setq listy (with-input-from-string (s list1) (read s))))
     (t (return t))))
  (send h :remove)
  (def listx (append listy listx))
  (def h (histogram listx))
  (send h :location 2 18)
  (send h :size 404 200))

(defun new-data ()
  (setq list1 (get-string-dialog 
               "Enter a new set of scores within the parentheis:" 
               :initial "(    )"))
  (setq listx (with-input-from-string (s list1) (read s)))
  (loop
   (cond
     ((not (listp listx))
      (setq list1 (get-string-dialog 
                   "Incorrect, enter numbers%
within the bracket like this: (1 2 3 4 5)" 
                   :initial "(      )"))
      (setq listx (with-input-from-string (s list1) (read s))))
     (t (return t))))
   (def h (histogram listx))
  (send h :location 2 18)
  (send h :size 404 200))