| 1 | package SAL::Graph; | ||
| 2 | |||
| 3 | # This module is licensed under the FDL (Free Document License) | ||
| 4 | # The complete license text can be found at http://www.gnu.org/copyleft/fdl.html | ||
| 5 | # Contains excerpts from various man pages, tutorials and books on perl | ||
| 6 | # GRAPHING MODULE | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use DBI; | ||
| 10 | use Carp; | ||
| 11 | use Data::Dumper; | ||
| 12 | use GD; | ||
| 13 | use GD::Graph::lines; | ||
| 14 | use GD::Graph::bars; | ||
| 15 | use GD::Graph::linespoints; | ||
| 16 | use GD::Graph::lines3d; | ||
| 17 | use GD::Graph::bars3d; | ||
| 18 | use GD::Graph::Data; | ||
| 19 | use GD::Graph::colour qw(:colours :lists :files :convert); | ||
| 20 | |||
| 21 | |||
| 22 | BEGIN { | ||
| 23 | use Exporter (); | ||
| 24 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); | ||
| 25 | $VERSION = '3.03'; | ||
| 26 | @ISA = qw(Exporter); | ||
| 27 | @EXPORT = qw(); | ||
| 28 | %EXPORT_TAGS = (); | ||
| 29 | @EXPORT_OK = qw(); | ||
| 30 | } | ||
| 31 | our @EXPORT_OK; | ||
| 32 | |||
| 33 | END { } | ||
| 34 | |||
| 35 | =pod | ||
| 36 | |||
| 37 | =head1 Name | ||
| 38 | |||
| 39 | SAL::Graph - Graphing abstraction for SAL::DBI database objects | ||
| 40 | |||
| 41 | =head1 Synopsis | ||
| 42 | |||
| 43 | # Derived from salgraph.cgi in the samples directory | ||
| 44 | use CGI; | ||
| 45 | use SAL::DBI; | ||
| 46 | use SAL::Graph; | ||
| 47 | |||
| 48 | my $send_mime_headers = 1; | ||
| 49 | |||
| 50 | my $q = new CGI; | ||
| 51 | my $self_url = $q->script_name(); | ||
| 52 | |||
| 53 | my $graph_obj = new SAL::Graph; | ||
| 54 | my $dbo_factory = new SAL::DBI; | ||
| 55 | my $dbo_data = $dbo_factory->spawn_sqlite(':memory:'); | ||
| 56 | |||
| 57 | # Build a sample database... | ||
| 58 | my $report_query = qq[create table ReportData (dfm varchar(255), name varchar(255), purchases int(11), sort int(11))]; | ||
| 59 | $dbo_data->do($report_query); | ||
| 60 | |||
| 61 | # Obviously not optimized... | ||
| 62 | $report_query = qq[insert into ReportData values('Data Formatting Markup Tags','Customer','Purchases','0')]; | ||
| 63 | $dbo_data->do($report_query); | ||
| 64 | $report_query = qq[insert into ReportData values(' ','Morris','30','1')]; | ||
| 65 | $dbo_data->do($report_query); | ||
| 66 | $report_query = qq[insert into ReportData values(' ','Albert','22','1')]; | ||
| 67 | $dbo_data->do($report_query); | ||
| 68 | |||
| 69 | my $graph_query = 'SELECT name, purchases FROM ReportData WHERE (sort > 0) and (sort < 998) ORDER BY sort, name'; | ||
| 70 | my ($w, $h) = $dbo_data->execute($graph_query); | ||
| 71 | |||
| 72 | my @legend = qw(a b); | ||
| 73 | $graph_obj->set_legend(@legend); | ||
| 74 | $graph_obj->{image}{width} = '400'; | ||
| 75 | $graph_obj->{image}{height} = '300'; | ||
| 76 | $graph_obj->{formatting}{title} = "Customer Purchases"; | ||
| 77 | $graph_obj->{formatting}{'y_max_value'} = 50; | ||
| 78 | $graph_obj->{formatting}{'y_min_value'} = 0; | ||
| 79 | $graph_obj->{formatting}{'x_label'} = 'Customer'; | ||
| 80 | $graph_obj->{formatting}{'y_label'} = 'Purchases'; | ||
| 81 | $graph_obj->{formatting}{'y_tick_number'} = 10; | ||
| 82 | $graph_obj->{formatting}{'boxclr'} = '#EEEEFF'; | ||
| 83 | $graph_obj->{formatting}{'long_ticks'} = '1'; | ||
| 84 | $graph_obj->{formatting}{'line_types'} = [(1,3,4)]; | ||
| 85 | $graph_obj->{formatting}{'line_width'} = '2'; | ||
| 86 | $graph_obj->{formatting}{'markers'} = [(7,5,1,8,2,6)]; | ||
| 87 | $graph_obj->{type}='bars3d'; | ||
| 88 | |||
| 89 | my $graph = $graph_obj->build_graph($send_mime_headers, $dbo_data, $graph_query); | ||
| 90 | print $graph; | ||
| 91 | |||
| 92 | =head1 Eponymous Hash | ||
| 93 | |||
| 94 | This section describes some useful items in the SAL::_ eponymous hash. Arrow syntax is used here for readability, | ||
| 95 | but is not strictly required. | ||
| 96 | |||
| 97 | Note: Replace $SAL::Graph with the name of your database object... eg. $graph->{datasource} = $dbo_data | ||
| 98 | |||
| 99 | =over 1 | ||
| 100 | |||
| 101 | =item Datasource | ||
| 102 | |||
| 103 | $SAL::Graph->{datasource} should be a SAL::DBI object (currently unused. see build_graph() method.) | ||
| 104 | |||
| 105 | =item Image Attributes | ||
| 106 | |||
| 107 | $SAL::Graph->{image}{width} should be set to the desired output width. Default: 400px | ||
| 108 | $SAL::Graph->{image}{height} should be set to the desired output height. Default: 400px | ||
| 109 | |||
| 110 | =item Legend and Formatting | ||
| 111 | |||
| 112 | $SAL::Graph->{type} should be set to the GD::Graph or GD::Graph3d graph-type. (eg. linespoints, bar3d, etc) | ||
| 113 | $SAL::Graph->{legend} should be set to a list containing entries to show in the graph's legend. | ||
| 114 | $SAL::Graph->{formatting} should be a hash containing GD::Graph and/or GD::Graph3d formatting options. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | our %Graph = ( | ||
| 121 | ###################################### | ||
| 122 | 'datasource' => '', | ||
| 123 | ###################################### | ||
| 124 | 'type' => '', | ||
| 125 | ###################################### | ||
| 126 | 'legend' => [], | ||
| 127 | ###################################### | ||
| 128 | 'image' => {}, | ||
| 129 | ###################################### | ||
| 130 | 'formatting' => {}, | ||
| 131 | ###################################### | ||
| 132 | 'out' => '', | ||
| 133 | ###################################### | ||
| 134 | 'dump' => '', | ||
| 135 | ###################################### | ||
| 136 | ); | ||
| 137 | |||
| 138 | # Setup accessors via closure (from perltooc manpage) | ||
| 139 | sub _classobj { | ||
| 140 | my $obclass = shift || __PACKAGE__; | ||
| 141 | my $class = ref($obclass) || $obclass; | ||
| 142 | no strict "refs"; | ||
| 143 | return \%$class; | ||
| 144 | } | ||
| 145 | |||
| 146 | for my $datum (keys %{ _classobj() }) { | ||
| 147 | no strict "refs"; | ||
| 148 | *$datum = sub { | ||
| 149 | my $self = shift->_classobj(); | ||
| 150 | $self->{$datum} = shift if @_; | ||
| 151 | return $self->{$datum}; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | ########################################################################################################################## | ||
| 156 | # Constructors (Public) | ||
| 157 | |||
| 158 | =pod | ||
| 159 | |||
| 160 | =head1 Constructors | ||
| 161 | |||
| 162 | =head2 new() | ||
| 163 | |||
| 164 | Prepares a new Graph object. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | sub new { | ||
| 169 | my $obclass = shift || __PACKAGE__; | ||
| 170 | my $class = ref($obclass) || $obclass; | ||
| 171 | my $self = {}; | ||
| 172 | |||
| 173 | bless($self, $class); | ||
| 174 | |||
| 175 | $self->{'type'} = 'lines'; | ||
| 176 | $self->{'out'} = 'png'; | ||
| 177 | |||
| 178 | $self->{legend}->[0] = 'Legend not defined.'; | ||
| 179 | $self->{legend}->[1] = 'Legend not defined.'; | ||
| 180 | |||
| 181 | $self->{'image'}{'width'} = 400; | ||
| 182 | $self->{'image'}{'height'} = 400; | ||
| 183 | $self->{'formatting'}{'x_label'} = 'X Label'; | ||
| 184 | $self->{'formatting'}{'x_label_skip'} = 1; | ||
| 185 | $self->{'formatting'}{'x_labels_vertical'} = 1; | ||
| 186 | $self->{'formatting'}{'y_label'} = 'Y Label'; | ||
| 187 | $self->{'formatting'}{'title'} = 'Graph Title'; | ||
| 188 | $self->{'formatting'}{'box_axis'} = 1; | ||
| 189 | $self->{'formatting'}{'long_ticks'} = 0; | ||
| 190 | $self->{'formatting'}{'show_values'} = 0; | ||
| 191 | $self->{'formatting'}{'values_vertical'} = 0; | ||
| 192 | $self->{'formatting'}{'text_space'} = 8; | ||
| 193 | $self->{'formatting'}{'axis_space'} = 10; | ||
| 194 | $self->{'formatting'}{'fgclr'} = '#AAAAAA'; | ||
| 195 | $self->{'formatting'}{'boxclr'} = '#FFFFFF'; | ||
| 196 | $self->{'formatting'}{'labelclr'} = 'black'; | ||
| 197 | $self->{'formatting'}{'axislabelclr'} = 'black'; | ||
| 198 | $self->{'formatting'}{'textclr'} = 'black'; | ||
| 199 | $self->{'formatting'}{'valuesclr'} = 'black'; | ||
| 200 | $self->{'formatting'}{'shadowclr'} = 'dgray'; | ||
| 201 | $self->{'formatting'}{'shadow_depth'} = '4'; | ||
| 202 | $self->{'formatting'}{'transparent'} = 1; | ||
| 203 | |||
| 204 | my @plot_colors = ('#598F94','#980D36','#4848FF','#DDDD00'); | ||
| 205 | $self->{formatting}{'dclrs'} = \@plot_colors; | ||
| 206 | |||
| 207 | return $self; | ||
| 208 | } | ||
| 209 | |||
| 210 | ########################################################################################################################## | ||
| 211 | # Destructor (Public) | ||
| 212 | sub destruct { | ||
| 213 | my $self = shift; | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | ########################################################################################################################## | ||
| 218 | # Public Methods | ||
| 219 | |||
| 220 | =pod | ||
| 221 | |||
| 222 | =head1 Methods | ||
| 223 | |||
| 224 | =head2 $graph = build_graph($send_mime_headers, $datasource, $query, @params) | ||
| 225 | |||
| 226 | Generate a graph by running the sql $query (and @params if provided) against $datasource (a SAL::DBI object). | ||
| 227 | |||
| 228 | If you're generating a graph on the fly to be displayed on the web, set $send_mime_headers to a non-zero value. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | sub build_graph { | ||
| 233 | my ($self, $send_mime, $datasource, $query, @params) = @_; | ||
| 234 | |||
| 235 | GD::Graph::colour::add_colour('#AAAAAA'); | ||
| 236 | GD::Graph::colour::add_colour('#1F9DC2'); | ||
| 237 | |||
| 238 | my $data = new GD::Graph::Data; | ||
| 239 | |||
| 240 | if ($datasource) { | ||
| 241 | # do dbi | ||
| 242 | my ($w, $h) = $datasource->execute($query, @params); | ||
| 243 | |||
| 244 | $datasource->clean_times(0); | ||
| 245 | $datasource->short_dates(0); | ||
| 246 | |||
| 247 | for (my $record = 0; $record < $h; $record++) { | ||
| 248 | my @row = $datasource->get_row($record); | ||
| 249 | $data->add_point(@row); | ||
| 250 | } | ||
| 251 | } else { | ||
| 252 | croak("No datasource set\n"); | ||
| 253 | } | ||
| 254 | |||
| 255 | my $graph; | ||
| 256 | my $gtype = $self->{'type'}; | ||
| 257 | my $gpkg = "GD::Graph::$gtype"; | ||
| 258 | |||
| 259 | $graph = $gpkg->new($self->{image}{width}, $self->{image}{height}); | ||
| 260 | |||
| 261 | my @colour_names = GD::Graph::colour::colour_list(8); | ||
| 262 | |||
| 263 | $graph->set( %{$self->{formatting}} ) or die $graph->error; | ||
| 264 | |||
| 265 | my @legend = @{$self->{legend}}; | ||
| 266 | $graph->set_legend(@legend); | ||
| 267 | |||
| 268 | $graph->plot($data) or die $graph->error; | ||
| 269 | |||
| 270 | my $result; | ||
| 271 | |||
| 272 | # If the caller requested the mime type, add it to the results... | ||
| 273 | if ($send_mime) { | ||
| 274 | if ($self->{out} eq 'png') { | ||
| 275 | $result = "Content-type: image/png\n\n"; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | # Put the graph in the results... | ||
| 280 | if ($self->{out} eq 'png') { | ||
| 281 | $result .= $graph->gd->png; | ||
| 282 | } | ||
| 283 | |||
| 284 | # And return them | ||
| 285 | return $result; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub set_legend { | ||
| 289 | my ($self, @legend) = @_; | ||
| 290 | |||
| 291 | my $index = 0; | ||
| 292 | foreach my $entry (@legend) { | ||
| 293 | $self->{legend}[$index] = $entry; | ||
| 294 | $index++; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | =pod | ||
| 299 | |||
| 300 | =head1 Author | ||
| 301 | |||
| 302 | Scott Elcomb <psema4@gmail.com> | ||
| 303 | |||
| 304 | =head1 See Also | ||
| 305 | |||
| 306 | SAL, SAL::DBI, SAL::WebDDR, SAL::WebApplication, GD::Graph, GD::Graph3d | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | 1; |