#!/bin/sh
# route_file_reader.tcl \
exec tclsh "$0" ${1+"$@"}


lappend auto_path ./
package require Plotchart
package require Tk

toplevel .barchart_test

#--- Creating Canvas to print the plots
canvas .barchart_test.c \
    -background black \
    -width 700 \
    -height 500 \
    -borderwidth 1 

pack .barchart_test.c \
    -side top  \
    -anchor nw \
    -fill both \
    -expand 1
    



set wid          .barchart_test.c 
set title        "Bar Chart Example" 
set subtitle     "Overall sales" 
set ytitle       "Large sales" 
set ysubtitle    "Number of houses" 
set xtitle       "Vacancy" 
set xsubtitle    "Multiple points in the county" 
set xlabels      "lgl1 lgl2 lgl3 lgl4 lgl5 lgl6 lgl7" 
set series       [list {20 35 20 30 25 15 6}] 
set series_names [list "Max Vacancies"]
set colors        "blue green orange purple yellow" 


#------------------------------------------------
#--- Some general plot aperance configuration ---
#------------------------------------------------

#--- Sets the background of the plot.
::Plotchart::plotconfig vertbars title background black

#--- Setting the Main title font information
set ifont [list courier 12 bold]
::Plotchart::plotconfig vertbars  title font $ifont
::Plotchart::plotconfig vertbars  title textcolor yellow

#--- Sets the left axis font information
set ifont [list Helvetica 8 italic]
::Plotchart::plotconfig vertbars  leftaxis font $ifont
::Plotchart::plotconfig vertbars  leftaxis textcolor red
::Plotchart::plotconfig vertbars  leftaxis thickness 2
::Plotchart::plotconfig vertbars  leftaxis ticklength -5
::Plotchart::plotconfig vertbars  leftaxis color red
::Plotchart::plotconfig vertbars  leftaxis format "% 0.1f"
::Plotchart::plotstyle configure default vertbars  leftaxis   subtextfont  $ifont
::Plotchart::plotstyle configure default vertbars  leftaxis   subtextcolor red
::Plotchart::plotstyle configure default vertbars  leftaxis   usesubtext   1

::Plotchart::plotstyle configure vertbars  xyplot leftaxis   vsubtextfont  $ifont
::Plotchart::plotstyle configure vertbars  xyplot leftaxis   vsubtextcolor red
::Plotchart::plotstyle configure vertbars  xyplot leftaxis   usevsubtext 1

#--- Sets the bottom axis font information
set ifont [list Helvetica 8 italic]
::Plotchart::plotconfig vertbars  bottomaxis font $ifont
::Plotchart::plotconfig vertbars  bottomaxis textcolor red
::Plotchart::plotconfig vertbars  bottomaxis thickness 1
::Plotchart::plotconfig vertbars  bottomaxis ticklength -5
::Plotchart::plotconfig vertbars  bottomaxis color red
::Plotchart::plotconfig vertbars  bottomaxis format "% 0.1f"
::Plotchart::plotconfig vertbars  bottomaxis labeloffset 0

::Plotchart::plotstyle configure default vertbars  bottomaxis subtextcolor red
::Plotchart::plotstyle configure default vertbars  bottomaxis subtextfont  $ifont
::Plotchart::plotstyle configure default vertbars  bottomaxis usesubtext   1

# Setting Plot subtitle information
set ifont [list Helvetica 8 italic]
::Plotchart::plotconfig vertbars subtitle font $ifont
::Plotchart::plotconfig vertbars subtitle textcolor red
::Plotchart::plotconfig vertbars subtitle background black

#--- Finding the Left Axis (Correlation value) min and max to use in both axis so the plot is symetrical
set sorted_values [lsort -real [join $series]]
set min [lindex $sorted_values 0]
set max [lindex $sorted_values end]	

#--- Taking care of special case when min and max are the same
if {($min == $max) || ([expr int( ($max - $min) / 5.0 * 10000.0 ) /10000.0] == 0)} {
    if {$min != 0} {
	set min 0
    } else {
	set max [expr $min + 1.0]
    }
}
set tstep [expr int( ($max - $min) / 5.0 * 10000.0 ) /10000.0]

#---------------------------------------
#--- Preparing and creating the plot ---
#---------------------------------------

#--- Cleanup Canvas - Removes everything from the canvas before plotting 
$wid delete "all"

#--- Creating the plot in the canvas
set the_plot [::Plotchart::createBarchart $wid $xlabels "$min $max $tstep" [llength $series_names]]
$the_plot background axes black
$the_plot background plot black

#-----------------------------------------
#--- Plotting the Series
#-----------------------------------------
for {set i 0} {$i < [llength $series_names]} {incr i} {
    $the_plot plot   "[lindex $series_names $i]" "[lindex $series $i]"       [lindex $colors $i]
    $the_plot legend "[lindex $series_names $i]" "[lindex $series_names $i]"
}

#-----------------------------------------
#--- Setting the Title and Axis labels ---
#-----------------------------------------

# Setting up plot Title and SubTitle
$the_plot title "$title"
if {[string length $subtitle]  > 0} {$the_plot subtitle "$subtitle"}

# Setting up the titles and subtitles for the axes
if {[string length $xtitle]    > 0} {$the_plot xtext    "$xtitle"}
if {[string length $xsubtitle] > 0} {$the_plot xsubtext "$xsubtitle"}

if { [package vsatisfies [package present Tk] 8.6] } {
    if {[string length $ytitle]    > 0} {$the_plot vtext    "$ytitle"}
    if {[string length $ysubtitle] > 0} {$the_plot vsubtext "$ysubtitle"}
} else {
    if {[string length $ytitle]    > 0} {$the_plot ytext    "$ytitle"}
    if {[string length $ysubtitle] > 0} {$the_plot ysubtext "$ysubtitle"}
}

#--- Controlling the location of the legend
$the_plot legendconfig -position top-left

