#!/usr/local/bin/wish8.5 -f
#  graphpie - Graph pie chart of expenses by category
#
#  Written by Arlindo L. Oliveira (aml@inesc.pt)
#
#  Copyright (C) 1996  Arlindo L. Oliveira (aml@inesc.pt)
#
# $Id: graphpie,v 2.3 1996/12/13 01:25:21 curt Exp $
# (Log is kept at end of this file)


proc skip {cnt} {
   global use
        
   set use($cnt) 0
}

set data(0,1) " "
set cnt 1
frame .check
set iter 1

set skipped 0

frame .check.list_$iter
button .check.ok -text Ok -command {destroy .check}
button .check.clear -text "Clear all" -command \
        {for {set ii 1} {$ii < $cnt} {incr ii} {set use($ii) 0}}
button .check.set -text "Set all" -command \
        {for {set ii 1} {$ii < $cnt} {incr ii} {set use($ii) 1}}
pack .check.ok -side top -fill x
pack .check.clear -side top -fill x
pack .check.set -side top -fill x
pack .check.list_$iter -side left -anchor n
while {[gets stdin line] >= 1} {
     set data($cnt,0) [lindex $line 0]
     set data($cnt,1) [lindex $line 1]
     
         set use($cnt) 1
# This line here is different for the ones that take both pos and neg
         if {$data($cnt,1) > 0  || \
                [string range $data($cnt,0) 0 0] == "\["} { skip $cnt }

         checkbutton .check.list_$iter.but_$cnt -variable use($cnt) \
                        -text $data($cnt,0)
         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
for {set i 1} {$i < $cnt} {incr i} {
   if {$use($i) == 0} {
       incr skipped
   } else {
       set data([expr $i-$skipped],0) $data($i,0)
       set data([expr $i-$skipped],1) $data($i,1)
   }
}

set cnt [expr $cnt-$skipped]

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) "tomato"
set barColors(3) "blue3"
set barColors(4) "red"

proc graphColor {i} {
    global barColors

    return $barColors([expr $i % 5])
}

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

    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 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 60
            
            
            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)]\
                        -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
             }
            
#            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


# ----------------------------------------------------------------------------
# $Log: graphpie,v $
# Revision 2.3  1996/12/13 01:25:21  curt
# Updated paths, modified to work with reports.tcl
#
# Revision 2.2  1996/07/13 02:58:36  curt
# Misc. changes.
#
# Revision 2.1  1996/02/27  05:36:15  curt
# Just stumbling around a bit with cvs ... :-(
#
# Revision 2.0  1996/02/27  04:43:24  curt
# Initial 2.0 revision.  (See "Log" files for old history.)
