SVG::TT::Graph::Bar(3pm) | User Contributed Perl Documentation | SVG::TT::Graph::Bar(3pm) |
SVG::TT::Graph::Bar - Create presentation quality SVG bar graphs easily
use SVG::TT::Graph::Bar; my @fields = qw(Jan Feb Mar); my @data_sales_02 = qw(12 45 21); my $graph = SVG::TT::Graph::Bar->new({ 'height' => '500', 'width' => '300', 'fields' => \@fields, }); $graph->add_data({ 'data' => \@data_sales_02, 'title' => 'Sales 2002', }); print "Content-type: image/svg+xml\n\n"; print $graph->burn();
This object aims to allow you to easily create high quality SVG bar graphs. You can either use the default style sheet or supply your own. Either way there are many options which can be configured to give you control over how the graph is generated - with or without a key, data elements at each point, title, subtitle etc.
use SVG::TT::Graph::Bar; # Field names along the X axis my @fields = qw(Jan Feb Mar); my $graph = SVG::TT::Graph::Bar->new({ # Required 'fields' => \@fields, # Optional - defaults shown 'height' => '500', 'width' => '300', 'show_data_values' => 1, 'min_scale_value' => '0', 'max_scale_value' => undef, 'stagger_x_labels' => 0, 'rotate_x_labels' => 0, 'bar_gap' => 1, 'show_x_labels' => 1, 'show_y_labels' => 1, 'scale_integers' => 0, 'scale_divisions' => '', 'y_label_formatter' => sub { return @_ }, 'x_label_formatter' => sub { return @_ }, 'show_path_title' => 0, 'show_title_fields' => 0, 'show_x_title' => 0, 'x_title' => 'X Field names', 'show_y_title' => 0, 'y_title_text_direction' => 'bt', 'y_title' => 'Y Scale', 'show_graph_title' => 0, 'graph_title' => 'Graph Title', 'show_graph_subtitle' => 0, 'graph_subtitle' => 'Graph Sub Title', 'key' => 0, 'key_position' => 'right', # Stylesheet defaults 'style_sheet' => '/includes/graph.css', # internal stylesheet 'random_colors' => 0, });
The constructor takes a hash reference, fields (the names for each field on the X axis) MUST be set, all other values are defaulted to those shown above - with the exception of style_sheet which defaults to using the internal style sheet.
my @data_sales_02 = qw(12 45 21); $graph->add_data({ 'data' => \@data_sales_02, 'title' => 'Sales 2002', });
This method allows you to add data to the graph object. It can be called several times to add more data sets in, but the likelihood is you should be using SVG::TT::Graph::Line as it won't look great!
my $graph->clear_data();
This method removes all data from the object so that you can reuse it to create a new graph but with the same config options.
print $graph->burn();
This method processes the template with the data and config which has been set and returns the resulting SVG.
This method will croak unless at least one data set has been added to the graph object.
my $value = $graph->method(); my $confirmed_new_value = $graph->method($value);
The following is a list of the methods which are available to change the config of the graph object after it has been created.
Set to "inline:<style>...</style>" with your CSS in between the tags. You can thus override the default style without requireing an external URL.
The default stylesheet handles up to 12 data sets. All data series over the 12th will have no style and be in black. If you have over 12 data sets you can assign them all random colors (see the random_color() method) or create your own stylesheet and add the additional settings for the extra data sets.
To create an external stylesheet create a graph using the default internal version and copy the stylesheet section to an external file and edit from there.
$graph->x_label_formatter( sub { return '$' . $_[0] } );
$graph->y_label_formatter( sub { return '$' . $_[0] } );
For examples look at the project home page http://leo.cuckoo.org/projects/SVG-TT-Graph/
None by default.
SVG::TT::Graph, SVG::TT::Graph::Line, SVG::TT::Graph::BarHorizontal, SVG::TT::Graph::BarLine, SVG::TT::Graph::Pie, SVG::TT::Graph::TimeSeries, SVG::TT::Graph::XY, Compress::Zlib, XML::Tidy
2022-11-19 | perl v5.36.0 |