| 1 | # $File: //member/autrijus/DBIx-ReportBuilder/lib/DBIx/ReportBuilder/Graph/GD.pm $ $Author: autrijus $ | ||
| 2 | # $Revision: #10 $ $Change: 8941 $ $DateTime: 2003/11/18 18:02:06 $ | ||
| 3 | |||
| 4 | package DBIx::ReportBuilder::Graph::GD; | ||
| 5 | use base 'DBIx::ReportBuilder::Graph'; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | DBIx::ReportBuilder::Graph::GD - ReportBuilder wrapper to GD::Graph | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use constant Labels => | ||
| 20 | qw( title legend x_label y_label x_axis y_axis values label value ); | ||
| 21 | use constant Widgets => | ||
| 22 | qw( fg text label axislabel legend values accent ); | ||
| 23 | |||
| 24 | our $AUTOLOAD; | ||
| 25 | |||
| 26 | BEGIN { | ||
| 27 | no warnings 'redefine'; | ||
| 28 | # Okay, this is thoroughly evil, but totally neccessary. | ||
| 29 | # (otherwise GD::Text gets $n_space == 0); | ||
| 30 | use POSIX; | ||
| 31 | *POSIX::isgraph = sub { die if caller and caller eq 'GD::Text' }; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub new { | ||
| 35 | my ($class, %args) = @_; | ||
| 36 | |||
| 37 | delete $args{class}; | ||
| 38 | return unless %args; | ||
| 39 | |||
| 40 | # Normalize identifiers | ||
| 41 | $args{'3d'} = delete($args{threed}); | ||
| 42 | $args{'3d_shading'} = delete($args{threed_shading}); | ||
| 43 | delete $args{show_values} if $args{cumulate}; | ||
| 44 | |||
| 45 | return bless(\%args, $class); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub Plot { | ||
| 49 | my $self = shift; | ||
| 50 | my %args = (%$self, @_); | ||
| 51 | my $data = $args{data}; | ||
| 52 | my $labels = $args{labels}; | ||
| 53 | my @legends; | ||
| 54 | |||
| 55 | if ($args{legend}) { | ||
| 56 | my @fields = ( | ||
| 57 | ($args{x_axis_field}) | ||
| 58 | ? @{$labels}[1 .. $#{$labels}] | ||
| 59 | : @{shift(@$data) || []} | ||
| 60 | ); | ||
| 61 | shift @$labels unless $args{x_axis_field}; | ||
| 62 | @legends = map $self->escape($_), @fields; | ||
| 63 | } | ||
| 64 | |||
| 65 | if ($args{x_axis_field}) { | ||
| 66 | $labels = shift(@$data) || []; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | # rotate by 90 degrees | ||
| 70 | $data = [ | ||
| 71 | map { | ||
| 72 | my $y = $_; | ||
| 73 | [ map { $data->[$_][$y] } (0 .. $#{$data}) ] | ||
| 74 | } (0 .. $#{$data->[0]}) # y | ||
| 75 | ]; | ||
| 76 | } | ||
| 77 | |||
| 78 | # ensure minimal height when chart is rotated | ||
| 79 | my $min_height = @$labels * ($self->{cumulate} ? 1 : @$data) * 25; | ||
| 80 | $self->{height} = $min_height | ||
| 81 | if $self->{rotate_chart} and $self->{height} < $min_height; | ||
| 82 | $self->{height} += 50 if @legends; | ||
| 83 | |||
| 84 | my $graph = $self->make_graph or return; | ||
| 85 | $graph->set_legend(@legends) if @legends; | ||
| 86 | |||
| 87 | if ($graph->isa('GD::Graph::axestype')) { | ||
| 88 | require Chart::Math::Axis; | ||
| 89 | my $axis = Chart::Math::Axis->new; | ||
| 90 | |||
| 91 | if ($self->{cumulate}) { | ||
| 92 | require List::Util; | ||
| 93 | |||
| 94 | $axis->add_data( map { | ||
| 95 | my $y = $_; | ||
| 96 | List::Util::sum( | ||
| 97 | map { $data->[$_][$y] } (0 .. $#{$data}) | ||
| 98 | ); | ||
| 99 | } (0 .. $#{$data->[0]})); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $axis->add_data(map { @$_ } @$data); | ||
| 103 | } | ||
| 104 | |||
| 105 | $axis->add_data(int($axis->max * 1.1)) if $self->{show_values}; | ||
| 106 | $axis->include_zero; | ||
| 107 | $axis->apply_to($graph); | ||
| 108 | } | ||
| 109 | |||
| 110 | return eval { $graph->plot([ $labels, @$data ])->png }; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub make_graph { | ||
| 114 | my $self = shift; | ||
| 115 | my $graph = $self->make_gd or return; | ||
| 116 | |||
| 117 | $graph->set(%$self, title => $self->escape($self->{text})); | ||
| 118 | $graph->set(map { ("${_}clr" => 'black') } $self->Widgets); | ||
| 119 | |||
| 120 | foreach my $label ($self->Labels) { | ||
| 121 | my $font = ($self->{"${label}_font"} || 'ming') or next; | ||
| 122 | $font = ($font eq 'ming') ? 'bsmi00lp.ttf' : 'bkai00mp.ttf'; | ||
| 123 | |||
| 124 | my $method = "set_${label}_font"; | ||
| 125 | next unless $graph->can($method); | ||
| 126 | |||
| 127 | $graph->$method( | ||
| 128 | $self->ttf_file($font), | ||
| 129 | ($self->{ "${label}_fontsize" } || 10), | ||
| 130 | ); | ||
| 131 | } | ||
| 132 | |||
| 133 | return $graph; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub get_shape { | ||
| 137 | my $self = shift; | ||
| 138 | my $shape = $self->{shape} or return; | ||
| 139 | my $style = $self->{style} || ''; | ||
| 140 | |||
| 141 | return 'pie3d' if $shape eq 'pie'; | ||
| 142 | |||
| 143 | if ($shape eq 'bars') { | ||
| 144 | $shape .= '3d' if $self->{'3d'}; | ||
| 145 | return 'cylinder' if $style eq 'cylinder'; | ||
| 146 | } | ||
| 147 | elsif ($shape eq 'lines') { | ||
| 148 | $shape .= '3d' if $self->{'3d'}; | ||
| 149 | $shape .= 'points' if $style eq 'dots'; | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | die "unknown graph shape $shape"; | ||
| 153 | } | ||
| 154 | |||
| 155 | return $shape; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub make_gd { | ||
| 159 | my $self = shift; | ||
| 160 | my $shape = $self->get_shape or return; | ||
| 161 | my ($width, $height) = @{$self}{'width', 'height'}; | ||
| 162 | |||
| 163 | no strict 'refs'; | ||
| 164 | require "GD/Graph/$shape.pm"; | ||
| 165 | return "GD::Graph::$shape"->new($width, $height) | ||
| 166 | || die "Cannot make $shape.pm"; | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | 1; | ||
| 171 | |||
| 172 | __END__ |