Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 2368

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>6.2 Example: Drawing Bar Charts</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node33.html#section.canvas.widgets">&lt;&lt; Prev</A></TD><TD><A href="node32.html">- Up -</A></TD><TD><A href="node35.html#section.canvas.tags">Next &gt;&gt;</A></TD></TR></TABLE><DIV id="section.canvas.barchart"><H2><A name="section.canvas.barchart">6.2 Example: Drawing Bar Charts</A></H2><P> <A name="label289"></A>As a more interesting example let us consider a program to draw bar charts. The definition of a class to display barcharts is shown in <A href="node34.html#figure.canvas.barchart">Figure&nbsp;6.1</A>. Before any item is created in the canvas by the method <CODE>bars</CODE>, the canvas widget is configured such that the scrollable region is just large enough for the barchart to be drawn. </P><P> </P><DIV id="figure.canvas.barchart"><HR><P><A name="figure.canvas.barchart"></A></P><A name="label291"></A><DL class="anonymous"><DD class="code"><CODE><SPAN class="keyword">local</SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;O=<SPAN class="keyword">if</SPAN>&nbsp;Tk<SPAN class="keyword">.</SPAN>isColor&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;o(fill:wheat)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">else</SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;o(stipple:gray50&nbsp;fill:black)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;D=10&nbsp;&nbsp;D2=2<SPAN class="keyword">*</SPAN>D&nbsp;B=10<BR><SPAN class="keyword">in</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">class</SPAN>&nbsp;<SPAN class="type">BarCanvas</SPAN>&nbsp;<SPAN class="keyword">from</SPAN><SPAN class="type">&nbsp;Tk.canvas</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">DrawBars</SPAN>(Ys&nbsp;H&nbsp;X)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">case</SPAN>&nbsp;Ys&nbsp;<SPAN class="keyword">of</SPAN>&nbsp;nil&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;<SPAN class="keyword">skip</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">[]</SPAN>&nbsp;Y<SPAN class="keyword">|</SPAN>Yr&nbsp;<SPAN class="keyword">then</SPAN>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;tk(create&nbsp;rectangle&nbsp;X&nbsp;H&nbsp;X<SPAN class="keyword">+</SPAN>D&nbsp;H<SPAN class="keyword">-</SPAN>Y<SPAN class="keyword">*</SPAN>D2&nbsp;O)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;tk(create&nbsp;text&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X&nbsp;H<SPAN class="keyword">+</SPAN>D&nbsp;text:Y&nbsp;anchor:w)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;DrawBars(Yr&nbsp;H&nbsp;X<SPAN class="keyword">+</SPAN>D2)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">configure</SPAN>(SX&nbsp;SY)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;tk(configure&nbsp;scrollregion:q(B&nbsp;<SPAN class="keyword">~</SPAN>B&nbsp;SX<SPAN class="keyword">+</SPAN>B&nbsp;SY<SPAN class="keyword">+</SPAN>B))}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">meth</SPAN>&nbsp;<SPAN class="functionname">bars</SPAN>(Ys)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WY=D2<SPAN class="keyword">*</SPAN>({Length&nbsp;Ys}<SPAN class="keyword">+</SPAN>1)&nbsp;HY=D2<SPAN class="keyword">*</SPAN>({FoldL&nbsp;Ys&nbsp;Max&nbsp;0}<SPAN class="keyword">+</SPAN>1)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">in</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;configure(WY&nbsp;HY)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<SPAN class="keyword">self</SPAN>&nbsp;DrawBars(Ys&nbsp;HY&nbsp;D)}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR>&nbsp;&nbsp;&nbsp;<SPAN class="keyword">end</SPAN>&nbsp;<BR><SPAN class="keyword">end</SPAN></CODE></DD></DL><P class="caption"><STRONG>Figure&nbsp;6.1:</STRONG> A canvas for displaying bar charts.</P><HR></DIV><P> </P><P> The method <CODE>DrawBars</CODE> creates for each element of the list <CODE>Ys</CODE> a rectangle item as well as a text item, which both correspond to the value of the particular item. The value of <CODE>O</CODE> is used as option for the rectangle items. This value depends on <CODE>Tk<SPAN class="keyword">.</SPAN>isColor</CODE> which is <CODE><SPAN class="keyword">true</SPAN></CODE> if the screen is a color screen, and <CODE><SPAN class="keyword">false</SPAN></CODE> otherwise. For a color screen the rectangle items are filled with the color <CODE>wheat</CODE>. For a black and white screen, the rectangle items are drawn in a stippled fashion: only those pixels are drawn with the fill color (that is <CODE>black</CODE>) where the stipple bitmap contains a pixel. </P><P> <A href="node34.html#figure.canvas.usebar">Figure&nbsp;6.2</A> shows how the bar chart canvas is used in order to display data. </P><P> </P><DIV id="figure.canvas.usebar"><HR><P><A name="figure.canvas.usebar"></A></P><P> </P><DIV align="center"><IMG alt="" src="barchart.gif"></DIV><P> <A name="label293"></A> </P><DL class="anonymous"><DD class="code"><CODE>C={New&nbsp;BarCanvas&nbsp;&nbsp;&nbsp;&nbsp;tkInit(parent:W&nbsp;bg:white&nbsp;width:300&nbsp;height:120)}<BR>H={New&nbsp;Tk<SPAN class="keyword">.</SPAN>scrollbar&nbsp;tkInit(parent:W&nbsp;orient:horizontal)}<BR>V={New&nbsp;Tk<SPAN class="keyword">.</SPAN>scrollbar&nbsp;tkInit(parent:W&nbsp;orient:vertical)}<BR>{Tk<SPAN class="keyword">.</SPAN>addXScrollbar&nbsp;C&nbsp;H}&nbsp;{Tk<SPAN class="keyword">.</SPAN>addYScrollbar&nbsp;C&nbsp;V}<BR>{Tk<SPAN class="keyword">.</SPAN>batch&nbsp;[grid(C&nbsp;row:0&nbsp;column:0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grid(H&nbsp;row:1&nbsp;column:0&nbsp;sticky:we)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grid(V&nbsp;row:0&nbsp;column:1&nbsp;sticky:ns)]}<BR>{C&nbsp;bars([1&nbsp;3&nbsp;4&nbsp;5&nbsp;3&nbsp;4&nbsp;2&nbsp;1&nbsp;7&nbsp;2&nbsp;3&nbsp;4&nbsp;2&nbsp;4&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5&nbsp;6&nbsp;7&nbsp;7&nbsp;8&nbsp;4&nbsp;3&nbsp;5&nbsp;6&nbsp;7&nbsp;7&nbsp;8&nbsp;4&nbsp;3])}</CODE></DD></DL><P> </P><P class="caption"><STRONG>Figure&nbsp;6.2:</STRONG> Using a canvas for drawing barcharts.</P><HR></DIV><P> </P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node33.html#section.canvas.widgets">&lt;&lt; Prev</A></TD><TD><A href="node32.html">- Up -</A></TD><TD><A href="node35.html#section.canvas.tags">Next &gt;&gt;</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~schulte/">Christian&nbsp;Schulte</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>