GeniusTrader/Graphics/Axis.pm untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 378 CC 49 SCAR 73.4
CC
49
Cov
0%
display
2256.0
set_custom_ticks
6.0
set_custom_big_ticks
6.0
CRAP 1537.6
1 package Finance::GeniusTrader::Graphics::Axis;
2  
3 # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 use strict;
8 use Finance::GeniusTrader::Graphics::Graphic;
9 use Finance::GeniusTrader::Graphics::Driver;
10 use Finance::GeniusTrader::Graphics::Scale;
11 use Finance::GeniusTrader::Graphics::Tools qw(:color);
12 use Finance::GeniusTrader::Conf;
13  
14 Finance::GeniusTrader::Conf::default("Graphic::Grid::Color", "light grey");
15  
16 =head1 Finance::GeniusTrader::Graphics::Axis
17  
18 An axis can be displayed on a side of a Zone. It's associated to a scale.
19 It precises how much space there's between ticks.
20  
21 =head2 Finance::GeniusTrader::Graphics::Axis->new($scale)
22  
23 Create a new axis and use the associated scale.
24  
25 =cut
26 sub new {
27 my $type = shift;
28 my $class = ref($type) || $type;
29 my $scale = shift;
30 my $self = { 'scale' => $scale,
31 "color" =>
32 get_color(Finance::GeniusTrader::Conf::get("Graphic::ForegroundColor")),
33 'grid_level' => 1,
34 "grid_color" =>
35 get_color(Finance::GeniusTrader::Conf::get("Graphic::Grid::Color")),
36 'label' => 1
37 };
38 return bless $self, $class;
39 }
40  
41 =head2 $a->set_{left,right,top,bottom}_side()
42  
43 Indicate that the axis is on the corresponding side of the graphic.
44  
45 =cut
46 sub set_left_side { $_[0]->{'side'} = "left"; }
47 sub set_right_side { $_[0]->{'side'} = "right"; }
48 sub set_top_side { $_[0]->{'side'} = "top"; }
49 sub set_bottom_side { $_[0]->{'side'} = "bottom"; }
50  
51 =head2 $a->set_zone($zone)
52  
53 Indicate the zone attached to the axis.
54  
55 =cut
56 sub set_zone { $_[0]->{'zone'} = $_[1] }
57  
58 =head2 $a->set_rectangle($x1, $y1, $x2, $y2)
59  
60 Indicate the rectangle in which the axis should be displayed. The
61 rectangle is defined by the lower left corner (x1,y1) and the upper
62 right corner (x2,y2).
63  
64 =cut
65 sub set_rectangle {
66 my ($self, $x1, $y1, $x2, $y2) = @_;
67 $self->{'x1'} = $x1;
68 $self->{'y1'} = $y1;
69 $self->{'x2'} = $x2;
70 $self->{'y2'} = $y2;
71 }
72  
73 =head2 $a->set_space_for(_big)_ticks
74  
75 Define the space between (big) ticks.
76  
77 =cut
78 sub set_space_for_ticks { $_[0]->{'tick'} = $_[1] }
79 sub set_space_for_big_ticks { $_[0]->{'big_tick'} = $_[1] }
80  
81 =head2 $a->set_grid_level({0|1|2})
82  
83 Indicate if a grid should be displayed :
84 - 0 => no grid
85 - 1 => grid on big ticks
86 - 2 => grid on all ticks
87  
88 =cut
89 sub set_grid_level {
90 my ($self, $level) = @_;
91 $self->{'grid_level'} = $level;
92 }
93  
94 =head2 $a->set_grid_color($color)
95  
96 Use the indicated color for the grid.
97  
98 =cut
99 sub set_grid_color {
100 my ($self, $color) = @_;
101 $self->{'grid_color'} = get_color($color);
102 }
103  
104 =head2 $a->label_display({0|1})
105  
106 Tell if labels should be displayed or not.
107  
108 =cut
109 sub label_display {
110 my ($self, $label) = @_;
111 $self->{'label'} = $label;
112 }
113  
114 =head2 $a->set_custom_ticks([[$x,$label], ...], $below_zone)
115  
116 Use the given custom ticks. If $below_zone is set to one, the labels
117 will be displayed below the zone starting at the given coordinate, otherwise
118 it will be displayed right below the given coordinate (ie below the tick).
119  
120 =cut
121 sub set_custom_ticks {
122 my ($self, $ticks, $below_zone) = @_;
123 $below_zone = 0 if (! defined($below_zone));
124 $self->{'custom_ticks'} = $ticks;
125 $self->{'custom_ticks_below_zone'} = $below_zone;
126 }
127  
128 =head2 $a->set_custom_big_ticks([[$x,$label], ...], $below_zone)
129  
130 Use the given custom big ticks. If $below_zone is set to one, the labels
131 will be displayed below the zone starting at the given coordinate, otherwise
132 it will be displayed right below the given coordinate (ie below the tick).
133  
134 =cut
135 sub set_custom_big_ticks {
136 my ($self, $ticks, $below_zone) = @_;
137 $below_zone = 0 if (! defined($below_zone));
138 $self->{'custom_big_ticks'} = $ticks;
139 $self->{'custom_big_ticks_below_zone'} = $below_zone;
140 }
141  
142 =head2 $a->display($driver, $picture)
143  
144 Display the axis on the picture.
145  
146 =cut
147 sub display {
148 my ($self, $driver, $picture) = @_;
149 my ($offset_x, $offset_y, $fixed_x, $fixed_y) = (0, 0);
150 my $scale = $self->{'scale'};
151 my $zone = $self->{'zone'};
152
153 if ($self->{'side'} eq "left") {
154 $offset_x = -1;
155 $fixed_x = $self->{'x2'};
156 } elsif ($self->{'side'} eq "right") {
157 $offset_x = 1;
158 $fixed_x = $self->{'x1'};
159 } elsif ($self->{'side'} eq "top") {
160 $offset_y = 1;
161 $fixed_y = $self->{'y1'};
162 } elsif ($self->{'side'} eq "bottom") {
163 $offset_y = -1;
164 $fixed_y = $self->{'y2'};
165 }
166
167 my ($ll_x, $ll_y) = $zone->absolute_coordinate(0,0);
168 my ($ur_x, $ur_y) = $zone->absolute_coordinate($zone->width() - 1,
169 $zone->height() - 1);
170
171 if (defined($fixed_x)) {
172 # Vertical axis
173 my ($x1, $y1) = $scale->get_value_from_coordinate(0, 0);
174 my ($x2, $y2) = $scale->get_value_from_coordinate(0,
175 $zone->height() - 1);
176 $driver->line($picture,
177 $zone->absolute_coordinate($fixed_x, 0),
178 $zone->absolute_coordinate($fixed_x, $zone->height() - 1),
179 $self->{'color'});
180 # Simple ticks
181 if ($self->{'tick'}) {
182 my $factor = int($y1 / $self->{'tick'} + 0.999);
183 while ($factor * $self->{'tick'} < $y2) {
184 my ($nx, $ny) = $zone->absolute_coordinate($fixed_x,
185 ($scale->convert_to_coordinate($x1,
186 $factor * $self->{'tick'}))[1]);
187 $driver->line($picture, $nx, $ny, $nx + $offset_x * 3, $ny,
188 $self->{'color'});
189 if ($self->{'grid_level'} >= 2) {
190 $driver->line($picture, $ll_x, $ny, $ur_x, $ny,
191 $self->{'grid_color'});
192 }
193 $factor++;
194 }
195 }
196 # Simple big ticks
197 if ($self->{'big_tick'}) {
198 my $factor = int($y1 / $self->{'big_tick'} + 0.999);
199 while ($factor * $self->{'big_tick'} < $y2) {
200 my ($nx, $ny) = $zone->absolute_coordinate($fixed_x,
201 ($scale->convert_to_coordinate($x1,
202 $factor * $self->{'big_tick'}))[1]);
203 $driver->line($picture, $nx, $ny, $nx + $offset_x * 5, $ny,
204 $self->{'color'});
205 $driver->string($picture, $FONT_ARIAL, $FONT_SIZE_TINY,
206 $self->{'color'}, $nx + $offset_x * 7, $ny,
207 sprintf("%.1f", $factor * $self->{'big_tick'}),
208 ($offset_x > 0) ? $ALIGN_LEFT : $ALIGN_RIGHT,
209 $ALIGN_CENTER) if ($self->{'label'});
210 if ($self->{'grid_level'} >= 1) {
211 $driver->line($picture, $ll_x, $ny, $ur_x, $ny,
212 $self->{'grid_color'});
213 }
214 $factor++;
215 }
216 }
217 # Custom ticks
218 if (defined($self->{'custom_ticks'})) {
219 my $c = $self->{'custom_ticks'};
220 my $b = $self->{'custom_ticks_below_zone'};
221 for(my $i = 0; $i < scalar(@{$c}); $i++) {
222 my $ly = $scale->convert_to_y_coordinate($c->[$i][0]);
223 next if (($ly < 0) || ($ly > $zone->height()));
224 my ($nx, $ny) = $zone->absolute_coordinate($fixed_x, $ly);
225 $driver->line($picture, $nx, $ny, $nx + $offset_x * 3, $ny,
226 $self->{'color'});
227 my $zone_offset = 0;
228 if ($b) {
229 if (defined($c->[$i+1])) {
230 $zone_offset = int(($scale->convert_to_y_coordinate(
231 $c->[$i+1][0]) - $ly) / 2);
232 } else {
233 $zone_offset = int(($zone->height() - $ly) / 2);
234 }
235 }
236 $driver->string($picture, $FONT_ARIAL, $FONT_SIZE_TINY,
237 $self->{'color'}, $nx + $offset_x * 5,
238 $ny + $zone_offset, $c->[$i][1],
239 ($offset_x > 0) ? $ALIGN_LEFT : $ALIGN_RIGHT,
240 $ALIGN_CENTER) if ($self->{'label'} &&
241 ($c->[$i][1] ne ""));
242 if ($self->{'grid_level'} >= 2) {
243 $driver->line($picture, $ll_x, $ny, $ur_x, $ny,
244 $self->{'grid_color'});
245 }
246 }
247 }
248 # Custom big ticks
249 if (defined($self->{'custom_big_ticks'})) {
250 my $c = $self->{'custom_big_ticks'};
251 my $b = $self->{'custom_big_ticks_below_zone'};
252 for(my $i = 0; $i < scalar(@{$c}); $i++) {
253 my $ly = $scale->convert_to_y_coordinate($c->[$i][0]);
254 next if (($ly < 0) || ($ly > $zone->height()));
255 my ($nx, $ny) = $zone->absolute_coordinate($fixed_x, $ly);
256 $driver->line($picture, $nx, $ny, $nx + $offset_x * 5, $ny,
257 $self->{'color'});
258 my $zone_offset = 0;
259 if ($b) {
260 if (defined($c->[$i+1])) {
261 $zone_offset = int(($scale->convert_to_y_coordinate(
262 $c->[$i+1][0]) - $ly) / 2);
263 } else {
264 $zone_offset = int(($zone->height() - $ly) / 2);
265 }
266 }
267 $driver->string($picture, $FONT_ARIAL, $FONT_SIZE_TINY,
268 $self->{'color'}, $nx + $offset_x * 7,
269 $ny + $zone_offset, $c->[$i][1],
270 ($offset_x > 0) ? $ALIGN_LEFT : $ALIGN_RIGHT,
271 $ALIGN_CENTER) if ($self->{'label'} &&
272 ($c->[$i][1] ne ""));
273 if ($self->{'grid_level'} >= 1) {
274 $driver->line($picture, $ll_x, $ny, $ur_x, $ny,
275 $self->{'grid_color'});
276 }
277 }
278 }
279  
280 } elsif (defined($fixed_y)) {
281 # Horizontal axis
282 my ($x1, $y1) = $scale->get_value_from_coordinate(0, 0);
283 my ($x2, $y2) = $scale->get_value_from_coordinate(
284 $zone->width() - 1, 0);
285 $driver->line($picture,
286 $zone->absolute_coordinate(0, $fixed_y),
287 $zone->absolute_coordinate( $zone->width() - 1, $fixed_y),
288 $self->{'color'});
289 if ($self->{'tick'}) {
290 my $factor = int($x1 / $self->{'tick'} + 0.999);
291 while ($factor * $self->{'tick'} < $x2) {
292 my ($nx, $ny) = $zone->absolute_coordinate(
293 ($scale->convert_to_coordinate(
294 $factor * $self->{'tick'}, $y1
295 ))[0], $fixed_y);
296 $driver->line($picture, $nx, $ny, $nx, $ny + $offset_y * 3,
297 $self->{'color'});
298 if ($self->{'grid_level'} >= 2) {
299 $driver->line($picture, $nx, $ll_y, $nx, $ur_y,
300 $self->{'grid_color'});
301 }
302 $factor++;
303 }
304 }
305 if ($self->{'big_tick'}) {
306 my $factor = int($x1 / $self->{'big_tick'} + 0.999);
307 while ($factor * $self->{'big_tick'} < $x2) {
308 my ($nx, $ny) = $zone->absolute_coordinate(
309 ($scale->convert_to_coordinate(
310 $factor * $self->{'big_tick'}, $y1)
311 )[0],
312 $fixed_y);
313 $driver->line($picture, $nx, $ny, $nx, $ny + $offset_y * 5,
314 $self->{'color'});
315 $driver->string($picture, $FONT_ARIAL, $FONT_SIZE_TINY,
316 $self->{'color'}, $nx, $ny + $offset_y * 7,
317 sprintf("%.1f", $factor * $self->{'big_tick'}),
318 $ALIGN_CENTER,
319 ($offset_y > 0) ? $ALIGN_BOTTOM : $ALIGN_TOP
320 ) if ($self->{'label'});
321 if ($self->{'grid_level'} >= 1) {
322 $driver->line($picture, $nx, $ll_y, $nx, $ur_y,
323 $self->{'grid_color'});
324 }
325 $factor++;
326 }
327 }
328 # Custom ticks
329 if (defined($self->{'custom_ticks'})) {
330 my $c = $self->{'custom_ticks'};
331 my $b = $self->{'custom_ticks_below_zone'};
332 for(my $i = 0; $i < scalar(@{$c}); $i++) {
333 my $lx = $scale->convert_to_x_coordinate($c->[$i][0]);
334 next if (($lx < 0) || ($lx > $zone->width()));
335 my ($nx, $ny) = $zone->absolute_coordinate($lx, $fixed_y);
336 $driver->line($picture, $nx, $ny, $nx, $ny + $offset_y * 3,
337 $self->{'color'});
338 my $zone_offset = 0;
339 if ($b) {
340 if (defined($c->[$i+1])) {
341 $zone_offset = int(($scale->convert_to_x_coordinate(
342 $c->[$i+1][0]) - $lx) / 2);
343 } else {
344 $zone_offset = int(($zone->width() - $lx) / 2);
345 }
346 }
347 $driver->string($picture, $FONT_ARIAL, $FONT_SIZE_TINY,
348 $self->{'color'}, $nx + $zone_offset,
349 $ny + $offset_y * 5, $c->[$i][1],
350 $ALIGN_CENTER,
351 ($offset_y > 0) ? $ALIGN_BOTTOM : $ALIGN_TOP,
352 ) if ($self->{'label'} && ($c->[$i][1] ne ""));
353 if ($self->{'grid_level'} >= 2) {
354 $driver->line($picture, $nx, $ll_y, $nx, $ur_y,
355 $self->{'grid_color'});
356 }
357 }
358 }
359 # Custom big ticks
360 if (defined($self->{'custom_big_ticks'})) {
361 my $c = $self->{'custom_big_ticks'};
362 my $b = $self->{'custom_big_ticks_below_zone'};
363 for(my $i = 0; $i < scalar(@{$c}); $i++) {
364 my $lx = $scale->convert_to_x_coordinate($c->[$i][0]);
365 next if (($lx < 0) || ($lx > $zone->width()));
366 my ($nx, $ny) = $zone->absolute_coordinate($lx, $fixed_y);
367 $driver->line($picture, $nx, $ny, $nx, $ny + $offset_y * 5,
368 $self->{'color'});
369 my $zone_offset = 0;
370 if ($b) {
371 if (defined($c->[$i+1])) {
372 $zone_offset = int(($scale->convert_to_x_coordinate(
373 $c->[$i+1][0]) - $lx) / 2);
374 } else {
375 $zone_offset = int(($zone->width() - $lx) / 2);
376 }
377 }
378 $driver->string($picture, $FONT_ARIAL, $FONT_SIZE_TINY,
379 $self->{'color'}, $nx + $zone_offset,
380 $ny + $offset_y * 7, $c->[$i][1],
381 $ALIGN_CENTER,
382 ($offset_y > 0) ? $ALIGN_BOTTOM : $ALIGN_TOP,
383 ) if ($self->{'label'} && ($c->[$i][1] ne ""));
384 if ($self->{'grid_level'} >= 1) {
385 $driver->line($picture, $nx, $ll_y, $nx, $ur_y,
386 $self->{'grid_color'});
387 }
388 }
389 }
390  
391 }
392 }
393  
394 1;