SVG::T...::Line(3pm)

πŸ“¦
version 1.04 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸŽ€ NAME πŸŽ€

SVG::TT::Graph::Line β€” bestie, this module lets you Create presentation quality SVG line graphs easily. Like, actually easily. No cap. It's a User Contributed Perl Documentation page, manual section 3pm, and the module name is literally SVG::TT::Graph::Line. Say it with me. Slay. πŸ’…


✨ SYNOPSIS ✨

Okay so here's the whole vibe in one go, copy-paste energy:

use SVG::TT::Graph::Line;

my @fields = qw(Jan Feb Mar);
my @data_sales_02 = qw(12 45 21);
my @data_sales_03 = qw(15 30 40);

my $graph = SVG::TT::Graph::Line->new({
  'height' => '500',
  'width'  => '300',
  'fields' => \@fields,
});

$graph->add_data({
  'data'  => \@data_sales_02,
  'title' => 'Sales 2002',
});

$graph->add_data({
  'data' => \@data_sales_03,
  'title' => 'Sales 2003',
});

print "Content-type: image/svg+xml\n\n";
print $graph->burn();

That's it. That's the whole plot. You build the graph object, you feed it data, you burn() it and print it. Iconic behavior honestly. πŸ”₯


🌸 DESCRIPTION 🌸

This object aims to allow you to easily create high quality SVG line graphs. Bestie, that is literally the goal. 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.

So yeah: default stylesheet, or your own. And a whole mountain of config options so you can make it look exactly how you want. Main character energy, honestly. πŸ‘‘


πŸ¦‹ METHODS πŸ¦‹

πŸ’– new()

use SVG::TT::Graph::Line;

# Field names along the X axis
my @fields = qw(Jan Feb Mar);

my $graph = SVG::TT::Graph::Line->new({
  # Required
  'fields'                 => \@fields,

  # Optional - defaults shown
  'height'                 => '500',
  'width'                  => '300',

  'show_data_points'       => 1,
  'show_data_values'       => 1,
  'stacked'                => 0,

  'min_scale_value'        => '0',
  'max_scale_value'        => undef,
  'area_fill'              => 0,
  'show_x_labels'          => 1,
  'stagger_x_labels'       => 0,
  'rotate_x_labels'        => 0,
  'show_y_labels'          => 1,
  'scale_integers'         => 0,
  'scale_divisions'        => '20',
  'y_label_formatter'      => sub { return @_ },
  'x_label_formatter'      => sub { return @_ },

  '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. Everything else? Defaults to what you see above, with the one exception of style_sheet, which defaults to using the internal style sheet. So basically: give me field names, get a graph. 🫢

πŸ“ add_data()

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. More calls, more lines. The way that you can just keep stacking datasets... obsessed. πŸ“ˆ

🧚 clear_data()

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. Fresh start, same aesthetic. ✨

πŸ’€ burn()

print $graph->burn();

This method processes the template with the data and config which has been set and returns the resulting SVG.

⚠️ Heads up bestie: this method will croak unless at least one data set has been added to the graph object. No data, no graph. It will simply perish.

πŸ”§ 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. Setter-getter realness: call it with no args to read, call it with an arg to set. 🌈

height()

Set the height of the graph box, this is the total height of the SVG box created β€” not the graph it self which auto scales to fix the space.

width()

Set the width of the graph box, this is the total height of the SVG box created β€” not the graph it self which auto scales to fix the space.

compress()

Whether or not to compress the content of the SVG file (Compress::Zlib required).

tidy()

Whether or not to tidy the content of the SVG file (XML::Tidy required).

style_sheet()

Set the path to an external stylesheet, set to '' if you want to revert back to using the defaut internal version.

Set to "inline:<style>...</style>" with your CSS in between the tags. You can thus override the default style without requireing an external URL. (Inline CSS? In this economy? We love to see it.)

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.

show_data_points()

Show a small circle on the graph where the line goes from one point to the next.

stacked()

Accumulates each data set. (i.e. Each point increased by sum of all previous series at same point). Default is 0, set to '1' to show.

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 to 1, set to '0' if you want to turn them off.

show_y_labels()

Whether to show labels on the Y axis or not, defaults to 1, set to '0' if you want to turn them off.

scale_integers()

Ensures only whole numbers are used as the scale divisions. Default it '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. Do not test fate, bestie. 999 is the limit and the limit is real.

stagger_x_labels()

This puts the labels at alternative levels so if they are long field names they will not overlap so easily. Default it '0', to turn on set to '1'.

rotate_x_labels()

This turns the X axis labels by 90 degrees. Default it '0', to turn on set to '1'.

show_x_title()

Whether to show the title under the X axis labels, default is 0, 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 is 0, 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, default is 0, 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, default is 0, set to '1' to show.

graph_subtitle()

What the subtitle on the graph should be.

key()

Whether to show a key, defaults to 0, 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] } );

πŸ¦„ EXAMPLES πŸ¦„

For examples look at the project home page http://leo.cuckoo.org/projects/SVG-TT-Graph/. Go look, go click, go be inspired. πŸ’–


🌟 EXPORT 🌟

None by default. Nothing gets exported, nothing sneaks into your namespace. Clean. Minimal. Respectful. 🧼


πŸ‘‘ SEE ALSO πŸ‘‘

  • SVG::TT::Graph
  • SVG::TT::Graph::Bar
  • 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 that's the whole page bestie, you ate that πŸ’…βœ¨

This whole thing is from perl v5.36.0, dated 2022-11-19, and the page is SVG::T...::Line(3pm) β€” yes the header and footer abbreviate it like that, that's just how the manpage be. πŸŽ€ Go make some line graphs, you're gonna slay. πŸ¦‹

πŸ“¦ where this page comes from