1## -*-Tcl-*-
2 # ###################################################################
3 #
4 #  FILE: "pltimeseries.tcl"
5 #                                    created: 03/21/1998 {00:21:52 AM}
6 #                                last update: 07/01/2002 {06:11:57 PM}
7 #  Author: Vince Darley
8 #  E-mail: vince@biosgroup.com
9 #    mail: Bios Group
10 #          317 Paseo de Peralta, Santa Fe, NM 87501
11 #     www: http://www.biosgroup.com/
12 #
13 # Copyright (c) 1998-1999 Vince Darley
14 #
15 # All rights reserved.
16 # ###################################################################
17 ##
18
19package require Plplotter
20eval package require $pl_itk_package_name
21proc pltimeseries {args} {uplevel Pltimeseries $args}
22
23itcl::class Pltimeseries {
24    inherit itk::Widget
25
26    variable timespan 100
27    variable min_time 0
28    public variable max_time 100
29    public variable scroll_every 16
30    public variable y_min 9.0
31    public variable y_max 21.0
32    public variable actual_y_max -1000.0
33    public variable auto_reduce_scale 1
34    public variable auto_reduce_scale_minimum 0.75
35    public variable auto_scale_freq 0
36    public variable auto_scale_intelligently 0
37
38    variable colour_increment 1.0
39
40    itk_option define -title title Title "plot"
41    itk_option define -yname yname Yname "y"
42    itk_option define -xname xname Xname "time"
43
44    private variable _tick 0
45    private variable _ticks ""
46    private variable _points
47    private variable _replot 0
48
49    constructor {args} {}
50    destructor {}
51
52    method internal_set {var to} {
53	set $var $to
54    }
55    method initialise {}
56    method cmd {args} { uplevel 1 $itk_component(pl) cmd $args }
57    method datapoints {args}
58    method tick {} {}
59    method plotpoint {t y}
60    method plotline {t y}
61    method plotaxes {}
62    method test {}
63}
64
65itcl::body Pltimeseries::constructor {args} {
66    #
67    # Create the outermost frame to maintain geometry.
68    #
69    itk_component add shell {
70	frame $itk_interior.shell -relief ridge -bd 2
71    } {
72	keep -background -cursor -width -height
73    }
74    pack $itk_component(shell) -fill both -expand yes
75
76    itk_component add pl {
77	Plplotwin $itk_component(shell).pl
78    } {
79	usual
80    }
81
82    pack $itk_component(pl) -fill both -expand yes
83
84    ##
85    # Explicitly handle configs that may have been ignored earlier.
86    #
87    eval itk_initialize $args
88
89}
90
91itcl::body Pltimeseries::initialise {} {
92    cmd plcol0 15
93    cmd plenv $min_time $max_time $y_min $y_max 0 1
94    cmd pllab $itk_option(-xname) $itk_option(-yname) $itk_option(-title)
95    cmd plcol0 1
96}
97
98itcl::body Pltimeseries::destructor {} {
99}
100
101itcl::body Pltimeseries::tick {} {
102    incr _tick
103    lappend _ticks $_tick
104    if {$_tick <= $max_time} {
105	if {$_replot == 1 && (($auto_scale_intelligently && $min_time == 0) \
106	  || !$auto_scale_intelligently)} {
107	    set _replot 0
108	    plotaxes
109	    set l 0
110	} else {
111	    set l [expr {[llength $_ticks] -2}]
112	    if {$_replot > 0} {
113		incr _replot -1
114	    }
115	}
116	foreach trace [array names _points] {
117	    cmd plcol0 [expr {1 + int($trace * $colour_increment) % 15}]
118	    plotline [lrange $_ticks $l end] [lrange $_points($trace) $l end]
119	}
120	return
121    }
122    incr min_time $scroll_every
123    incr max_time $scroll_every
124    set i 0
125    while {[lindex [lindex $_ticks $i] 0] < $min_time} { incr i }
126    foreach trace [array names _points] {
127	set _points($trace) [lrange $_points($trace) $i end]
128    }
129    set _ticks [lrange $_ticks $i end]
130    plotaxes
131    foreach trace [array names _points] {
132	cmd plcol0 [expr {1 + int($trace * $colour_increment) % 15}]
133	plotline $_ticks $_points($trace)
134    }
135}
136
137itcl::body Pltimeseries::plotaxes {} {
138    cmd plcol0 15
139    cmd plenv $min_time $max_time $y_min $y_max 0 1
140    cmd pllab $itk_option(-xname) $itk_option(-yname) $itk_option(-title)
141    cmd plcol0 1
142}
143
144itcl::body Pltimeseries::plotpoint {t y} {
145    matrix gg f 1 = $t
146    matrix hh f 1 = $y
147    cmd plsym 1 gg hh 65
148}
149
150itcl::body Pltimeseries::plotline {t y} {
151    matrix _pt f [llength $t] = $t
152    matrix _py f [llength $y] = $y
153    cmd plline [llength $t] _pt _py
154}
155
156itcl::body Pltimeseries::datapoints {args} {
157    for {set i 0} {$i < [llength $args]} {incr i} {
158	lappend _points($i) [lindex $args $i]
159	if {$auto_scale_freq || $auto_scale_intelligently} {
160	    if {[lindex $args $i] > $actual_y_max} {
161		set actual_y_max [lindex $args $i]
162	    }
163	}
164    }
165    if {$actual_y_max > $y_max} {
166	set y_max $actual_y_max
167	if {$_replot == 0} {
168	    if {$auto_scale_intelligently} {
169		set _replot $scroll_every
170	    } else {
171		set _replot $auto_scale_freq
172	    }
173	}
174    }
175    if {$auto_scale_freq || $auto_scale_intelligently} {
176	if {$auto_reduce_scale} {
177	    if {$actual_y_max < $y_max * $auto_reduce_scale_minimum} {
178		set y_max [expr {$actual_y_max/$auto_reduce_scale_minimum}]
179		if {$_replot == 0} {
180		    if {$auto_scale_intelligently} {
181			set _replot $scroll_every
182		    } else {
183			set _replot $auto_scale_freq
184		    }
185		}
186	    }
187	}
188    }
189}
190
191itcl::body Pltimeseries::test {} {
192    for {set i 0} {$i < 10} {incr i} {
193	datapoints [expr rand()]
194	tick
195    }
196}
197
198
199
200