PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/external/jpgraph/docs/html/103Addinglinesandrectanglestoacanvas.html

https://github.com/wally/hamsterpaj4
HTML | 187 lines | 176 code | 0 blank | 11 comment | 0 complexity | 89b8e1ea07e9706000e5b9478ae48fed MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  2. <HTML>
  3. <HEAD>
  4. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=iso-iso-8859-1">
  5. <LINK REL="Start" HREF="index.html">
  6. <LINK REL="Contents" HREF="toc.html">
  7. <LINK REL="Prev" HREF="102Creatingasimplecanvas.html">
  8. <LINK REL="Next" HREF="104Usingacanvasscale.html">
  9. <STYLE TYPE="text/css"><!--
  10. BODY { font-family: serif }
  11. H1 { font-family: sans-serif }
  12. H2 { font-family: sans-serif }
  13. H3 { font-family: sans-serif }
  14. H4 { font-family: sans-serif }
  15. H5 { font-family: sans-serif }
  16. H6 { font-family: sans-serif }
  17. SUB { font-size: smaller }
  18. SUP { font-size: smaller }
  19. PRE { font-family: monospace }
  20. A { text-decoration: none }
  21. --></STYLE>
  22. </HEAD>
  23. <BODY>
  24. <A HREF="toc.html">Contents</A>
  25. <A HREF="102Creatingasimplecanvas.html">Previous</A>
  26. <A HREF="104Usingacanvasscale.html">Next</A>
  27. <HR NOSHADE>
  28. <H2><A NAME="10_3">10.3 Adding lines and rectangles to a canvas</A></H2>
  29. <P> A canvas also makes a good background for using standard graphic
  30. primitives, for example circles and lines. What you first have to
  31. remember is that you are (so far) working with absolute screen
  32. coordinates and secondly all drawing primitives are found in the <A href="../ref/Image.html#_C_IMAGE">
  33. Image Class</A> accessible as a property of the Graph class. So for
  34. example to draw a line between coordinate (0,0) and (100,100) you would
  35. have to add the line<DIV class="phpscript"><CODE><FONT color="#000000">
  36. <FONT color="#0000BB">&nbsp;$graph</FONT><FONT color="#007700">-&gt;</FONT><FONT
  37. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  38. Line</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">0</FONT><FONT
  39. color="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">
  40. ,</FONT><FONT color="#0000BB">100</FONT><FONT color="#007700">,</FONT><FONT
  41. color="#0000BB">100</FONT><FONT color="#007700">);</FONT><FONT color="#0000BB">
  42. </FONT></FONT></CODE></DIV></P>
  43. <P></P>
  44. <P> To your code. The following example shows some of the graphic
  45. primitives you have access to in the Image class<DIV class="phpscript">
  46. (File: canvasex02.php)
  47. <BR><CODE><FONT color="#000000"> <FONT color="#0000BB">&lt;?php
  48. <BR></FONT><FONT color="#FF8000">
  49. //&nbsp;$Id:&nbsp;canvasex02.php,v&nbsp;1.1&nbsp;2002/08/27&nbsp;20:08:57&nbsp;aditus&nbsp;Exp&nbsp;$
  50. <BR></FONT><FONT color="#007700">include&nbsp;</FONT><FONT color="#DD0000">
  51. &quot;../jpgraph.php&quot;</FONT><FONT color="#007700">;
  52. <BR>include&nbsp;</FONT><FONT color="#DD0000">&quot;../jpgraph_canvas.php&quot;</FONT><FONT
  53. color="#007700">;
  54. <BR>
  55. <BR></FONT><FONT color="#FF8000">//&nbsp;Setup&nbsp;a&nbsp;basic&nbsp;canvas&nbsp;we&nbsp;can&nbsp;work&nbsp;
  56. <BR></FONT><FONT color="#0000BB">$g&nbsp;</FONT><FONT color="#007700">=&nbsp;new&nbsp;</FONT><FONT
  57. color="#0000BB">CanvasGraph</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
  58. 400</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">200</FONT><FONT
  59. color="#007700">,</FONT><FONT color="#DD0000">'auto'</FONT><FONT color="#007700">
  60. );
  61. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  62. color="#0000BB">SetMargin</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
  63. 5</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">11</FONT><FONT
  64. color="#007700">,</FONT><FONT color="#0000BB">6</FONT><FONT color="#007700">
  65. ,</FONT><FONT color="#0000BB">11</FONT><FONT color="#007700">);
  66. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  67. color="#0000BB">SetShadow</FONT><FONT color="#007700">();
  68. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  69. color="#0000BB">SetMarginColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
  70. &quot;teal&quot;</FONT><FONT color="#007700">);
  71. <BR>
  72. <BR></FONT><FONT color="#FF8000">
  73. //&nbsp;We&nbsp;need&nbsp;to&nbsp;stroke&nbsp;the&nbsp;plotarea&nbsp;and&nbsp;margin&nbsp;before&nbsp;we&nbsp;add&nbsp;the
  74. <BR>//&nbsp;text&nbsp;since&nbsp;we&nbsp;otherwise&nbsp;would&nbsp;overwrite&nbsp;the&nbsp;text.
  75. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  76. color="#0000BB">InitFrame</FONT><FONT color="#007700">();
  77. <BR>
  78. <BR></FONT><FONT color="#FF8000">//&nbsp;Add&nbsp;a&nbsp;black&nbsp;line
  79. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  80. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  81. SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
  82. 'black'</FONT><FONT color="#007700">);
  83. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  84. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  85. Line</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">0</FONT><FONT
  86. color="#007700">,</FONT><FONT color="#0000BB">0</FONT><FONT color="#007700">
  87. ,</FONT><FONT color="#0000BB">100</FONT><FONT color="#007700">,</FONT><FONT
  88. color="#0000BB">100</FONT><FONT color="#007700">);
  89. <BR>
  90. <BR></FONT><FONT color="#FF8000">//&nbsp;..&nbsp;and&nbsp;a&nbsp;circle&nbsp;(x,y,diameter)
  91. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  92. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  93. Circle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">100</FONT><FONT
  94. color="#007700">,</FONT><FONT color="#0000BB">100</FONT><FONT color="#007700">
  95. ,</FONT><FONT color="#0000BB">50</FONT><FONT color="#007700">);
  96. <BR>
  97. <BR></FONT><FONT color="#FF8000">
  98. //&nbsp;..&nbsp;and&nbsp;a&nbsp;filled&nbsp;circle&nbsp;(x,y,diameter)
  99. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  100. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  101. SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">'red'</FONT><FONT
  102. color="#007700">);
  103. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  104. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  105. FilledCircle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
  106. 200</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">100</FONT><FONT
  107. color="#007700">,</FONT><FONT color="#0000BB">50</FONT><FONT color="#007700">
  108. );
  109. <BR>
  110. <BR></FONT><FONT color="#FF8000">//&nbsp;..&nbsp;add&nbsp;a&nbsp;rectangle
  111. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  112. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  113. SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
  114. 'green'</FONT><FONT color="#007700">);
  115. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  116. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  117. FilledRectangle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
  118. 10</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">10</FONT><FONT
  119. color="#007700">,</FONT><FONT color="#0000BB">50</FONT><FONT color="#007700">
  120. ,</FONT><FONT color="#0000BB">50</FONT><FONT color="#007700">);
  121. <BR>
  122. <BR></FONT><FONT color="#FF8000">//&nbsp;..&nbsp;add&nbsp;a&nbsp;filled&nbsp;rounded&nbsp;rectangle
  123. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  124. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  125. SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
  126. 'green'</FONT><FONT color="#007700">);
  127. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  128. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  129. FilledRoundedRectangle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
  130. 300</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">30</FONT><FONT
  131. color="#007700">,</FONT><FONT color="#0000BB">350</FONT><FONT color="#007700">
  132. ,</FONT><FONT color="#0000BB">80</FONT><FONT color="#007700">,</FONT><FONT
  133. color="#0000BB">10</FONT><FONT color="#007700">);
  134. <BR></FONT><FONT color="#FF8000">//&nbsp;..&nbsp;with&nbsp;a&nbsp;darker&nbsp;border
  135. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  136. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  137. SetColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
  138. 'darkgreen'</FONT><FONT color="#007700">);
  139. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  140. color="#0000BB">img</FONT><FONT color="#007700">-&gt;</FONT><FONT color="#0000BB">
  141. RoundedRectangle</FONT><FONT color="#007700">(</FONT><FONT color="#0000BB">
  142. 300</FONT><FONT color="#007700">,</FONT><FONT color="#0000BB">30</FONT><FONT
  143. color="#007700">,</FONT><FONT color="#0000BB">350</FONT><FONT color="#007700">
  144. ,</FONT><FONT color="#0000BB">80</FONT><FONT color="#007700">,</FONT><FONT
  145. color="#0000BB">10</FONT><FONT color="#007700">);
  146. <BR>
  147. <BR></FONT><FONT color="#FF8000">//&nbsp;Stroke&nbsp;the&nbsp;graph
  148. <BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-&gt;</FONT><FONT
  149. color="#0000BB">Stroke</FONT><FONT color="#007700">();
  150. <BR>
  151. <BR></FONT><FONT color="#0000BB">?&gt;
  152. <BR></FONT>
  153. <BR></FONT></CODE></DIV></P>
  154. <P> Pleas note the way to access these routines through the img property
  155. of the Graph class. Please also keep in mind that the coordinates are
  156. absolute.</P>
  157. <P><DIV class="example">
  158. <BR> <A href="exframes/frame_canvasex02.html" target="blank"><IMG border="0"
  159. HEIGHT="200" src="img/img/img/img/img/img/canvasex02.png" WIDTH="400"></A>
  160. <BR><B>Figure 189:</B> Example of graphic primitives <A href="exframes/frame_canvasex02.html"
  161. target="blank">[src]</A>&nbsp;
  162. <P></P>
  163. </DIV></P>
  164. <P></P>
  165. <P></P>
  166. <HR> <SMALL>
  167. <P><STRONG> A note on GD</STRONG> For those of you using GD 1.xx you
  168. might notice that the large &quot;filled circle&quot; isn't completely filled.
  169. This is because in GD 1.xx there are no low level primitives to fill an
  170. ellipse or circle so JpGraph tries to make the best out of a bad
  171. situation and manually fake a filled circle. For interest of speed
  172. JpGraph does not contain a complete (for example) Bresenham-circle fill
  173. but cheats by using some existing GD routines. This is not a perfect
  174. solution and for large filled circles like this you get some
  175. moire-patterns in the circle. If you upgrade to GD 2.x JpGraph will be
  176. able to make full use of those new existing methods and the fill will
  177. be perfect.</P>
  178. </SMALL>
  179. <HR>
  180. <P> We refer you to the class reference to find out what other graphic
  181. primitives are available for use.</P>
  182. <HR NOSHADE>
  183. <A HREF="toc.html">Contents</A>
  184. <A HREF="102Creatingasimplecanvas.html">Previous</A>
  185. <A HREF="104Usingacanvasscale.html">Next</A>
  186. </BODY>
  187. </HTML>