1#! /bin/sh
2# -*- tcl -*- \
3exec tclsh "$0" ${1+"$@"}
4
5package require Tcl 8.4
6package require Tk
7package require Plotchart
8
9# plotdemos6.tcl --
10#    Test/demo program 6 for the Plotchart package
11#
12
13#
14# Main code
15# Note:
16# The extremes and the canvas sizes are chosen so that the
17# coordinate mapping is isometric!
18#
19#
20canvas .c  -background white -width 400 -height 400
21canvas .c2 -background white -width 400 -height 200
22pack   .c .c2 -fill both -side top
23
24set s [::Plotchart::createXYPlot .c {0.0 100.0 10.0} {0.0 100.0 20.0}]
25
26$s vectorconfig series1 -colour "red"   -scale 40
27$s vectorconfig series2 -colour "blue"  -scale 50 -type nautical -centred 1
28
29#
30# Cartesian
31#
32set data {1.0 0.0 0.0 1.0 0.5 0.5 -2.0 1.0}
33
34set x 30.0
35set y 20.0
36foreach {u v} $data {
37   $s vector series1 $x $y $u $v
38}
39
40#
41# Nautical
42#
43set data {1.0 0.0 1.0 45.0 2.0 90.0}
44
45set x 60.0
46set y 40.0
47foreach {length angle} $data {
48   $s vector series2 $x $y $length $angle
49}
50
51set s2 [::Plotchart::createXYPlot .c2 {0.0 100.0 10.0} {0.0 100.0 20.0}]
52
53$s2 dotconfig series1 -colour "red" -scalebyvalue 1 -scale 2.5
54$s2 dotconfig series2 -colour "magenta" -classes {0 blue 1 green 2 yellow 3 red} \
55    -scalebyvalue 0 -outline 0
56$s2 dotconfig series3 -colour "magenta" -classes {0 blue 1 green 2 yellow 3 red} \
57    -scalebyvalue 1 -scale 2.5
58
59set y1 20
60set y2 50
61set y3 80
62set x  10
63foreach value {-1.0 0.5 1.5 2.5 3.5 4.5} {
64    $s2 dot series1 $x $y1 $value
65    $s2 dot series2 $x $y2 $value
66    $s2 dot series3 $x $y3 $value
67    set x [expr {$x + 10}]
68}
69
70#
71# A more interesting vector plot: the forces in a dipole field
72#
73proc forcesDipole {x y} {
74    set xd1 51.0
75    set yd1 50.0
76    set xd2 49.0
77    set yd2 50.0
78
79    set r1p3 [expr {pow(hypot($x-$xd1,$y-$yd1),3.0)}]
80    set r2p3 [expr {pow(hypot($x-$xd2,$y-$yd2),3.0)}]
81
82    set fx [expr {($x-$xd1)/$r1p3 - ($x-$xd2)/$r2p3}]
83    set fy [expr {($y-$yd1)/$r1p3 - ($y-$yd2)/$r2p3}]
84
85    return [list $fx $fy]
86}
87
88toplevel .dipole
89canvas .dipole.c -background white -width 500 -height 500
90pack   .dipole.c -fill both -side top
91
92set s [::Plotchart::createXYPlot .dipole.c {45.0 55.0 1.0} {45.0 55.0 1.0}]
93
94$s title "Forces in a dipole field"
95
96$s vectorconfig series1 -colour "black" -scale 40 -type polar
97
98$s dotconfig dipole -colour red -scalebyvalue 0 -radius 5
99$s dot dipole 49.0 50.0 1.0
100$s dot dipole 51.0 50.0 1.0
101
102for {set y 45.25} {$y < 55.0} {set y [expr {$y+0.5}]} {
103    for {set x 45.25} {$x < 55.0} {set x [expr {$x+0.5}]} {
104        foreach {u v} [forcesDipole $x $y] {break}
105
106        # Scale the vector for better display
107
108        set angle  [expr {180.0*atan2($v,$u)/3.1415926}]
109        set length [expr {(0.5+hypot($u,$v))/(1.0+hypot($u,$v))}]
110
111        $s vector series1 $x $y $length $angle
112    }
113}
114
115#
116# Simple demonstration of an R-chart
117#
118toplevel .rchart
119canvas .rchart.c -background white -width 400 -height 200
120pack   .rchart.c -fill both -side top
121
122set s [::Plotchart::createXYPlot .rchart.c {0.0 100.0 10.0} {0.0 50.0 10.0}]
123
124$s title "R-chart (arbitrary data)"
125
126$s dataconfig series1 -colour "green"
127
128for {set x 1.0} {$x < 50.0} {set x [expr {$x+3.0}]} {
129    set y [expr {20.0 + 3.0*rand()}]
130    $s rchart series1 $x $y
131}
132
133#
134# Now some data outside the expected range
135#
136
137$s rchart series1 50.0 41.0
138$s rchart series1 52.0 42.0
139$s rchart series1 54.0 39.0
140
141#
142# And continue with the well-behaved series
143#
144for {set x 57.0} {$x < 100.0} {set x [expr {$x+3.0}]} {
145    set y [expr {20.0 + 3.0*rand()}]
146    $s rchart series1 $x $y
147}
148