| 1 | package Finance::GeniusTrader::Graphics::Scale; | ||
| 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 | |||
| 9 | =head1 Finance::GeniusTrader::Graphics::Scale | ||
| 10 | |||
| 11 | A scale converts local data (numbers) into coordinate ready to be displayed. | ||
| 12 | |||
| 13 | Can use linear scale or logarithmic ones. | ||
| 14 | |||
| 15 | linear : X = a * x + b | ||
| 16 | logarithmic : X = ln(x - b + 1) * a | ||
| 17 | |||
| 18 | =cut | ||
| 19 | sub new { | ||
| 20 | my $type = shift; | ||
| 21 | my $class = ref($type) || $type; | ||
| 22 | |||
| 23 | my $self = { "x_a" => 1, "x_b" => 0, "x_log" => 0, | ||
| 24 | "y_a" => 1, "y_b" => 0, "y_log" => 0 | ||
| 25 | }; | ||
| 26 | |||
| 27 | return bless $self, $class; | ||
| 28 | } | ||
| 29 | |||
| 30 | =head2 $s->set_vertical_linear_mapping($y1, $y2, $ty1, $ty2) | ||
| 31 | |||
| 32 | Parameterize the scale to elaborate a mapping of [$y1,$y2] => [$ty1,$ty2] | ||
| 33 | on the vertical axis. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | sub set_vertical_linear_mapping { | ||
| 37 | my ($self, $y1, $y2, $ty1, $ty2) = @_; | ||
| 38 | $self->{'y_a'} = ($y1 - $y2 != 0) ? ($ty1 - $ty2) / ($y1 - $y2) : 0; | ||
| 39 | $self->{'y_b'} = $ty1 - $y1 * $self->{'y_a'}; | ||
| 40 | $self->{'y_log'} = 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | =head2 $s->set_horizontal_linear_mapping($x1, $x2, $tx1, $tx2) | ||
| 44 | |||
| 45 | Parameterize the scale to elaborate a mapping of [$x1,$x2] => [$tx1,$tx2] | ||
| 46 | on the horizontal axis. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | sub set_horizontal_linear_mapping { | ||
| 50 | my ($self, $x1, $x2, $tx1, $tx2) = @_; | ||
| 51 | $self->{'x_a'} = ($x1 - $x2 != 0) ? ($tx1 - $tx2) / ($x1 - $x2) : 0; | ||
| 52 | $self->{'x_b'} = $tx1 - $x1 * $self->{'x_a'}; | ||
| 53 | $self->{'x_log'} = 0; | ||
| 54 | } | ||
| 55 | |||
| 56 | =head2 $s->set_vertical_logarithmic_mapping($y1, $y2, $ty1, $ty2) | ||
| 57 | |||
| 58 | Parameterize the scale to elaborate a logarithmic mapping of [$y1,$y2] => | ||
| 59 | [$ty1,$ty2] on the vertical axis. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | sub set_vertical_logarithmic_mapping { | ||
| 63 | my ($self, $y1, $y2, $ty1, $ty2) = @_; | ||
| 64 | $self->{'y_a'} = ($y2 - $y1 + 1 != 1 && $y2 - $y1 + 1 > 0) ? $ty2 / log($y2 - $y1 + 1) : 0; | ||
| 65 | # $self->{'y_a'} = ($y2 - $y1 + 1 != 1 && $y2 - $y1 + 1 != 0) ? $ty2 / log($y2 - $y1 + 1) : 0; | ||
| 66 | $self->{'y_b'} = $y1 - 1; | ||
| 67 | $self->{'y_log'} = 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 $s->set_horizontal_logarithmic_mapping($x1, $x2, $tx1, $tx2) | ||
| 71 | |||
| 72 | Parameterize the scale to elaborate a logarithmic mapping of [$x1,$x2] => | ||
| 73 | [$tx1,$tx2] on the horizontal axis. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | sub set_horizontal_logarithmic_mapping { | ||
| 77 | my ($self, $x1, $x2, $tx1, $tx2) = @_; | ||
| 78 | $self->{'x_a'} = (($x2 - $x1 + 1 != 1) && ($x2 - $x1 + 1 > 0)) ? $tx2 / log($x2 - $x1 + 1) : 0; | ||
| 79 | # $self->{'x_a'} = (($x2 - $x1 + 1 != 1) && ($x2 - $x1 + 1 != 0)) ? $tx2 / log($x2 - $x1 + 1) : 0; | ||
| 80 | $self->{'x_b'} = $x1 - 1; | ||
| 81 | $self->{'x_log'} = 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | =head2 ($nx, $ny) = $s->convert_to_coordinate($x, $y) | ||
| 86 | |||
| 87 | Returns the coordinate of the ($x, $y) point with the scale | ||
| 88 | modification applied. | ||
| 89 | |||
| 90 | =cut | ||
| 91 | sub convert_to_coordinate { | ||
| 92 | my ($self, $x, $y) = @_; | ||
| 93 | if ( ($self->{'y_log'}) && ($y <= $self->{'y_b'}) ) { | ||
| 94 | $y = $self->{'y_b'} + 0.000001; | ||
| 95 | } | ||
| 96 | return ($self->{'x_log'} ? int($self->{'x_a'} * log($x - $self->{'x_b'})) : | ||
| 97 | int($self->{'x_a'} * $x + $self->{'x_b'}), | ||
| 98 | $self->{'y_log'} ? int($self->{'y_a'} * log($y - $self->{'y_b'})) : | ||
| 99 | int($self->{'y_a'} * $y + $self->{'y_b'})); | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 $nx = $s->convert_to_x_coordinate($x) | ||
| 103 | |||
| 104 | Returns the X coordinate of the $X value with the scale | ||
| 105 | modification applied. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | sub convert_to_x_coordinate { | ||
| 109 | my ($self, $x) = @_; | ||
| 110 | return ($self->{'x_log'} ? int($self->{'x_a'} * log($x - $self->{'x_b'})) : | ||
| 111 | int($self->{'x_a'} * $x + $self->{'x_b'})); | ||
| 112 | } | ||
| 113 | |||
| 114 | =head2 $ny = $s->convert_to_y_coordinate($y) | ||
| 115 | |||
| 116 | Returns the Y coordinate of the $y value with the scale | ||
| 117 | modification applied. | ||
| 118 | |||
| 119 | =cut | ||
| 120 | sub convert_to_y_coordinate { | ||
| 121 | my ($self, $y) = @_; | ||
| 122 | if ( ($self->{'y_log'}) && ($y <= $self->{'y_b'}) ) { | ||
| 123 | $y = $self->{'y_b'} + 0.000001; | ||
| 124 | } | ||
| 125 | return ($self->{'y_log'} ? int($self->{'y_a'} * log($y - $self->{'y_b'})) : | ||
| 126 | int($self->{'y_a'} * $y + $self->{'y_b'})); | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 ($x, $y) = $s->get_value_from_coordinate($nx, $ny) | ||
| 130 | |||
| 131 | Returns the value corresponding to the given coordinate. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | sub get_value_from_coordinate { | ||
| 135 | my ($self, $x, $y) = @_; | ||
| 136 | my ($x_value, $y_value) = (0, 0); | ||
| 137 | if ($self->{'x_a'} != 0) { | ||
| 138 | $x_value = $self->{'x_log'} ? exp($x / $self->{'x_a'}) + $self->{'x_b'} : | ||
| 139 | ($x - $self->{'x_b'}) / $self->{'x_a'}; | ||
| 140 | } | ||
| 141 | if ($self->{'y_a'} != 0) { | ||
| 142 | $y_value = $self->{'y_log'} ? exp($y / $self->{'y_a'}) + $self->{'y_b'} : | ||
| 143 | ($y - $self->{'y_b'}) / $self->{'y_a'}; | ||
| 144 | } | ||
| 145 | return ($x_value, $y_value); | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 $s->copy_horizontal_scale($other) | ||
| 149 | |||
| 150 | =head2 $s->copy_vertical_scale($other) | ||
| 151 | |||
| 152 | Copy the horizontal/vertical scale defined in the $other scale object. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | sub copy_horizontal_scale { | ||
| 156 | my ($self, $other) = @_; | ||
| 157 | $self->{'x_a'} = $other->{'x_a'}; | ||
| 158 | $self->{'x_b'} = $other->{'x_b'}; | ||
| 159 | $self->{'x_log'} = $other->{'x_log'}; | ||
| 160 | } | ||
| 161 | sub copy_vertical_scale { | ||
| 162 | my ($self, $other) = @_; | ||
| 163 | $self->{'y_a'} = $other->{'y_a'}; | ||
| 164 | $self->{'y_b'} = $other->{'y_b'}; | ||
| 165 | $self->{'y_log'} = $other->{'y_log'}; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; |