1#!/usr/bin/ruby 2 3require 'tk' 4require 'tkextlib/tcllib/plotchart' 5 6############################### 7 8c1 = TkCanvas.new(:background=>'white', :width=>400, :height=>200) 9c2 = TkCanvas.new(:background=>'white', :width=>400, :height=>200) 10c3 = TkCanvas.new(:background=>'white', :width=>400, :height=>200) 11Tk.pack(c1,c2,c3, :fill=>:both, :side=>:top) 12 13h = TkToplevel.new(:title=>'h') 14hc1 = TkCanvas.new(h, :background=>'white', :width=>400, :height=>200) 15hc2 = TkCanvas.new(h, :background=>'white', :width=>400, :height=>200) 16Tk.pack(hc1,hc2, :fill=>:both, :side=>:top) 17 18v = TkToplevel.new(:title=>'v') 19vc1 = TkCanvas.new(v, :background=>'white', :width=>400, :height=>200) 20vc2 = TkCanvas.new(v, :background=>'white', :width=>400, :height=>200) 21vc3 = TkCanvas.new(v, :background=>'white', :width=>400, :height=>200) 22Tk.pack(vc1,vc2,vc3, :fill=>:both, :side=>:top) 23 24############################### 25 26s = Tk::Tcllib::Plotchart::XYPlot.new(c1, [0.0, 100.0, 10.0], 27 [0.0, 100.0, 20.0]) 28 29 30xd = 5.0 31yd = 20.0 32xold = 0.0 33yold = 50.0 34 35s.dataconfig('series1', :color=>'red') 36 37(0..19).each{|i| 38 xnew = xold + xd 39 ynew = yold + (rand() - 0.5) * yd 40 ynew2 = yold + (rand() - 0.5) * 2.0 * yd 41 s.plot('series1', xnew, ynew) 42 s.plot('series2', xnew, ynew2) 43 xold = xnew 44 yold = ynew 45} 46 47s.xtext "X-coordinate" 48s.ytext "Y-data" 49s.title "Aha!" 50 51c1.wait_visibility 52 53s.save_plot "aha.ps" 54 55############################### 56 57s = Tk::Tcllib::Plotchart::Piechart.new(c2) 58 59s.plot([ ["Long names", 10], ["Short names", 30], 60 ["Average", 40], ["Ultra-short names", 5] ]) 61 62# 63# Note: title should be shifted up 64# - distinguish a separate title area 65# 66s.title "Okay - this works" 67 68############################### 69 70s = Tk::Tcllib::Plotchart::PolarPlot.new(c3, [3.0, 1.0]) 71 720.step(359, 10){|angle| 73 rad = 1.0+Math.cos(angle*Math::PI/180.0) 74 s.plot('cardioid', rad, angle) 75} 76 77s.title "Cardioid" 78 79############################### 80 81s = Tk::Tcllib::Plotchart::Barchart.new(hc1, %w(A B C D E), 82 [0.0, 10.0, 2.0], 2) 83 84s.plot('series1', [1.0, 4.0, 6.0, 1.0, 7.0], 'red') 85s.plot('series2', [0.0, 3.0, 7.0, 9.3, 2.0], 'green') 86s.title "Arbitrary data" 87 88############################### 89 90s = Tk::Tcllib::Plotchart::Barchart.new(hc2, %w(A B C D E), 91 [0.0, 20.0, 5.0], :stacked) 92 93s.plot('series1', [1.0, 4.0, 6.0, 1.0, 7.0], 'red') 94s.plot('series2', [0.0, 3.0, 7.0, 9.3, 2.0], 'green') 95s.title "Stacked diagram" 96 97############################### 98 99s = Tk::Tcllib::Plotchart::HorizontalBarchart.new(vc1, [0.0, 10.0, 2.0], 100 %w(A B C D E), 2) 101 102s.plot('series1', [1.0, 4.0, 6.0, 1.0, 7.0], 'red') 103s.plot('series2', [0.0, 3.0, 7.0, 9.3, 2.0], 'green') 104s.title "Arbitrary data" 105 106############################### 107 108s = Tk::Tcllib::Plotchart::HorizontalBarchart.new(vc2, [0.0, 20.0, 5.0], 109 %w(A B C D E), :stacked) 110 111s.plot('series1', [1.0, 4.0, 6.0, 1.0, 7.0], 'red') 112s.plot('series2', [0.0, 3.0, 7.0, 9.3, 2.0], 'green') 113s.title "Stacked diagram" 114 115############################### 116 117s = Tk::Tcllib::Plotchart::Timechart.new(vc3, "1 january 2004", 118 "31 december 2004", 4) 119 120s.period("Spring", "1 march 2004", "1 june 2004", 'green') 121s.period("Summer", "1 june 2004", "1 september 2004", 'yellow') 122s.vertline("1 jan", "1 january 2004") 123s.vertline("1 apr", "1 april 2004") 124s.vertline("1 jul", "1 july 2004") 125s.vertline("1 oct", "1 october 2004") 126s.milestone("Longest day", "21 july 2004") 127s.title "Seasons (northern hemisphere)" 128 129############################### 130 131z = TkToplevel.new(:title=>'3D') 132 133zc1 = TkCanvas.new(z, :background=>'white', :width=>400, :height=>300) 134zc2 = TkCanvas.new(z, :background=>'white', :width=>400, :height=>250) 135Tk.pack(zc1,zc2) 136 137s = Tk::Tcllib::Plotchart::Plot3D.new(zc1, 138 [0, 10, 3], [-10, 10, 10], [0, 10, 2.5]) 139 140s.title "3D Plot" 141s.plot_function{|x, y| 142 # cowboyhat 143 x1 = x.to_f/9.0 144 y1 = y.to_f/9.0 145 3.0 * (1.0-(x1*x1+y1*y1))*(1.0-(x1*x1+y1*y1)) 146} 147 148s = Tk::Tcllib::Plotchart::Plot3D.new(zc2, 149 [0, 10, 3], [-10, 10, 10], [0, 10, 2.5]) 150s.title "3D Plot - data " 151s.colour("green", "black") 152s.plot_data([ [1.0, 2.0, 1.0, 0.0], 153 [1.1, 3.0, 1.1, -0.5], 154 [3.0, 1.0, 4.0, 5.0] ]) 155 156############################### 157 158Tk.mainloop 159