Advertisement
iPPle

Untitled

Sep 27th, 2022
2,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 1.69 KB | None | 0 0
  1.  global ready
  2.  set ready 0
  3.  proc histogram {slist wid ht} { ;# render a histogram
  4.         global ready
  5.         array set scores $slist
  6.         set nms [lsort -integer [array names scores]]
  7.         catch {destroy .h} {}
  8.         catch {destroy .b} {}
  9.         canvas .h -width $wid -height $ht -bg beige
  10.           pack .h
  11.         set nbars [array size scores] ;#how many histogram bars
  12.         set nax [expr $nbars/10] ;# axis spacing
  13.         set hwid [expr $wid/ $nbars]
  14.         set i 0
  15.         set hmax -9999999 ;# largest y value
  16.         set hmin  9999999 ;# smallest y value
  17.         while {$i<$nbars} {
  18.                 set f $scores([lindex $nms $i])
  19.                 set hmax [ expr {$f>$hmax} ? $f : $hmax]
  20.                 set hmin [ expr {$f<$hmin} ? $f : $hmin]
  21.                 incr i
  22.         }
  23.  
  24.         if {$hmax>$hmin} {
  25.         set i 0
  26.         set nay 100
  27.         while {$nay<$hmax} {
  28.                 set yp [expr $ht-0.75*$nay-20]
  29.                 .h create line  0 $yp $wid $yp -fill red
  30.                 incr nay 100
  31.         }
  32.         set nax 10
  33.         while {$i<$nbars} {
  34.                 set x [expr $hwid*$i]
  35.                 set rhs [expr $x+$hwid/2]
  36.                 set f [expr $ht-20-.75*$ht*$scores([lindex $nms $i])/$hmax]
  37.                 .h create rectangle  $x [expr $ht-20] $rhs $f -fill gray
  38.                 if {[expr $i>=$nax]} {
  39.                 .h create line  $x [expr $ht-20] $x 0 -fill red
  40.                         incr nax 10
  41.                 }
  42.                 incr i
  43.                 incr x $hwid
  44.         }
  45.         update
  46.  
  47.  #        button .b  -text "Next" -command "incr ready" ;# when ready is changed, proceeds with commands
  48.  #        pack .b
  49.         }
  50.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement