home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2000 February
/
PCWorld_2000-02_cd.bin
/
live
/
usr
/
X11R6
/
lib
/
X11
/
cbb
/
graphs
/
graphbal
< prev
next >
Wrap
Text File
|
1998-10-07
|
7KB
|
281 lines
#!/usr/bin/wish -f
# graphbal - Graph running balance
#
# Written by Arlindo L. Oliveira (aml@inesc.pt)
#
# Copyright (C) 1996 Arlindo L. Oliveira (aml@inesc.pt)
#
# $Id: graphbal,v 2.6 1998/08/14 14:30:25 curt Exp $
# (Log is kept at end of this file)
set data(0,0) " "
set data(0,1) " "
set cnt 1
while {[gets stdin line] >= 1} {
set data($cnt,0) [lindex $line 0]
set data($cnt,1) [lindex $line 1]
if {$data($cnt,0) == $data([expr $cnt-1],0)} {
set data([expr $cnt-1],1) $data($cnt,1)
} else {
incr cnt
}
}
proc graphData {graph row col} {
global data
return $data($row,$col)
}
proc graphCols {canvas} {
return 1
}
proc graphRows {canvas} {
global cnt
return [expr $cnt-1]
}
#
# createColumnGraph
# rows : number of data rows 1 ... rows
# cols : number of data cols 1 ... cols
# data(0,i) contains data labels
# data(i,0) contains abcissa labels
#
set barColors(1) "blue"
set barColors(2) "green"
set barColors(3) "red"
set barColors(4) "yellow"
set barColors(5) "brown"
proc graphColor {i} {
global barColors
return $barColors($i)
}
proc defGraphMargin {} {
return 60
}
proc defGraphHeight {} {
return 480
}
proc defGraphWidth {} {
return 640
}
proc defGraphWinHeight {} {
return [expr [defGraphHeight]+2*[defGraphMargin]]
}
proc defGraphWinWidth {} {
return [expr [defGraphWidth]+[defGraphMargin]+[defGraphMargin]]
}
proc cutZero {st} {
if {[string range $st 0 0] == 0} {
return [string range $st 1 2]
} else {
return $st
}
}
set months(01) Jan
set months(02) Feb
set months(03) Mar
set months(04) Apr
set months(05) May
set months(06) Jun
set months(07) Jul
set months(08) Aug
set months(09) Sep
set months(10) Oct
set months(11) Nov
set months(12) Dec
proc month {date} {
global months
return $months([string range $date 4 5])
}
proc daysBetween {first last} {
set y1 [string range $first 0 3]
set m1 [cutZero [string range $first 4 5]]
set d1 [cutZero [string range $first 6 7]]
set y2 [string range $last 0 3]
set m2 [cutZero [string range $last 4 5]]
set d2 [cutZero [string range $last 6 7]]
return [expr 365*($y2-$y1) + 30*($m2-$m1) + $d2-$d1]
}
proc createColumnGraph {graphName {canv 0}} {
global graphCnt canvas
set cols [graphCols $graphName]
set rows [graphRows $graphName]
wm withdraw .
if {$canv == 0} {
toplevel .graph$graphCnt
set canvas [canvas .graph$graphCnt.graph -width [defGraphWinWidth] \
-height [defGraphWinHeight] -bg white]
button .graph$graphCnt.dismiss -text dismiss \
-command {destroy .}
button .graph$graphCnt.print -text Print \
-command {
toplevel .m
message .m.msg -font 12x24 -text "Printing to file bal.ps"
pack .m.msg
wm geometry .m +300+300
after 2000 {destroy .m}
$canvas postscript -file bal.ps -pagewidth 19c
}
}
pack $canvas
pack .graph$graphCnt.dismiss -fill x
pack .graph$graphCnt.print -fill x
#
# Width of each bar
#
set gw [defGraphWidth]
set gh [defGraphHeight]
set gm [defGraphMargin]
$canvas create rectangle $gm $gm [expr $gm+$gw] [expr $gm+$gh] \
-fill gray80 -outline gray80
set max 0
set min 0
for {set j 1} {$j <= $cols} {incr j} {
for {set i 1} {$i <= $rows} {incr i} {
set x [graphData graphName $i $j]
if {$x < $min} {set min $x}
if {$x > $max} {set max $x}
}
}
set max_min_diff 1.0
if {$max != $min} {set max_min_diff ($max-$min)}
set yscale [expr $gh/1.0/($max_min_diff)]
set zero [expr $gh+$gm+$min/($max_min_diff)*$gh]
set nlevels [expr $rows/8]
set ndays [daysBetween [graphData $graphName 1 0] \
[graphData $graphName $rows 0]]
if {$ndays != 0} {
set xscale [expr $gw/1.0/$ndays]
} else {
set xscale [expr $gw/1.0]
}
#
# Draw axes
#
$canvas create line $gm $zero [expr $gm+$gw] $zero
$canvas create line $gm [expr $gm+$gh] $gm $gm -arrow last
for {set i -1} {$i < 20} {incr i} {
set val [expr $i*1000]
if {$val < $max && $val > $min} {
$canvas create text [expr $gm-8] [expr $zero-$val*$yscale] \
-text $val -anchor e
$canvas create line $gm [expr $zero-$val*$yscale] \
[expr $gm-5] [expr $zero-$val*$yscale]
}
}
#
# Draw a tic marking the final balance for period graphed.
#
set finbal [graphData $graphName $rows 1]
$canvas create line $gm [expr $zero-$finbal*$yscale] \
[expr $gm-5] [expr $zero-$finbal*$yscale] -fill red
set prevmt [month [graphData $graphName 1 0]]
for {set i 2} {$i <= $rows} {incr i} {
set x1 [daysBetween [graphData $graphName 1 0] \
[graphData $graphName [expr $i-1] 0]]
set x2 [daysBetween [graphData $graphName 1 0] \
[graphData $graphName $i 0]]
if {[graphData $graphName $i 1] > 0} {
set color red
} else {
set color red
}
$canvas create polygon \
[expr $gm+$x1*$xscale] \
[expr $zero-[graphData $graphName [expr $i-1] 1]*$yscale] \
[expr $gm+$x2*$xscale] \
[expr $zero-[graphData $graphName $i 1]*$yscale] \
[expr $gm+$x2*$xscale] \
$zero \
[expr $gm+$x1*$xscale] \
$zero \
-fill $color
set mt [month [graphData $graphName $i 0]]
if {$mt != $prevmt} {
$canvas create text [expr $gm+$x2*$xscale+2] [expr $gm+$gh+10] \
-anchor w -text $mt
$canvas create line [expr $gm+$x2*$xscale] [expr $gm+$gh+10] \
[expr $gm+$x2*$xscale] $zero
set prevmt $mt
}
}
}
set graphCnt 0
createColumnGraph lixo 0
# ----------------------------------------------------------------------------
# $Log: graphbal,v $
# Revision 2.6 1998/08/14 14:30:25 curt
# Patches to the graphs/graphbal script to avoid divide by zero in certain
# circumstances.
#
# Revision 2.5 1997/05/06 02:33:51 curt
# Added "require memorized".
#
# Revision 2.4 1997/05/06 02:06:20 curt
# Added patches by Ken Latta <ken@kdl.sc.scruznet.comt> to fix problems
# with the running balance graph.
#
# Revision 2.3 1996/12/13 01:25:19 curt
# Updated paths, modified to work with reports.tcl
#
# Revision 2.2 1996/07/13 02:58:33 curt
# Misc. changes.
#
# Revision 2.1 1996/02/27 05:36:12 curt
# Just stumbling around a bit with cvs ... :-(
#
# Revision 2.0 1996/02/27 04:43:22 curt
# Initial 2.0 revision. (See "Log" files for old history.)