| 1 | package Finance::GeniusTrader::Graphics::Object::Histogram; | ||
| 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 | # ras hack -- fixes clipping at zone boundaries | ||
| 8 | # -- alternate clipping marker color scheme | ||
| 9 | # $Id$ | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw(@ISA); | ||
| 13 | @ISA = qw(Finance::GeniusTrader::Graphics::Object); | ||
| 14 | |||
| 15 | use Finance::GeniusTrader::Prices; | ||
| 16 | use Finance::GeniusTrader::Graphics::Object; | ||
| 17 | use Finance::GeniusTrader::Graphics::Driver; | ||
| 18 | use Finance::GeniusTrader::Graphics::Tools qw(:color); | ||
| 19 | use Finance::GeniusTrader::Conf; | ||
| 20 | use Finance::GeniusTrader::Graphics::DataSource::GenericIndicatorResults; | ||
| 21 | |||
| 22 | Finance::GeniusTrader::Conf::default("Graphic::Histogram::Color", "yellow"); | ||
| 23 | Finance::GeniusTrader::Conf::default("Graphic::Histogram::ClipColor", "blue"); | ||
| 24 | |||
| 25 | =head1 Finance::GeniusTrader::Graphics::Object::Histogram | ||
| 26 | |||
| 27 | This graphic object displays a histogram. | ||
| 28 | |||
| 29 | used in graphic.pl it takes two arguments, an indicator and a color. | ||
| 30 | it plots vertical bars (histogram) from zone zero to data value. | ||
| 31 | |||
| 32 | the histogram default color is "yellow", but can be changed | ||
| 33 | via configuration option "Graphic::Histogram::Color" and by the | ||
| 34 | color parameter on the graphic option statement. in addition the | ||
| 35 | color can be controlled by an indicator as well (see examples). | ||
| 36 | |||
| 37 | bars will be clipped at upper and lower zone boundries. clipped bars | ||
| 38 | will display a small arrow at the clipped end. | ||
| 39 | there are two hardcoded variations that set the | ||
| 40 | color of this marker. the old way requires you to edit the file and | ||
| 41 | set the hash variable $self->{'inverse'} in the sub init method to any value. | ||
| 42 | |||
| 43 | $self->{'inverse'} = 'enable'; | ||
| 44 | |||
| 45 | in this mode the clip marker color will be the inverse of the | ||
| 46 | current color. benefits include good contrast with respect to the | ||
| 47 | histogram especially when an indicator is controlling the color. | ||
| 48 | |||
| 49 | currently the clip marker color will be either the default value | ||
| 50 | "blue" or the value set by your configuration option parameter | ||
| 51 | "Graphic::Histogram::ClipColor" | ||
| 52 | |||
| 53 | |||
| 54 | =head1 examples | ||
| 55 | |||
| 56 | typical in a graphic config file | ||
| 57 | --add=Histogram(I:MACD/3 26 52 20, brown) | ||
| 58 | --add=Histogram(I:MACD) | ||
| 59 | --add=Histogram(I:VOSC 21, [127,127,127]) | ||
| 60 | --add=Histogram(I:ADL, "dark blue") | ||
| 61 | |||
| 62 | using indicator to set historgram color: (in options file only) | ||
| 63 | Graphic::Histogram::Color Indicators::Generic::If \ | ||
| 64 | {Signals:Generic:Below {I:Prices OPEN} {I:Prices CLOSE}} green red | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub init { | ||
| 69 | my ($self, $calc) = @_; | ||
| 70 | |||
| 71 | # Default values ... | ||
| 72 | $self->{'fg_color'} = Finance::GeniusTrader::Conf::get("Graphic::Histogram::Color"); | ||
| 73 | |||
| 74 | $self->{'color_ds'} = undef; | ||
| 75 | |||
| 76 | $self->{'clip_color'} = get_color( | ||
| 77 | Finance::GeniusTrader::Conf::get("Graphic::Histogram::ClipColor")); | ||
| 78 | |||
| 79 | if (defined($calc)) { | ||
| 80 | $self->{'calc'} = $calc; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (defined($calc) && $self->{'fg_color'} =~ /^\s*(Indicators|I:)/) { | ||
| 84 | $self->{'color_ds'} = | ||
| 85 | Finance::GeniusTrader::Graphics::DataSource::GenericIndicatorResults->new( | ||
| 86 | $calc, $self->{'fg_color'}); | ||
| 87 | } else { | ||
| 88 | $self->{'fg_color'} = get_color($self->{'fg_color'}); | ||
| 89 | } | ||
| 90 | |||
| 91 | # set this (to anything) if you prefer the old foreground color inversion | ||
| 92 | $self->{'inverse'} = ''; | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 $hist->set_color_datasource($ds) | ||
| 96 | |||
| 97 | Use the indicated datasource to retrieve the color of the bar. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | sub set_color_datasource { | ||
| 101 | my ($self, $color_ds) = @_; | ||
| 102 | $self->{'color_ds'} = $color_ds; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub display { | ||
| 106 | my ($self, $driver, $picture) = @_; | ||
| 107 | my $scale = $self->get_scale(); | ||
| 108 | my $zone = $self->{'zone'}; | ||
| 109 | my $color = $self->{'fg_color'}; | ||
| 110 | my ($start, $end) = $self->{'source'}->get_selected_range(); | ||
| 111 | my $space = $scale->convert_to_x_coordinate($start + 1) - | ||
| 112 | $scale->convert_to_x_coordinate($start); | ||
| 113 | $space = 2 if ($space < 2); | ||
| 114 | |||
| 115 | my $yc_zero = $scale->convert_to_y_coordinate(0); | ||
| 116 | |||
| 117 | # only $y_min and $y_max are significant | ||
| 118 | my ($x_min, $y_min) = $scale->get_value_from_coordinate($start, 0); | ||
| 119 | my ($x_max, $y_max) = | ||
| 120 | $scale->get_value_from_coordinate($end, $zone->height-1); | ||
| 121 | |||
| 122 | # these are coordinate values of $y_min and $y_max | ||
| 123 | my $yc_min = $scale->convert_to_y_coordinate($y_min); | ||
| 124 | my $yc_max = $scale->convert_to_y_coordinate($y_max); | ||
| 125 | |||
| 126 | # clip at zone boundary | ||
| 127 | if ( $yc_zero < $yc_min ) { | ||
| 128 | $yc_zero = $yc_min; | ||
| 129 | } elsif ( $yc_zero > $yc_max ) { | ||
| 130 | $yc_zero = $yc_max; | ||
| 131 | } | ||
| 132 | # mark clip at zone top | ||
| 133 | |||
| 134 | my $tooshort = 0; # 1 for top clip, -1 for bottom clip | ||
| 135 | for(my $i = $start; $i <= $end; $i++) | ||
| 136 | { | ||
| 137 | |||
| 138 | next if (! $self->{'source'}->is_available($i)); | ||
| 139 | my @data = $self->{'source'}->get($i); | ||
| 140 | my $y = $scale->convert_to_y_coordinate($data[0]); | ||
| 141 | my $x = $scale->convert_to_x_coordinate($i); | ||
| 142 | |||
| 143 | if ($self->{'fg_color'} =~ /^\s*(Indicators|I:)/ | ||
| 144 | && defined($self->{'color_ds'})) { | ||
| 145 | $color = get_color($self->{'color_ds'}->get($i)); | ||
| 146 | |||
| 147 | #print STDERR ">>> @$color\n"; | ||
| 148 | } | ||
| 149 | |||
| 150 | if ($y > $yc_max) { | ||
| 151 | $y = $yc_max; | ||
| 152 | $tooshort = 1; | ||
| 153 | # mark clip at zone bottom | ||
| 154 | } elsif ($y < $yc_min) { | ||
| 155 | $y = $yc_min; | ||
| 156 | $tooshort = -1; | ||
| 157 | } else { | ||
| 158 | $tooshort = 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | if ($y > $yc_zero) { | ||
| 162 | $driver->filled_rectangle($picture, | ||
| 163 | $zone->absolute_coordinate($x, $yc_zero), | ||
| 164 | $zone->absolute_coordinate($x + $space - 2, $y), | ||
| 165 | $color); | ||
| 166 | } else { | ||
| 167 | $driver->filled_rectangle($picture, | ||
| 168 | $zone->absolute_coordinate($x, $y), | ||
| 169 | $zone->absolute_coordinate($x + $space - 2, $yc_zero), | ||
| 170 | $color); | ||
| 171 | } | ||
| 172 | # | ||
| 173 | # this puts a ^/v marker at top/bot of bar if it is clipped | ||
| 174 | # | ||
| 175 | if ( $tooshort != 0 ) { | ||
| 176 | my $inverse = []; | ||
| 177 | my @points = (); | ||
| 178 | if ( ! $self->{'inverse'} ) { | ||
| 179 | $inverse = $self->{'clip_color'}; | ||
| 180 | } else { | ||
| 181 | foreach ( 0..$#$color ) { | ||
| 182 | $inverse->[$_] = 255 - $color->[$_] | ||
| 183 | unless ($_ > 2); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | if ( $tooshort > 0 ) { | ||
| 187 | @points = ( | ||
| 188 | [$zone->absolute_coordinate($x + int($space / 2) - 1, $y)], | ||
| 189 | [$zone->absolute_coordinate($x, $y - $space)], | ||
| 190 | [$zone->absolute_coordinate($x + $space - 2, $y - $space)] | ||
| 191 | ); | ||
| 192 | } else { | ||
| 193 | @points = ( | ||
| 194 | [$zone->absolute_coordinate($x + int($space / 2) - 1, $y)], | ||
| 195 | [$zone->absolute_coordinate($x, $y + $space)], | ||
| 196 | [$zone->absolute_coordinate($x + $space - 2, $y + $space)] | ||
| 197 | ); | ||
| 198 | } | ||
| 199 | $driver->filled_polygon($picture, $inverse, @points); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | sub set_foreground_color { | ||
| 206 | my ($self, $color) = @_; | ||
| 207 | if ( $self->{'calc'} ne "" && $color =~ /^\s*(Indicators|I:)/ ) { | ||
| 208 | $self->{'fg_color'} = $color; | ||
| 209 | $self->{'color_ds'} = | ||
| 210 | Finance::GeniusTrader::Graphics::DataSource::GenericIndicatorResults->new($self->{'calc'}, $color ); | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | $self->{'fg_color'} = get_color($color); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | 1; |