home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2000 February
/
PCWorld_2000-02_cd.bin
/
live
/
usr
/
X11R6
/
lib
/
X11
/
cbb
/
graphs
/
descpie
< prev
next >
Wrap
Text File
|
1998-10-07
|
7KB
|
283 lines
#!/usr/bin/wish -f
# descpie - Graph pie chart of expenses by description inside given category
#
# Written by Alexandr E. Bravo (abravo@tctube.spb.su)
# Copyright (C) 1997 Alexandr E. Bravo (abravo@tctube.spb.su)
# Based on script graphpie written by Arlindo L. Oliveira (aml@inesc.pt)
# Copyright (C) 1996 Arlindo L. Oliveira (aml@inesc.pt)
#
# $Id: descpie,v 1.1.1.1 1998/10/07 07:11:44 phil Exp $
# (Log is kept at end of this file)
if { [file exists "$env(HOME)/.cbbrc.tcl"] } {
source "$env(HOME)/.cbbrc.tcl"
}
set data(0,1) " "
set desc " "
set cnt 1
set dcnt 1
frame .check
set iter 1
frame .check.list_$iter
button .check.ok -text Ok -command {destroy .check}
pack .check.ok -side top -fill x
pack .check.list_$iter -side left -anchor n
while {[gets stdin line] >= 1 && [lindex $line 0] != "!" } {
set desc [lindex $line 0]
# This line here is different for the ones that take both pos and neg
# option add *font "-cronyx-helvetica-bold-r-normal-*-*-130-*-*-*-*-*-*"
option add *font $cbb(msg_text_font)
radiobutton .check.list_$iter.but_$cnt \
-text $desc \
-variable desc -relief flat -value $desc \
-relief flat
# -font "-cronyx-helvetica-bold-r-normal-*-*-130-*-*-*-*-*-*"
pack .check.list_$iter.but_$cnt -anchor w
if {$cnt % 20 == 0} {
incr iter
frame .check.list_$iter
pack .check.list_$iter -side left -anchor n
}
incr cnt
}
pack .check
tkwait window .check
while {[gets stdin line] >= 1 } {
if { [lindex $line 0] == $desc } {
set data($dcnt,0) [lindex $line 1]
set data($dcnt,1) [lindex $line 2]
incr dcnt
}
}
set cnt $dcnt
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]
}
#
# createPieChart
# 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(0) "darkgreen"
set barColors(1) "gray40"
set barColors(2) "IndianRed3"
set barColors(3) "blue3"
set barColors(4) "red"
set barColors(5) "chartreuse4"
set barColors(6) "SkyBlue4"
set barColors(7) "gold4"
proc graphColor {i} {
global barColors
return $barColors([expr $i % 8])
}
proc defGraphMargin {} {
return 120
}
proc defGraphHeight {} {
return 450
}
proc defGraphWidth {} {
return 450
}
proc defGraphWinHeight {} {
return [expr [defGraphHeight]+2*[defGraphMargin]]
}
proc defGraphWinWidth {} {
return [expr [defGraphWidth]+[defGraphMargin]+[defGraphMargin]]
}
proc createPieChart {graphName {canv 0}} {
global graphCnt use canvas cbb
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 gray80]
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 pie.ps"
pack .m.msg
wm geometry .m +300+300
after 2000 {destroy .m}
$canvas postscript -file pie.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]
#
# Find total
#
set total 0
for {set j 1} {$j <= $cols} {incr j} {
for {set i 1} {$i <= $rows} {incr i} {
set total [expr $total+[graphData $graphName $i 1]]
}
}
#
# Draw total
#
set x2 [expr $gm + $gw/2.0]
set total2 [expr $total]
if { $total < 0 } {
set total2 [expr -1.0*$total]
}
$canvas create text $x2 40 -fill "red" \
-font $cbb(button_font) -text "$graphName=$total2"
#
# Draw axes
#
# $canvas create rect $gm $gm [expr $gm+$gw] [expr $gm+$gh] \
# -fill gray80 -outline gray80
#
# Draw legends
#
for {set i 1} {$i <= $cols} {incr i} {
# $canvas create rect \
# [expr $gm+$gw+$gm/2] [expr $gm+40*$i] \
# [expr $gm+$gw+$gm+$gm/2] [expr $gm+40*$i+15] \
# -fill [graphColor $i]
$canvas create text \
[expr $gm+$gw+$gm/2+10] [expr $gm+40*$i+18] \
-anchor n -text [graphData $graphName 0 $i]
}
#
# Draw pie slices
#
set parcial 0
for {set i 1} {$i <= $rows} {incr i} {
set x [expr -[graphData $graphName $i 1]]
set offset 50
set tmp [$canvas create arc [expr $gm-$offset] [expr $gm-$offset] \
[expr $gm+$gw+$offset] [expr $gm+$gh+$offset] \
-extent 2 \
-start [expr 360.0*($parcial+$x/2.0)/$total] \
-tag [graphData $graphName $i 0] \
-style arc -outline gray80]
set xx [$canvas bbox [graphData $graphName $i 0]]
$canvas create arc $gm $gm [expr $gm+$gw] [expr $gm+$gh] \
-extent [expr 360.0*$x/$total] \
-start [expr 360.0*$parcial/$total] \
-fill [graphColor $i]
set parcial [expr $parcial+$x]
#
# Draw labels
#
set x1 [expr ([lindex $xx 0]+[lindex $xx 2])/2]
set y1 [expr ([lindex $xx 1]+[lindex $xx 3])/2]
$canvas delete $tmp
set s "[graphData $graphName $i 0] [format "%4.1f" [expr -(1.0*$x/$total*100)]]"
set offset 0
set sign 1
while {1} {
set a [$canvas create text $x1 [expr $y1+($offset*$sign)]\
-font $cbb(fixed_font) \
-text "$s%" \
-fill [graphColor $i]]
set x [$canvas bbox $a]
set ll [$canvas find overlap \
[lindex $x 0] [lindex $x 1] \
[lindex $x 2] [lindex $x 3]]
if {[llength $ll] == 1} {break}
$canvas delete $a
set sign [expr -$sign]
incr offset
}
# -font "-cronyx-helvetica-bold-r-normal-*-*-130-*-*-*-*-*-*" \
# set tx [expr $cols*$cw/2+($i-1)*$rs+$gm]
# set tt [graphData $graphName $i 0]
# set tt [string range $tt 0 7]
# $canvas create text $tx \
# [expr $zero+6+(($i-1) % $nlevels)*13] -anchor n \
# -text $tt
}
}
set graphCnt 0
#createPieChart lixo 0
createPieChart $desc 0
# ----------------------------------------------------------------------------
# $Log: descpie,v $
# Revision 1.1.1.1 1998/10/07 07:11:44 phil
#
#
# Revision 1.1 1998/08/14 14:24:03 curt
# Initial revision.
#