playing BLT

JM The following examples show the basic usage of the BLT charting capabilities.

As many of the BLT users will notice, these examples just show the tip of the iceberg, but sometimes we are looking for the minimal example for a quick startup...


Barchart basic usage

package require BLT
blt::barchart .g -title "Comparison"
pack .g
.g element create BT1 -xdata {1 2 3 4 5 6 7 8} -ydata {.76 .98 .88 .92 .96 .98 .91 .80} \
        -label {Series 1} -showvalues y

Barchart showing two series of data

package require BLT
blt::barchart .g -title "Comparison" -barmode aligned
pack .g
.g element create BT1 -xdata {1 2 3 4 5 6 7 8} -ydata {.76 .98 .88 .92 .96 .98 .91 .80} \
        -label "Series 1" -showvalues y
.g element create BT2 -xdata {1 2 3 4 5 6 7 8} -ydata {.1 .2 .3 .4 .5 .6 .7 .8} \
        -label "Series 2" -showvalues y -foreground red

Function plotting using [vector expr ...]

UK:

#!/usr/bin/wish
 
package require BLT
namespace import ::blt::*
 
proc compute args {
    catch {
        ::y expr {                      \
              ($::a * (::x ^ 3))  + ($::b * (::x ^ 2))   \
            + ($::c * ::x)        +  $::d                \
        }
    } cerr 
}
 
label .l -text "Function: ax^3 + bx^2 + cx + d"        ; table . .l 0,0
 
graph .g -width 700 -height 600 -bd 2 -relief groove   ; table . .g 1,0 -fill both
Blt_ZoomStack .g

vector ::x ; ::x seq -10 10 0.5
vector ::y
 
set items {label variable to from digits resolution tickinterval gridpos}
set scale_cfg {
    a ::a  -0.31  0.3 5 0.001 0.1   1,1
    b ::b  -1.01  1.0 5 0.002 0.5   1,2
    c ::c  -4.01  4.0 4 0.01  2.0   1,3
    d ::d -10.01 10.0 3 0.1   5.0   1,4
}
foreach $items  $scale_cfg {
    set $variable 0.0
    trace add variable $variable write compute

    set w .sc$label
    set label [ string totitle $label ]
    scale $w -label $label \
            -variable $variable -to $to -from $from \
            -digits $digits -resolution $resolution \
            -tickinterval $tickinterval \
            -bd 2 -relief groove
    table . $w $gridpos -fill y
}
 
# fill vector ::y 
compute
 
.g element create Function -xdata ::x -ydata ::y -pixels 3
.g axis configure y -min -20 -max 20
.g grid configure -hide no -dashes { 2 2 }
 
# ready

The above function plotting can include mousewheel control of the 4 scales. Add the below bind just after the table . above, and include the proc wheel before the call to compute.

In some cases, on windows, the mousewheel distance might not be 120, so you might need to change that. If it's multiples of 120, it can indicate a larger mouse twirl. Adjust as needed.

On linux, you need to bind to button 4 and 5 for mousewheel up/down. Probably should do a platform check below.

#choose either windows or linux, on linux <MouseWheel> is just ignored, and if no buttons 4/5 on windows, ditto
bind $w <MouseWheel> [list wheel %W %D] ;# for windows
bind $w <Button-4>   [list wheel %W 120]  ;# linux up just send the 120
bind $w <Button-5>   [list wheel %W -120] ;# linux down



proc wheel {w d} {
    if { $d < 0 } {
        set dir down
    } else {
        set dir up
    }
    set times [expr {   abs($d) / 120   }] ;# wheel on windows returns multiples of 120 (not sure why)
    for {set n 0} {$n < $times } {incr n} {
        tk::ScaleIncrement $w $dir little noRepeat
    }
}

See also: Tclodbc + BLT