| 1 | package Finance::GeniusTrader::Graphics::Object::MountainBand; | ||
| 2 | |||
| 3 | # Copyright 2003 Oliver Bossert | ||
| 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 vars qw(@ISA); | ||
| 9 | @ISA = qw(Finance::GeniusTrader::Graphics::Object); | ||
| 10 | |||
| 11 | use Finance::GeniusTrader::Graphics::Object; | ||
| 12 | use Finance::GeniusTrader::Graphics::Driver; | ||
| 13 | use Finance::GeniusTrader::Graphics::Tools qw(:color); | ||
| 14 | use Finance::GeniusTrader::Conf; | ||
| 15 | |||
| 16 | Finance::GeniusTrader::Conf::default("Graphic::MountainBand::Color", "black"); | ||
| 17 | |||
| 18 | =head1 Finance::GeniusTrader::Graphics::Object::MountainBand | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | # | ||
| 23 | # note: implementation (see script graphic.pl) creates a datasource | ||
| 24 | # with the first argument, then creates a second datasource | ||
| 25 | # with the second argument. | ||
| 26 | # the mountainband object combines both datasources | ||
| 27 | # | ||
| 28 | sub init { | ||
| 29 | my ($self, $source2) = @_; | ||
| 30 | $self->{"source2"} = $source2; | ||
| 31 | |||
| 32 | # Default values ... | ||
| 33 | $self->{'fg_color'} = get_color(Finance::GeniusTrader::Conf::get("Graphic::MountainBand::Color")); | ||
| 34 | |||
| 35 | } | ||
| 36 | |||
| 37 | sub display { | ||
| 38 | my ($self, $driver, $picture) = @_; | ||
| 39 | my $scale = $self->get_scale(); | ||
| 40 | my $zone = $self->{'zone'}; | ||
| 41 | my ($start, $end) = $self->{'source'}->get_selected_range(); | ||
| 42 | my $space = $scale->convert_to_x_coordinate($start + 1) - | ||
| 43 | $scale->convert_to_x_coordinate($start); | ||
| 44 | |||
| 45 | # only $y_min and $y_max are significant | ||
| 46 | my ($x_min, $y_min) = $scale->get_value_from_coordinate($start, 0); | ||
| 47 | my ($x_max, $y_max) = $scale->get_value_from_coordinate($end, $zone->height-1); | ||
| 48 | |||
| 49 | # these are coordinate values of $y_min and $y_max | ||
| 50 | my $yc_min = $scale->convert_to_y_coordinate($y_min); | ||
| 51 | my $yc_max = $scale->convert_to_y_coordinate($y_max); | ||
| 52 | |||
| 53 | my ($first_pt, $second_pt); | ||
| 54 | for(my $i = $start; $i <= $end; $i++) | ||
| 55 | { | ||
| 56 | # Find two available points | ||
| 57 | if ($self->{'source'}->is_available($i) && | ||
| 58 | $self->{'source2'}->is_available($i)) { | ||
| 59 | $second_pt = $i; | ||
| 60 | } else { | ||
| 61 | next; | ||
| 62 | } | ||
| 63 | if (! defined($first_pt)) { | ||
| 64 | $first_pt = $second_pt; | ||
| 65 | next; | ||
| 66 | } | ||
| 67 | |||
| 68 | # Draw | ||
| 69 | my $data1_1 = $self->{'source'}->get($first_pt); | ||
| 70 | my $data1_2 = $self->{'source'}->get($second_pt); | ||
| 71 | my $data2_1 = $self->{'source2'}->get($first_pt); | ||
| 72 | my $data2_2 = $self->{'source2'}->get($second_pt); | ||
| 73 | |||
| 74 | my ($x1, $y1) = $scale->convert_to_coordinate($first_pt, $data1_1); | ||
| 75 | my ($x2, $y2) = $scale->convert_to_coordinate($second_pt, $data1_2); | ||
| 76 | my $y1l = $scale->convert_to_y_coordinate($data2_1); | ||
| 77 | my $y2l = $scale->convert_to_y_coordinate($data2_2); | ||
| 78 | |||
| 79 | $x1 += int($space / 2); | ||
| 80 | $x2 += int(($space-0.5) / 2); | ||
| 81 | |||
| 82 | # clip at top of zone | ||
| 83 | $y1 = $yc_max if ($y1 > $yc_max); | ||
| 84 | $y2 = $yc_max if ($y2 > $yc_max); | ||
| 85 | $y1l = $yc_max if ($y1l > $yc_max); | ||
| 86 | $y2l = $yc_max if ($y2l > $yc_max); | ||
| 87 | # clip at bottom of zone | ||
| 88 | $y1 = $yc_min if ($y1 < $yc_min); | ||
| 89 | $y2 = $yc_min if ($y2 < $yc_min); | ||
| 90 | $y1l = $yc_min if ($y1l < $yc_min); | ||
| 91 | $y2l = $yc_min if ($y2l < $yc_min); | ||
| 92 | |||
| 93 | my @points = ( | ||
| 94 | [$zone->absolute_coordinate($x1, $y1)], | ||
| 95 | [$zone->absolute_coordinate($x2, $y2)], | ||
| 96 | [$zone->absolute_coordinate($x2, $y2l)], | ||
| 97 | [$zone->absolute_coordinate($x1, $y1l)] | ||
| 98 | ); | ||
| 99 | |||
| 100 | $driver->filled_polygon($picture, $self->{'fg_color'}, @points); | ||
| 101 | |||
| 102 | # Shift the points | ||
| 103 | $first_pt = $second_pt; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; |