
SAL::Graph - Graphing abstraction for SAL::DBI database objects

# Derived from salgraph.cgi in the samples directory
use CGI;
use SAL::DBI;
use SAL::Graph;
my $send_mime_headers = 1;
my $q = new CGI;
my $self_url = $q->script_name();
my $graph_obj = new SAL::Graph;
my $dbo_factory = new SAL::DBI;
my $dbo_data = $dbo_factory->spawn_sqlite(':memory:');
# Build a sample database...
my $report_query = qq[create table ReportData (dfm varchar(255), name varchar(255), purchases int(11), sort int(11))];
$dbo_data->do($report_query);
# Obviously not optimized...
$report_query = qq[insert into ReportData values('Data Formatting Markup Tags','Customer','Purchases','0')];
$dbo_data->do($report_query);
$report_query = qq[insert into ReportData values(' ','Morris','30','1')];
$dbo_data->do($report_query);
$report_query = qq[insert into ReportData values(' ','Albert','22','1')];
$dbo_data->do($report_query);
my $graph_query = 'SELECT name, purchases FROM ReportData WHERE (sort > 0) and (sort < 998) ORDER BY sort, name';
my ($w, $h) = $dbo_data->execute($graph_query);
my @legend = qw(a b);
$graph_obj->set_legend(@legend);
$graph_obj->{image}{width} = '400';
$graph_obj->{image}{height} = '300';
$graph_obj->{formatting}{title} = "Customer Purchases";
$graph_obj->{formatting}{'y_max_value'} = 50;
$graph_obj->{formatting}{'y_min_value'} = 0;
$graph_obj->{formatting}{'x_label'} = 'Customer';
$graph_obj->{formatting}{'y_label'} = 'Purchases';
$graph_obj->{formatting}{'y_tick_number'} = 10;
$graph_obj->{formatting}{'boxclr'} = '#EEEEFF';
$graph_obj->{formatting}{'long_ticks'} = '1';
$graph_obj->{formatting}{'line_types'} = [(1,3,4)];
$graph_obj->{formatting}{'line_width'} = '2';
$graph_obj->{formatting}{'markers'} = [(7,5,1,8,2,6)];
$graph_obj->{type}='bars3d';
my $graph = $graph_obj->build_graph($send_mime_headers, $dbo_data, $graph_query);
print $graph;

This section describes some useful items in the SAL::_ eponymous hash. Arrow syntax is used here for readability, but is not strictly required.
Note: Replace $SAL::Graph with the name of your database object... eg. $graph->{datasource} = $dbo_data
$SAL::Graph->{datasource} should be a SAL::DBI object (currently unused. see build_graph() method.)
$SAL::Graph->{image}{width} should be set to the desired output width. Default: 400px
$SAL::Graph->{image}{height} should be set to the desired output height. Default: 400px
$SAL::Graph->{type} should be set to the GD::Graph or GD::Graph3d graph-type. (eg. linespoints, bar3d, etc)
$SAL::Graph->{legend} should be set to a list containing entries to show in the graph's legend.
$SAL::Graph->{formatting} should be a hash containing GD::Graph and/or GD::Graph3d formatting options.

Prepares a new Graph object.

Generate a graph by running the sql $query (and @params if provided) against $datasource (a SAL::DBI object).
If you're generating a graph on the fly to be displayed on the web, set $send_mime_headers to a non-zero value.

Scott Elcomb <psema4@gmail.com>

SAL, SAL::DBI, SAL::WebDDR, SAL::WebApplication, GD::Graph, GD::Graph3d