1#  demo.tcl --
2#      Demo program for Plotchart
3#
4source d:/tcl-programs/plotchart/cvs-dir/plotchart.tcl
5package require Plotchart
6
7proc demo {code} {
8    .cnv delete all
9
10    .code delete 1.0 end
11    .code insert end $code
12
13    eval $code
14
15    vwait next
16}
17
18proc nextDemo {} {
19    set ::next 1
20}
21
22button .next -text "   >   " -command nextDemo
23text   .code -width 70 -height 30
24canvas .cnv  -width 600 -height 600
25grid .code .cnv -sticky news
26grid .next -
27
28# DEMO 1
29
30demo {
31set s [::Plotchart::createXYPlot .cnv {0.0 100.0 10.0} {0.0 100.0 20.0}]
32set r [::Plotchart::createRightAxis .cnv {0.0 0.1 0.01}]
33
34set xd    5.0
35set yd   20.0
36set xold  0.0
37set yold 50.0
38
39$s dataconfig series1 -colour "red"
40$s dataconfig series2 -colour "blue"
41$s dataconfig series3 -colour "magenta"
42
43for { set i 0 } { $i < 20 } { incr i } {
44   set xnew [expr {$xold+$xd}]
45   set ynew [expr {$yold+(rand()-0.5)*$yd}]
46   set ynew2 [expr {$yold+(rand()-0.5)*2.0*$yd}]
47   $s plot series1 $xnew $ynew
48   $s plot series2 $xnew $ynew2
49   $s trend series3 $xnew $ynew2
50   set xold $xnew
51   set yold $ynew
52}
53
54$s interval series2 50.0 40.0 60.0 52.0
55$s interval series2 60.0 40.0 60.0
56
57$s xtext "X-coordinate"
58$s ytext "Y-data"
59$r ytext "Right axis"
60$s title "Aha!"
61
62#
63# Some data for the right axis
64#
65$r dataconfig right -type both -symbol circle -colour green
66$r plot right 10.0 0.01
67$r plot right 30.0 0.03
68$r plot right 40.0 0.02
69}
70
71demo {
72set p [::Plotchart::createBoxplot .cnv {0 40 5} {A B C D E F}]
73
74$p plot A {0 1 2 5 7 1 4 5 0.6 5 5.5}
75$p plot C {2 2 3 6 1.5 3}
76
77$p plot E {2 3 3 4 7 8 9 9 10 10 11 11 11 14 15 17 17 20 24 29}
78}
79
80# Histograms and the like
81demo {
82set s [::Plotchart::createBarchart .cnv {A B C D E} {0.0 10.0 2.0} 2.5]
83
84$s plot series1 {1.0 4.0 6.0 1.0 7.0} red
85$s plot series2 {0.0 3.0 7.0 9.3 2.0} green
86
87$s legend series1 "Series 1"
88$s legend series2 "Series 2"
89
90$s title "Arbitrary data"
91}
92
93demo {
94set s [::Plotchart::createPiechart .cnv]
95
96$s plot {"Long names" 10 "Short names" 30 "Average" 40
97         "Ultra-short names" 5}
98
99$s explode auto
100$s explode 1
101$s title "Names - click on a segment!"
102}
103
104demo {
105set s [::Plotchart::createGanttchart .cnv "1 january 2004" \
106        "31 december 2004" 14]
107
108set from [$s task "Spring" "1 march 2004" "1 june 2004" 30]
109set to   [$s task "Summer" "1 june 2004" "1 september 2004" 10]
110$s summary "First half" $from $to
111$s connect $from $to
112$s vertline "1 jan" "1 january 2004"
113$s vertline "1 apr" "1 april 2004"
114$s vertline "1 jul" "1 july 2004"
115$s vertline "1 oct" "1 october 2004"
116$s milestone "Longest day" "21 july 2004"
117$s title "Seasons (northern hemisphere)"
118}
119
120demo {
121::Plotchart::plotconfig horizbars leftaxis font "Helvetica 10 italic"
122::Plotchart::plotconfig horizbars background outercolor steelblue3
123::Plotchart::plotconfig horizbars bottomaxis ticklength -5
124
125set s [::Plotchart::createHorizontalBarchart .cnv {0.0 10.0 2.0} \
126         {Antarctica Eurasia "The Americas" "Australia and Oceania" Ocean} 2]
127
128$s plot series1 {1.0 4.0 6.0 1.0 7.0} red left-right
129$s plot series2 {0.0 3.0 7.0 9.3 2.0} green right-left dark
130$s title "Arbitrary data"
131}
132
133# DEMO 5
134demo {
135proc cowboyhat {x y} {
136   set x1 [expr {$x/9.0}]
137   set y1 [expr {$y/9.0}]
138
139   expr { 3.0 * (1.0-($x1*$x1+$y1*$y1))*(1.0-($x1*$x1+$y1*$y1)) }
140}
141
142set xlimits {-10. 10.  10.  }
143set ylimits {-10. 10.  10.  }
144set zlimits { -5. 10.   5.  }
145
146set zmin   0.0
147set zmax   3.0
148
149set nc    51
150set dz    [expr {($zmax - $zmin) / ($nc - 1)}]
151
152set contours {}
153for {set cnt 1} {$cnt < $nc} {incr cnt} {
154    set zval [expr {$zmin + ($dz * ($cnt - 1))}]
155    lappend contours $zval
156}
157
158set chart6 [::Plotchart::create3DPlot .cnv $xlimits $ylimits $zlimits]
159::Plotchart::colorMap jet
160$chart6 title "3D Plot"
161$chart6 plotfuncont cowboyhat $contours
162}
163
164# DEMO 9
165demo {
166#
167# Wind rose diagram
168#
169set p [::Plotchart::createWindRose .cnv {30 6} 4]
170
171$p plot {5 10 0 3} red
172$p plot {10 10 10 3} blue
173
174$p title "Simple wind rose - margins need to be corrected ..."
175}
176
177demo {
178#
179# Bands in two directions
180#
181set p [::Plotchart::createXYPlot .cnv {0 10 2} {0 40 10}]
182
183$p plot data 1 10
184$p plot data 6 20
185$p plot data 9 10
186
187$p xband 15 25
188$p yband 3 5
189}
190
191demo {
192#
193# Label-dots and vertical text
194#
195
196set p [::Plotchart::createXYPlot .cnv {0 10 2} {0 40 10}]
197
198$p labeldot 3 10 "Point 1" w
199$p labeldot 6 20 "Point 2" e
200$p labeldot 9 10 "Point 3" n
201$p labeldot 9 30 "Point 4" s
202}
203
204demo {
205#
206# 3D ribbon chart
207#
208
209set p [::Plotchart::create3DRibbonChart .cnv {A B C D} {0.0 1.0 0.25} {0.0 50.0 10.0}]
210
211$p area {0.1 10.0 0.2 10.0 0.5 45.0 0.9 20.0 1.0 30.0} green
212$p line {0.2 8.0 0.5 25.0 0.7 10.0 0.9 10.0 1.0 50.0} blue
213}
214
215demo {
216    set s [::Plotchart::createHistogram .cnv {0.0 100.0 10.0} {0.0 100.0 20.0}]
217
218    $s dataconfig series1 -colour green
219
220    set xd    5.0
221    set yd   20.0
222    set xold  0.0
223    set yold 50.0
224
225    for { set i 0 } { $i < 20 } { incr i } {
226        set xnew [expr {$xold+$xd}]
227        set ynew [expr {$yold+(rand()-0.5)*$yd}]
228        $s plot series1 $xnew $ynew
229        set xold $xnew
230        set yold $ynew
231    }
232
233    $s balloonconfig -background green -rimwidth 3 -arrowsize 10 -font "Times 14"
234    $s balloon 50 50 "Here it is!"  south-east
235
236    $s balloonconfig -background red
237    $s balloonconfig -margin 10
238    $s balloon 50 100 "No, here!"  north-east
239
240    $s title "Aha!"
241}
242
243exit
244