| 1 |
lars |
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="112Creatingasimplecanvas.html">
|
|
|
8 |
<LINK REL="Next" HREF="114Usingacanvasscale.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="112Creatingasimplecanvas.html">Previous</A>
|
|
|
26 |
<A HREF="114Usingacanvasscale.html">Next</A>
|
|
|
27 |
<HR NOSHADE>
|
|
|
28 |
<H2><A NAME="11_3">11.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"> $graph</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
37 |
color="#0000BB">img</FONT><FONT color="#007700">-></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"><?php
|
|
|
48 |
<BR></FONT><FONT color="#FF8000">
|
|
|
49 |
// $Id: canvasex02.php,v 1.1 2002/08/27 20:08:57 aditus Exp $
|
|
|
50 |
<BR></FONT><FONT color="#007700">include </FONT><FONT color="#DD0000">
|
|
|
51 |
"../jpgraph.php"</FONT><FONT color="#007700">;
|
|
|
52 |
<BR>include </FONT><FONT color="#DD0000">"../jpgraph_canvas.php"</FONT><FONT
|
|
|
53 |
color="#007700">;
|
|
|
54 |
<BR>
|
|
|
55 |
<BR></FONT><FONT color="#FF8000">// Setup a basic canvas we can work
|
|
|
56 |
<BR></FONT><FONT color="#0000BB">$g </FONT><FONT color="#007700">= new </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">-></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">-></FONT><FONT
|
|
|
67 |
color="#0000BB">SetShadow</FONT><FONT color="#007700">();
|
|
|
68 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
69 |
color="#0000BB">SetMarginColor</FONT><FONT color="#007700">(</FONT><FONT color="#DD0000">
|
|
|
70 |
"teal"</FONT><FONT color="#007700">);
|
|
|
71 |
<BR>
|
|
|
72 |
<BR></FONT><FONT color="#FF8000">
|
|
|
73 |
// We need to stroke the plotarea and margin before we add the
|
|
|
74 |
<BR>// text since we otherwise would overwrite the text.
|
|
|
75 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
76 |
color="#0000BB">InitFrame</FONT><FONT color="#007700">();
|
|
|
77 |
<BR>
|
|
|
78 |
<BR></FONT><FONT color="#FF8000">// Add a black line
|
|
|
79 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
80 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">-></FONT><FONT
|
|
|
84 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">// .. and a circle (x,y,diameter)
|
|
|
91 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
92 |
color="#0000BB">img</FONT><FONT color="#007700">-></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 |
// .. and a filled circle (x,y,diameter)
|
|
|
99 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
100 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">-></FONT><FONT
|
|
|
104 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">// .. add a rectangle
|
|
|
111 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
112 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">-></FONT><FONT
|
|
|
116 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">// .. add a filled rounded rectangle
|
|
|
123 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
124 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">-></FONT><FONT
|
|
|
128 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">// .. with a darker border
|
|
|
135 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
136 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">-></FONT><FONT
|
|
|
140 |
color="#0000BB">img</FONT><FONT color="#007700">-></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">// Stroke the graph
|
|
|
148 |
<BR></FONT><FONT color="#0000BB">$g</FONT><FONT color="#007700">-></FONT><FONT
|
|
|
149 |
color="#0000BB">Stroke</FONT><FONT color="#007700">();
|
|
|
150 |
<BR>
|
|
|
151 |
<BR></FONT><FONT color="#0000BB">?>
|
|
|
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/canvasex02.png" WIDTH="400"></A>
|
|
|
160 |
<BR><B>Figure 185:</B> Example of graphic primitives <A href="exframes/frame_canvasex02.html"
|
|
|
161 |
target="blank">[src]</A>
|
|
|
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 "filled circle" 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="112Creatingasimplecanvas.html">Previous</A>
|
|
|
185 |
<A HREF="114Usingacanvasscale.html">Next</A>
|
|
|
186 |
</BODY>
|
|
|
187 |
</HTML>
|