SVG::T...ph::Bar(3pm)
π SVG::TT::Graph::Bar π
okay bestie, so this is SVG::TT::Graph::Bar(3pm) β it lives in the User Contributed Perl Documentation section of the manual, and it is literally the moment you stop crying about making bar graphs by hand. we're making presentation quality SVG bar graphs, and we're making them easily. periodt. π
β¨ NAME β¨
SVG::TT::Graph::Bar β Create presentation quality SVG bar graphs easily π
πΈ SYNOPSIS πΈ
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();
like. that's it. that's the whole glow-up. π¦
π DESCRIPTION π
so the whole point of this object is to let you create high quality SVG bar graphs easily. You can either use the default style sheet or supply your own β either way there are so many options you can configure to control exactly how the graph gets generated: with or without a key, data elements at each point, title, subtitle, etc. it's giving customizable. it's giving main character. π
π§ METHODS π§
π new()
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, okay? and fields β the names for each field on the X axis β MUST be set. like, non-negotiable, bestie. everything else defaults to the values shown up there βοΈ with the one exception of style_sheet, which defaults to using the internal style sheet. π«Ά
π add_data()
my @data_sales_02 = qw(12 45 21);
$graph->add_data({
'data' => \@data_sales_02,
'title' => 'Sales 2002',
});
this method lets you add data to the graph object. you can call it several times to add more data sets in, but honestly? the likelihood is you should be using SVG::TT::Graph::Line as it won't look great! π (not me warning you in advance, but fr fr)
β¨ clear_data()
my $graph->clear_data();
this one removes all data from the object so you can reuse it to create a new graph but with the same config options. recycling icon behavior. β»οΈ
π₯ burn()
print $graph->burn();
this method processes the template with the data and config which has been set and returns the resulting SVG. π₯
okay so important: this method will croak unless at least one data set has been added to the graph object. so like... add data first. π
π config methods π
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. buckle up, there's a lot, and every single one is iconic. π
height()β Set the height of the graph box. This is the total height of the SVG box created β not the graph itself, which auto scales to fix the space.width()β Set the width of the graph box. This is the total width of the SVG box created β not the graph itself, which auto scales to fix the space.compress()β Whether or not to compress the content of the SVG file (requiresCompress::Zlib).tidy()β Whether or not to tidy the content of the SVG file (requiresXML::Tidy).style_sheet()β Set the path to an external stylesheet, or set it to''if you want to revert back to using the default internal version.
Set it to "inline:<style>...</style>" with your CSS in between the tags. You can thus override the default style without requiring 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.
random_colors()β Use random colors in the internal stylesheet. πshow_data_values()β Show the value of each element of data on the graph.bar_gap()β Whether to have a gap between the bars or not. Default is'1'; set to'0'if you don't want gaps.min_scale_value()β The point at which the Y axis starts. Defaults to'0'; if set to''it will default to the minimum data value.max_scale_value()β The maximum value for the Y axis. If set to'', it will default to the maximum data value.show_x_labels()β Whether to show labels on the X axis or not. Defaults to1; set to'0'if you want to turn them off.stagger_x_labels()β This puts the labels at alternative levels, so if they are long field names they will not overlap so easily. Default is'0'; to turn on, set to'1'.rotate_x_labels()β This turns the X axis labels by 90 degrees. Default is'0'; to turn on, set to'1'.show_y_labels()β Whether to show labels on the Y axis or not. Defaults to1; set to'0'if you want to turn them off.scale_integers()β Ensures only whole numbers are used as the scale divisions. Default is'0'; to turn on, set to'1'. This has no effect if scale divisions are less than 1.scale_divisions()β This defines the gap between markers on the Y axis. Default is a 10th of the max_value, e.g. you will have 10 markers on the Y axis.
NOTE: do not set this too low β you are limited to 999 markers, after that the graph won't generate. π
show_x_title()β Whether to show the title under the X axis labels. Default is0; set to'1'to show.x_title()β What the title under X axis should be, e.g.'Months'.show_y_title()β Whether to show the title under the Y axis labels. Default is0; set to'1'to show.y_title_text_direction()β Aligns writing mode for Y axis label. Defaults to'bt'(Bottom to Top). Change to'tb'(Top to Bottom) to reverse.y_title()β What the title under Y axis should be, e.g.'Sales in thousands'.show_graph_title()β Whether to show a title on the graph. Defaults to0; set to'1'to show.graph_title()β What the title on the graph should be.show_graph_subtitle()β Whether to show a subtitle on the graph. Defaults to0; set to'1'to show.graph_subtitle()β What the subtitle on the graph should be.key()β Whether to show a key. Defaults to0; set to'1'if you want to show it.key_position()β Where the key should be positioned. Defaults to'right'; set to'bottom'if you want to move it.x_label_formatter ()β A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
y_label_formatter()β A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
show_path_title()β Whether to add the title attribute to the data path tags, which will show "tooltips" when hovering over the bar area.show_title_fields()β Whether to show field values as title elements in path tag. Defaults to0; set to'1'to turn on. Suggest on singleadd_datagraphs; for overlapping graphs leave off to see the title value used in theadd_datacall.
π¦ EXAMPLES π¦
for examples, look at the project home page: http://leo.cuckoo.org/projects/SVG-TT-Graph/ β go, go, go. π«Ά
π EXPORT π
None by default. (nothing sneaking out of this module, bestie π )
π SEE ALSO π
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
okay and literally the cutest little sign-off: this whole thing is perl v5.36.0, dated 2022-11-19, and it's filed under SVG::TT::Graph::Bar(3pm). that's the vibe check, and it passed. mwah. πβ¨π
π¦ where this page comes from
- debian bookworm: libsvg-tt-graph-perl 1.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libsvg-tt-graph-perl 1.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libsvg-tt-graph-perl 1.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libsvg-tt-graph-perl 1.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libsvg-tt-graph-perl 1.04-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms