SVG-TT-Graph-1.04 000755 000765 000024 0 13617237165 12537 5 ustar 00leo staff 000000 000000 README 100644 000765 000024 564 13617237165 13465 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04 This archive contains the distribution SVG-TT-Graph,
version 1.04:
Base module for generating SVG graphics
This software is copyright (c) 2019 by Leo Lapworth.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
This README file was generated by Dist::Zilla::Plugin::Readme v6.012.
Changes 100755 000765 000024 15057 13617237165 14146 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04 Revision history for Perl extension SVG::TT::Graph.
1.04 Sat 1 Feb 2020 21:41:03
- Fix typo (Martin McGrath)
1.03 Sat 1 Feb 2020 21:41:03
- Call tzset conditionally (Martin McGrath)
1.02 Thu 5 Sep 2019 21:23:11 BST
- Fix of missing template bracket (Iain Loasby)
1.01 Mon 27 May 2019 09:45:54 BST
- Release without a 'local' dir!
1.00 Fri 10 May 2019 07:19:53
- add support for `inline` (jmccanta)
- doc and timezone fix (Eric Jensen)
0.26 Sun 4 Oct 2015 20:18:50
- Add HeatMap! (stuartskelton)
0.25 Tue 14 Apr 2015 21:26:11
- Make TimeSeries accept max_scale_value. (FGasper)
- Teach TimeSeries to accept a time zone for date display. (FGasper)
0.24 Wed 9 Jul 2014 19:23:11
- Added config.show_path_title to control whether to include new
title attribute in path tag, default to '0' (mikejbwhoo)
- Added TITLE attribute to PATH tag in SHOW DATA section (mikejbwhoo)
- Added config.show_title_fields to allow for fields array element to
be shown instead of title attribute used in add_data, looks nice
when creating non-overlapping bars, default to '0' (mikejbwhoo)
- Above changes made only to Pie, Bar, BarHorizontal, BarLine
(mikejbwhoo)
0.23 Mon 4 Feb 2013 21:14:40
- fix division by zero errors in all graph types (Carsten Wolff)
- Doc clarification (elnjensen)
0.22 Sun 14 Oct 2012 09:43:58
- Link to github for issue tracking
- Allow reversed y-axis (elnjensen)
- Add missing class for data point labels (snabb)
0.21
- New type of graph available: XY (written by Florent Angly, but largely based
on the TimeSeries module by David Meibusch)
0.20
- Fixed wrong formatting of Y labels in SVG::TT:Graph::BarHorizontal (patch by
Erin Schönhals)
- Fixed wrong formatting of Y labels in SVG::TT::Graph::Bar
0.19
- Fixed warning messages with missing data title (#69691, reported by
dean@fragfest.com.au, patch by Florent Angly)
0.18 - Thu 5 Jan 2012
- Added format_x_axis, format_y_axis callback options (Eric Johnson)
0.17 - Wed Jul 20 2011
- By default, do not compress() or tidy() the SVG
- Line and Timeseries graphs now have points styled as '.dataPointNN',
consistently with BarLine graphs (patch from Henrik Pauli, #67834)
- Added a max_scale_value option to set the maximum value for the Y axis of
Bar and Line graphs (patch by Bryce and Henrik Pauli, #20475)
- Repackaged module without extra files (#56718)
0.16 - Sun Apr 18 2010
- Javascript code in the piechart and timeseries put into a CDATA tag
0.15 - Sat Apr 17 2010
- Method get_template() moved from SVG::TT::Graph::* to SVG::TT::Graph to avoid redundancy
- Fixed bug in get_template(). The DATA filehandle is now read from the beginning
at each get_template call
- Makefile.PL does not use autoinstall aymore (not a good practice for CPAN modules)
- Rollover improvements in piechart
0.14 - Mon Apr 05 2010
- Fixed some unit tests and the URL of the repository
0.13 - Sat Apr 03 2010
- Revised TT templates warnings due to undef values (bug # 44115)
- Fixed the "used only once" warnings (bug # 43854, thank you Ia Hun)
- Introduced a new method to use random colors instead of the 12 predefined
colors. Along with the dynamic allocation of the number of style elements,
this allows an unlimited number of dataseries
- Added Javascript code to display field name when the mouse rolls over a
piechart wedge
- Implemented an optional new method to tidy the formatting of the SVG files
(using XML::Tidy)
- Replaced carriage returns by line feeds because it is more portable
(http://www.webmasterworld.com/forum13/4067.htm)
- Did documentation, unit testing and packaging revisions
- Misc little code touch ups
0.12 - Mon Feb 16 12:00:00 EST 2009
- Missed updating the versions for some of the sub libraries.
0.11 - Sun Feb 15 22:54:00 EST 2009
- Applied patches:
http://rt.cpan.org/Ticket/Display.html?id=38441 - Ampersands in Fields cause XML error
http://rt.cpan.org/Ticket/Display.html?id=43326 - Multiple data sets in Bar.pm will now show stacked graphs
http://rt.cpan.org/Ticket/Display.html?id=43325 - Allow toggling the display of data on TimeSeries
0.10 - Mon Apr 28 21:07:30 BST 2008
- Applied patches:
http://rt.cpan.org/Ticket/Display.html?id=34491
http://rt.cpan.org/Ticket/Display.html?id=22896
http://rt.cpan.org/Ticket/Display.html?id=20828
http://rt.cpan.org/Ticket/Display.html?id=20475
0.09
- Applied path patch for Firefox from Pat Ekman for Pie
- patched other modules so Firefox works (doesn't like comma's in a path)
0.08
- Fix stylesheet for Pie
0.07
Help from Yanick with the following:
- Bug fixes:
#7342 (bad default stylesheet class)
#8680 (Typo in defaut Stylesheet for SVG::TT::Graph::TimeSeries)
#7541 (BarHorizontal)
#7549 (Character width setting in BarHorizontal)
0.06
- Put in comment so people know where to copy the
style sheet from (e.g. not including the " 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.
=item random_colors()
Use random colors in the internal stylesheet.
=item show_data_values()
Show the value of each element of data on the graph (or
optionally a user-defined label; see add_data).
=item show_data_points()
Show a small circle on the graph where the line
goes from one point to the next.
=item rollover_values()
Shows data values and data points when the mouse is over the point.
Used in combination with show_data_values and/or show_data_points.
=item data_value_format()
Format specifier to for data values (as per printf).
=item max_x_span()
Maximum span for a line between data points on the X-axis. If this span is
exceeded, the points are not connected. This is useful for skipping missing data
sections. If you set this value to something smaller than 0 (e.g. -1), you will
get an XY scatter plot with no line joining the data points.
=item 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.
=item min_yscale_value()
The point at which the Y axis starts, defaults to '0',
if set to '' it will default to the minimum data value.
=item max_yscale_value()
The point at which the Y axis ends,
if set to '' it will default to the maximum data value.
=item yscale_divisions()
This defines the gap between markers on the Y axis,
default is a 10th of the range, 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.
=item 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.
=item 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.
=item y_label_format()
Format string for presenting the Y axis labels (as per printf).
=item xscale_divisions()
This defines the gap between markers on the X axis.
Default is the entire range (only start and end axis
labels).
=item 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'.
=item rotate_x_labels()
This turns the X axis labels by 90 degrees.
Default it '0', to turn on set to '1'.
=item min_xscale_value()
This sets the minimum X value. Any data points before this value will not be
shown.
=item max_xscale_value()
This sets the maximum X value. Any data points after this value will not be
shown.
=item show_x_title()
Whether to show the title under the X axis labels,
default is 0, set to '1' to show.
=item x_title()
What the title under X axis should be, e.g. 'Parameter X'.
=item show_y_title()
Whether to show the title under the Y axis labels,
default is 0, set to '1' to show.
=item y_title()
What the title under Y axis should be, e.g. 'Sales in thousands'.
=item show_graph_title()
Whether to show a title on the graph,
default is 0, set to '1' to show.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph,
default is 0, set to '1' to show.
=item graph_subtitle()
What the subtitle on the graph should be.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_position()
Where the key should be positioned, defaults to
'right', set to 'bottom' if you want to move it.
=item x_label_formatter ()
A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
=item y_label_formatter()
A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init {
my $self = shift;
}
sub _set_defaults {
my $self = shift;
my @fields = ();
my %default = (
'fields' => \@fields,
'width' => '500',
'height' => '300',
'style_sheet' => '',
'random_colors' => 0,
'show_data_points' => 1,
'show_data_values' => 1,
'rollover_values' => 0,
'max_x_span' => '',
'area_fill' => 0,
'show_y_labels' => 1,
'yscale_divisions' => '',
'min_yscale_value' => '0',
'stacked' => 0,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'xscale_divisions' => '',
'x_label_formatter' => sub { return @_ },
'y_label_formatter' => sub { return @_ },
'show_x_title' => 0,
'x_title' => 'X Field names',
'show_y_title' => 0,
'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', # bottom or right
);
while( my ($key,$value) = each %default ) {
$self->{config}->{$key} = $value;
}
}
# override this so we can pre-manipulate the data
sub add_data {
my ($self, $conf) = @_;
croak 'no data provided'
unless (defined $conf->{'data'} && ref($conf->{'data'}) eq 'ARRAY');
# create an array
unless(defined $self->{'data'}) {
my @data;
$self->{'data'} = \@data;
}
# convert to sorted (by ascending numeric value) array of [ x, y ]
my @new_data = ();
my ($i,$x,@pair);
$i = 0;
while ($i < @{$conf->{'data'}}) {
@pair = ();
if (ref($conf->{'data'}->[$i]) eq 'ARRAY') {
push @pair,@{$conf->{'data'}->[$i]};
$i++;
}
else {
$pair[0] = $conf->{'data'}->[$i++];
$pair[1] = $conf->{'data'}->[$i++];
}
push @new_data, [ @pair ];
}
my @sorted = sort {@{$a}[0] <=> @{$b}[0]} @new_data;
# if stacked, we accumulate the
if (($self->{config}->{stacked}) && (@{$self->{'data'}})) {
my $prev = $self->{'data'}->[@{$self->{'data'}} - 1]->{pairs};
# check our length matches previous
croak sprintf("Series %d can not be stacked on previous series. Mismatched length.",
scalar(@{$self->{'data'}}))
unless (scalar(@sorted) == scalar(@$prev));
for (my $i = 0; $i < @sorted; $i++) {
# check the x value matches
croak sprintf("Series %d can not be stacked on previous series. Mismatched x value at sample %d (x %s).",
scalar(@{$self->{'data'}}),
$i,
$sorted[$i][0])
unless ($sorted[$i][0] == $prev->[$i][0]);
$sorted[$i][1] += $prev->[$i][1];
}
}
my %store = (
'pairs' => \@sorted,
);
$store{'title'} = $conf->{'title'} if defined $conf->{'title'};
push (@{$self->{'data'}},\%store);
return 1;
}
# override calculations to set a few calculated values, mainly for scaling
sub calculations {
my $self = shift;
# run through the data and calculate maximum and minimum values
my ($max_key_size, $max_x, $min_x, $max_y, $min_y, $max_x_label_length, $x_label);
foreach my $dataset (@{$self->{data}}) {
$max_key_size = length($dataset->{title}) if ((!defined $max_key_size) || ($max_key_size < length($dataset->{title})));
foreach my $pair (@{$dataset->{pairs}}) {
$max_x = $pair->[0] if ((!defined $max_x) || ($max_x < $pair->[0]));
$min_x = $pair->[0] if ((!defined $min_x) || ($min_x > $pair->[0]));
$max_y = $pair->[1] if (($pair->[1] ne '') && ((!defined $max_y) || ($max_y < $pair->[1])));
$min_y = $pair->[1] if (($pair->[1] ne '') && ((!defined $min_y) || ($min_y > $pair->[1])));
$x_label = $pair->[0];
$max_x_label_length = length($x_label) if ((!defined $max_x_label_length) || ($max_x_label_length < length($x_label)));
}
}
$self->{calc}->{max_key_size} = $max_key_size;
$self->{calc}->{max_x} = $max_x;
$self->{calc}->{min_x} = $min_x;
$self->{calc}->{max_y} = $max_y;
$self->{calc}->{min_y} = $min_y;
$self->{calc}->{max_x_label_length} = $max_x_label_length;
# Calc the x axis scale values
$self->{calc}->{min_xscale_value} = ($self->_is_valid_config('min_xscale_value')) ? $self->{config}->{min_xscale_value} : $min_x;
$self->{calc}->{max_xscale_value} = ($self->_is_valid_config('max_xscale_value')) ? $self->{config}->{max_xscale_value} : $max_x;
$self->{calc}->{xscale_range} = $self->{calc}->{max_xscale_value} - $self->{calc}->{min_xscale_value};
# Calc the y axis scale values
$self->{calc}->{min_yscale_value} = ($self->_is_valid_config('min_yscale_value')) ? $self->{config}->{min_yscale_value} : $min_y;
$self->{calc}->{max_yscale_value} = ($self->_is_valid_config('max_yscale_value')) ? $self->{config}->{max_yscale_value} : $max_y;
$self->{calc}->{yscale_range} = $self->{calc}->{max_yscale_value} - $self->{calc}->{min_yscale_value};
my ($range,$division,$precision);
if ($self->_is_valid_config('yscale_divisions')) {
$division = $self->{config}->{yscale_divisions};
if ($division >= 1) {
$precision = 0;
}
else {
$precision = length($division) - 2;
}
}
else {
# Find divisions, format and range
($range, $division, $precision) = $self->_range_calc($self->{calc}->{yscale_range});
# If a max value hasn't been set we can set a revised range and max value
if (! $self->_is_valid_config('max_yscale_value')) {
$self->{calc}->{max_yscale_value} = $self->{calc}->{min_yscale_value} + $range;
$self->{calc}->{yscale_range} = $self->{calc}->{max_yscale_value} - $self->{calc}->{min_yscale_value};
}
}
$self->{calc}->{yscale_division} = $division;
$self->{calc}->{y_label_format} = ($self->_is_valid_config('y_label_format')) ? $self->{config}->{y_label_format} : "%.${precision}f";
$self->{calc}->{data_value_format} = ($self->_is_valid_config('data_value_format')) ? $self->{config}->{data_value_format} : "%.${precision}f";
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
synopsis_line.pl 100644 000765 000024 1041 13617237165 17353 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/script use lib qw(lib);
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,
'compress' => 0,
});
$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\r\n\r\n";
print $graph->burn();
Pie.pm 100755 000765 000024 55266 13617237165 16531 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::Pie;
use strict;
use Carp;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
=head1 NAME
SVG::TT::Graph::Pie - Create presentation quality SVG pie graphs easily
=head1 SYNOPSIS
use SVG::TT::Graph::Pie;
my @fields = qw(Jan Feb Mar);
my @data_sales_02 = qw(12 45 21);
my $graph = SVG::TT::Graph::Pie->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();
=head1 DESCRIPTION
This object aims to allow you to easily create high quality
SVG pie 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, display percent on pie chart,
title, subtitle etc.
=head1 METHODS
=head2 new()
use SVG::TT::Graph::Pie;
# Field names along the X axis
my @fields = qw(Jan Feb Mar);
my $graph = SVG::TT::Graph::Pie->new({
# Required
'fields' => \@fields,
# Optional - defaults shown
'height' => '500',
'width' => '300',
'show_graph_title' => 0,
'graph_title' => 'Graph Title',
'show_graph_subtitle' => 0,
'graph_subtitle' => 'Graph Sub Title',
'show_shadow' => 1,
'shadow_size' => 1,
'shadow_offset' => 15,
'key_placement' => 'R',
# data by pie chart wedges:
'show_data_labels' => 0,
'show_actual_values' => 0,
'show_percent' => 1,
'rollover_values' => 0,
'show_path_title' => 0,
'show_title_fields' => 0,
# data on key:
'show_key_data_labels' => 1,
'show_key_actual_values' => 1,
'show_key_percent' => 0,
'expanded' => 0,
'expand_greatest' => 0,
# Stylesheet defaults
'style_sheet' => '/includes/graph.css', # internal stylesheet
'style_sheet_field_names' => 0,
'random_colors' => 0,
});
The constructor takes a hash reference, fields (the name for each
slice on the pie) 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.
=head2 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, only
the first data set added will be used!
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item 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.
=item width()
Set the width of the graph box, this is the total width
of the SVG box created - not the graph it self which auto
scales to fix the space.
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item 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:" 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.
=item random_colors()
Use random colors in the internal stylesheet
=item style_sheet_field_names()
If you use the style_sheet_field_names() option then you can
use the field names within your stylesheet. This allows
consistent use of styles. The names should be:
=over 4
=item _dataPoint
=item _key
=back
=item show_graph_title()
Whether to show a title on the graph, default is '0'.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph, default is '0'.
=item graph_subtitle()
What the subtitle on the graph should be.
=item show_shadow()
Turn the shadow on and off, default to '1', set
to '0' if you don't want it. It is automatically
turned off if you extract one section of the pie.
=item shadow_size()
Size of the shadow if shown, measured as
percentage of pie chart radius, default of 1
being the same size as the pie.
=item shadow_offset()
Offset (in pixels) of shadow to bottom-right
in relation to the center of the pie chart.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_placement()
Defaults to 'R' - right, can be
'R', 'L', 'T' or 'B'.
=item show_data_labels()
Show label on pie chart, defaults
to '0', can be set to '1'.
=item show_actual_values()
Show values on pie chart, defaults
to '0', can be set to '1'.
=item show_percent()
Show percent (rounded) on the pie chart, defaults
to '1', can be set to '0'.
=item rollover_values()
Shows data field and value when the mouse is over a piechart wedge.
=item show_path_title()
Whether to add the title attribute to the data path tags,
which will show "tooltips" when hovering over the bar area.
=item show_title_fields()
Whether to show field values as title elements in path tag,
defaults to 0, set to '1' to turn on. Suggest on single
add_data graphs, for overlapping graphs leave off to see
the title value used in the add_data call.
=item show_key_data_labels()
Show label on the key, defaults
to '1', can be set to '0'.
=item show_key_actual_values()
Show value on the key, defaults
to '1', can be set to '0'.
=item show_key_percent()
Show percent (rounded) on the key, defaults
to '0', can be set to '1'.
=item expanded()
All slices of pie are exploded out, defaults
to '0'. Do not set to '1' if you are going to
use expanded_greatest().
=item expand_greatest()
The largest slice of pie is exploded out
from the pie, defaults to '0'. Useful if you are
only showing the percentages (which are rounded) but
still want to visually show which slice was largest.
Do not set to '1' if you are going to
use expanded().
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init {
my $self = shift;
croak "fields was not supplied or is empty"
unless defined $self->{'config'}->{fields}
&& ref($self->{'config'}->{fields}) eq 'ARRAY'
&& scalar(@{$self->{'config'}->{fields}}) > 0;
}
sub _set_defaults {
my $self = shift;
my %default = (
'width' => '500',
'height' => '300',
'style_sheet' => '',
'style_sheet_field_names' => 0,
'random_colors' => 0,
'show_graph_title' => 0,
'graph_title' => 'Graph Title',
'show_graph_subtitle' => 0,
'graph_subtitle' => 'Graph Sub Title',
'show_shadow' => 1,
'shadow_size' => 1,
'shadow_offset' => 15,
'key_placement' => 'R',
'show_data_labels' => 0,
'show_actual_values' => 0,
'show_percent' => 1,
'rollover_values' => 0,
'show_path_title' => 0,
'show_title_fields' => 0,
'key' => 0,
'show_key_data_labels' => 1,
'show_key_actual_values' => 1,
'show_key_percent' => 0,
'expanded' => 0,
'expand_greatest' => 0,
);
while( my ($key,$value) = each %default ) {
$self->{config}->{$key} = $value;
}
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
Bar.pm 100755 000765 000024 64126 13617237165 16513 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::Bar;
use strict;
use Carp;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
=head1 NAME
SVG::TT::Graph::Bar - Create presentation quality SVG bar graphs easily
=head1 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();
=head1 DESCRIPTION
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.
=head1 METHODS
=head2 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, 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.
=head2 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,
but the likelihood is you should be using SVG::TT::Graph::Line
as it won't look great!
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item 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.
=item width()
Set the width of the graph box, this is the total width
of the SVG box created - not the graph it self which auto
scales to fix the space.
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item 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:" 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.
=item random_colors()
Use random colors in the internal stylesheet
=item show_data_values()
Show the value of each element of data on the graph
=item bar_gap()
Whether to have a gap between the bars or not, default
is '1', set to '0' if you don't want gaps.
=item 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.
=item max_scale_value()
The maximum value for the Y axis. If set to '', it will
default to the maximum data value.
=item 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.
=item 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'.
=item rotate_x_labels()
This turns the X axis labels by 90 degrees.
Default it '0', to turn on set to '1'.
=item 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.
=item 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.
=item 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.
=item show_x_title()
Whether to show the title under the X axis labels,
default is 0, set to '1' to show.
=item x_title()
What the title under X axis should be, e.g. 'Months'.
=item show_y_title()
Whether to show the title under the Y axis labels,
default is 0, set to '1' to show.
=item 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.
=item y_title()
What the title under Y axis should be, e.g. 'Sales in thousands'.
=item show_graph_title()
Whether to show a title on the graph, defaults
to 0, set to '1' to show.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph, defaults
to 0, set to '1' to show.
=item graph_subtitle()
What the subtitle on the graph should be.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_position()
Where the key should be positioned, defaults to
'right', set to 'bottom' if you want to move it.
=item x_label_formatter ()
A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
=item y_label_formatter()
A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
=item show_path_title()
Whether to add the title attribute to the data path tags,
which will show "tooltips" when hovering over the bar area.
=item show_title_fields()
Whether to show field values as title elements in path tag,
defaults to 0, set to '1' to turn on. Suggest on single
add_data graphs, for overlapping graphs leave off to see
the title value used in the add_data call.
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init {
my $self = shift;
croak "fields was not supplied or is empty"
unless defined $self->{'config'}->{fields}
&& ref($self->{'config'}->{fields}) eq 'ARRAY'
&& scalar(@{$self->{'config'}->{fields}}) > 0;
}
sub _set_defaults {
my $self = shift;
my %default = (
'width' => '500',
'height' => '300',
'style_sheet' => '',
'random_colors' => 0,
'show_data_values' => 1,
'min_scale_value' => '0',
'max_scale_value' => '',
'scale_divisions' => '',
'bar_gap' => 1,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'x_label_formatter' => sub { return @_ },
'y_label_formatter' => sub { return @_ },
'show_path_title' => 0,
'show_title_fields' => 0,
'show_y_labels' => 1,
'scale_integers' => 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', # bottom or right
);
while( my ($key,$value) = each %default ) {
$self->{config}->{$key} = $value;
}
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
Line.pm 100755 000765 000024 63670 13617237165 16701 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::Line;
use strict;
use Carp;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
=head1 NAME
SVG::TT::Graph::Line - Create presentation quality SVG line graphs easily
=head1 SYNOPSIS
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();
=head1 DESCRIPTION
This object aims to allow you to easily create high quality
SVG line 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.
=head1 METHODS
=head2 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, all other values are defaulted to those
shown above - with the exception of style_sheet which defaults
to using the internal style sheet.
=head2 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.
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item 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.
=item 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.
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item 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:" 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.
=item random_colors()
Use random colors in the internal stylesheet
=item show_data_values()
Show the value of each element of data on the graph
=item show_data_points()
Show a small circle on the graph where the line
goes from one point to the next.
=item 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.
=item 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.
=item max_scale_value()
The maximum value for the Y axis. If set to '', it will
default to the maximum data value.
=item 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.
=item 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.
=item 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.
=item 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.
=item 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'.
=item rotate_x_labels()
This turns the X axis labels by 90 degrees.
Default it '0', to turn on set to '1'.
=item show_x_title()
Whether to show the title under the X axis labels,
default is 0, set to '1' to show.
=item x_title()
What the title under X axis should be, e.g. 'Months'.
=item show_y_title()
Whether to show the title under the Y axis labels,
default is 0, set to '1' to show.
=item 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.
=item y_title()
What the title under Y axis should be, e.g. 'Sales in thousands'.
=item show_graph_title()
Whether to show a title on the graph,
default is 0, set to '1' to show.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph,
default is 0, set to '1' to show.
=item graph_subtitle()
What the subtitle on the graph should be.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_position()
Where the key should be positioned, defaults to
'right', set to 'bottom' if you want to move it.
=item x_label_formatter ()
A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
=item y_label_formatter()
A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init {
my $self = shift;
croak "fields was not supplied or is empty"
unless defined $self->{'config'}->{fields}
&& ref($self->{'config'}->{fields}) eq 'ARRAY'
&& scalar(@{$self->{'config'}->{fields}}) > 0;
}
sub _set_defaults {
my $self = shift;
my %default = (
'width' => '500',
'height' => '300',
'style_sheet' => '',
'random_colors' => 0,
'show_data_points' => 1,
'show_data_values' => 1,
'stacked' => 0,
'min_scale_value' => '0',
'max_scale_value' => '',
'area_fill' => 0,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'show_y_labels' => 1,
'scale_integers' => 0,
'scale_divisions' => '',
'x_label_formatter' => sub { return @_ },
'y_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', # bottom or right
);
while( my ($key,$value) = each %default ) {
$self->{config}->{$key} = $value;
}
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
synopsis_bubble.pl 100644 000765 000024 4300 13617237165 17660 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/script use lib qw(../lib);
use SVG::TT::Graph::Bubble;
my @data_flat_array;
my @data_AOA;
my @data_AOH;
# DONT DOTHIS
my @data_AOAHs;
for (1..20)
{
push @data_flat_array, (rand(),rand(100),rand(20));
push @data_AOA, [rand(),rand(100), rand(20)];
push @data_AOH, {x=>rand(),y=>rand(100), z=>rand(20)};
push @data_AOAHs, [rand(),rand(100), rand(20)];
push @data_AOAHs, {x=>rand(),y=>rand(100), z=>rand(20)};
}
my $graph = SVG::TT::Graph::Bubble->new( {
'height' => 900,
'width' => 900,
'show_y_labels' => 1,
'yscale_divisions' => '',
'min_yscale_value' => 0,
'max_yscale_value' => 100,
'show_x_labels' => 1,
'xscale_divisions' => '',
'min_xscale_value' => 0,
'max_xscale_value' => 1,
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'y_label_formatter' => sub {return @_},
'x_label_formatter' => sub {return @_},
'show_data_values' => 0,
'rollover_values' => 0,
'area_fill' => 1,
'show_x_title' => 1,
'x_title' => 'X Field names',
'show_y_title' => 0,
'y_title' => 'Y Scale',
'show_graph_title' => 0,
'graph_title' => 'Graph Title',
'show_graph_subtitle' => 0,
'graph_subtitle' => 'Graph Sub Title',
'key' => '',
'key_position' => 'right',
# Stylesheet defaults
# 'style_sheet' => '/includes/graph.css', # internal stylesheet
'bubble_fill' => 0.4,
'bubble_stroke' => 0,
'diagonal_path' => 0,
'random_colors' => 0
} );
$graph->add_data( { 'data' => \@data_flat_array,
'title' => 'Flat_array',
} );
$graph->add_data( { 'data' => \@data_AOA,
'title' => 'Array_of_arrays',
} );
$graph->add_data( { 'data' => \@data_AOH,
'title' => 'Array_of_hashes',
} );
$graph->add_data( { 'data' => \@data_AOAHs,
'title' => 'Array_of_array_and_hashes',
} );
$graph->tidy(1);
#$graph->compress(1);
#print "Content-type: image/svg+xml\n\n";
print $graph->burn();
synopsis_heatmap.pl 100644 000765 000024 1761 13617237165 20054 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/script use lib qw(../lib);
use SVG::TT::Graph::HeatMap;
use Data::Dumper;
my @data_cpu1;
my $x_size = 10;
for ( 1 .. 20 )
{
push @data,
{ x => "ABC$_",
c => $_ * 30,
b => $_ * 20,
d => int( rand(1000) ),
absasasc => $_,
f => int( rand(1000) ),
e => int( rand(100) ),
};
}
my %ylabs = %{ $data[0] };
delete $ylabs{ x };
my $graph = SVG::TT::Graph::HeatMap->new(
{ block_height => 24,
block_width => 24,
gutter_width => 1,
y_axis_order => [reverse sort keys %ylabs],
rotate_x_labels => 0,
debug => 0,
} );
$graph->add_data(
{ 'data' => \@data,
'title' => 'CPU',
} );
$graph->tidy(1);
print $graph->burn();
1;
Bubble.pm 100644 000765 000024 103563 13617237165 17216 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::Bubble;
use strict;
use Carp;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
=head1 NAME
SVG::TT::Graph::Bubble - Create presentation quality SVG Bubble plot
=head1 SYNOPSIS
use SVG::TT::Graph::Bubble;
my @data = ( 2, 15, 18.10, 6, 19, 0.10, 12, 17, 12.92, 17, 10, 17.97 );
my $graph = SVG::TT::Graph::Bubble->new( { 'height' => '500',
'width' => '300',
} );
$graph->add_data( { 'data' => \@data_cpu,
'title' => 'CPU',
} );
print "Content-type: image/svg+xml\n\n";
print $graph->burn();
=head1 DESCRIPTION
This object aims to allow you to easily create high quality
SVG Bubble plot graphs using xyz data. 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.
=head1 METHODS
=head2 new()
use SVG::TT::Graph::Bubble;
my $graph = SVG::TT::Graph::Bubble->new({
# Optional - defaults shown
'height' => 500,
'width' => 300,
'show_y_labels' => 1,
'yscale_divisions' => '',
'min_yscale_value' => 0,
'max_yscale_value' => '',
'show_x_labels' => 1,
'xscale_divisions' => '',
'min_xscale_value' => '',
'max_xscale_value' => '',
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'y_label_formatter' => sub { return @_ },
'x_label_formatter' => sub { return @_ },
'show_data_values' => 1,
'rollover_values' => 0,
'area_fill' => 0,
'show_x_title' => 0,
'x_title' => 'X Field names',
'show_y_title' => 0,
'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,
'bubble_fill' => 0.4,
'bubble_border_stroke' => 0,
});
The constructor takes a hash reference with values defaulted to those
shown above - with the exception of style_sheet which defaults
to using the internal style sheet.
=head2 add_data()
my @data = ( 2, 15, 18.10,
6, 19, 0.10,
12, 17, 12.92,
17, 10, 17.97 );
or
my @data = ( [2, 15, 18.10] , [6, 19, 0.10],
[12, 17, 12.92], [17, 10, 17.97], );
or
my @data = ( { x => 2, y => 15, z => 18.10 },
{ x => 6, y => 19, z => 0.10 },
{ x => 12, y => 17, z => 12.92 },
{ x => 17, y => 10, z => 17.97 },
)
$graph->add_data({
'data' => \@data_cpu,
'title' => 'CPU',
});
This method allows you to add data to the graph object. The
data are expected to be either a list of scalars (in which
case pairs of elements are taken to be X,Y pairs) or a list
of array references. In the latter case, the first two
elements in each referenced array are taken to be X and Y,
and the optional third element (if present) is used as the
text to display for that point for show_data_values and
rollover_values; otherwise the Y value itself is displayed.
It can be called several times to add more data sets in.
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item 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.
=item width()
Set the width of the graph box, this is the total width
of the SVG box created - not the graph it self which auto
scales to fix the space.
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item style_sheet()
Set the path to an external stylesheet, set to '' if
you want to revert back to using the default internal version.
Set to "inline:" 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.
=item random_colors()
Use random colors in the internal stylesheet.
=item show_data_values()
Show the value of each element of data on the graph (or
optionally a user-defined label; see add_data).
=item rollover_values()
Shows data values and data points when the mouse is over the point.
Used in combination with show_data_values.
=item data_value_format()
Format specifier to for data values (as per printf).
=item max_x_span()
Maximum span for a line between data points on the X-axis. If this span is
exceeded, the points are not connected. This is useful for skipping missing data
sections. If you set this value to something smaller than 0 (e.g. -1), you will
get an XY scatter plot with no line joining the data points.
=item 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.
=item min_yscale_value()
The point at which the Y axis starts, defaults to '0',
if set to '' it will default to the minimum data value.
=item max_yscale_value()
The point at which the Y axis ends,
if set to '' it will default to the maximum data value.
=item yscale_divisions()
This defines the gap between markers on the Y axis,
default is a 10th of the range, 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.
=item 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.
=item 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.
=item y_label_format()
Format string for presenting the Y axis labels (as per printf).
=item xscale_divisions()
This defines the gap between markers on the X axis.
Default is the entire range (only start and end axis
labels).
=item 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'.
=item rotate_x_labels()
This turns the X axis labels by 90 degrees.
Default it '0', to turn on set to '1'.
=item min_xscale_value()
This sets the minimum X value. Any data points before this value will not be
shown.
=item max_xscale_value()
This sets the maximum X value. Any data points after this value will not be
shown.
=item show_x_title()
Whether to show the title under the X axis labels,
default is 0, set to '1' to show.
=item x_title()
What the title under X axis should be, e.g. 'Parameter X'.
=item show_y_title()
Whether to show the title under the Y axis labels,
default is 0, set to '1' to show.
=item y_title()
What the title under Y axis should be, e.g. 'Sales in thousands'.
=item show_graph_title()
Whether to show a title on the graph,
default is 0, set to '1' to show.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph,
default is 0, set to '1' to show.
=item graph_subtitle()
What the subtitle on the graph should be.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_position()
Where the key should be positioned, defaults to
'right', set to 'bottom' if you want to move it.
=item x_label_formatter ()
A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
=item y_label_formatter()
A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
=item bubble_fill()
This is a floating number from 0 to 1 and determins how opaque the bubbles are.
Default: 0.4
=item bubble_border_stroke()
Whether to show a 1px border stroke around the bubble.
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init
{
my $self = shift;
}
sub _set_defaults
{
my $self = shift;
my @fields = ();
my %default = (
'fields' => \@fields,
'width' => '500',
'height' => '300',
'style_sheet' => '',
'random_colors' => 0,
'show_data_values' => 1,
'rollover_values' => 0,
'bubble_fill' => 0.4,
'bubble_border_stroke' => 0,
'max_x_span' => '',
'area_fill' => 0,
'show_y_labels' => 1,
'yscale_divisions' => '',
'min_yscale_value' => '0',
'pad_top_y_axis' => 1,
'stacked' => 0,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'xscale_divisions' => '',
'x_label_formatter' => sub {return @_},
'y_label_formatter' => sub {return @_},
'show_x_title' => 0,
'x_title' => 'X Field names',
'show_y_title' => 0,
'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', # bottom or right
'diagonal_path' => 0,
);
while ( my ( $key, $value ) = each %default )
{
$self->{ config }->{ $key } = $value;
}
}
# override this so we can pre-manipulate the data
sub add_data
{
my ( $self, $conf ) = @_;
croak 'no data provided'
unless ( defined $conf->{ 'data' } &&
ref( $conf->{ 'data' } ) eq 'ARRAY' );
# create an array
unless ( defined $self->{ 'data' } )
{
my @data;
$self->{ 'data' } = \@data;
}
# convert to sorted (by ascending numeric value) array of [ x, y ]
my @new_data = ();
my ( $i, $x, @pair );
$i = 0;
while ( $i < @{ $conf->{ 'data' } } )
{
@pair = ();
if ( ref( $conf->{ 'data' }->[$i] ) eq 'ARRAY' )
{
push @pair, @{ $conf->{ 'data' }->[$i] };
$i++;
}
elsif ( ref( $conf->{ 'data' }->[$i] ) eq 'HASH' )
{
$pair[0] = $conf->{ 'data' }->[$i]->{ x };
$pair[1] = $conf->{ 'data' }->[$i]->{ y };
$pair[2] = $conf->{ 'data' }->[$i]->{ z };
$i++;
}
else
{
$pair[0] = $conf->{ 'data' }->[$i++];
$pair[1] = $conf->{ 'data' }->[$i++];
$pair[2] = $conf->{ 'data' }->[$i++];
}
push @new_data, [@pair];
}
my %store = ( 'pairs' => \@new_data, );
$store{ 'title' } = $conf->{ 'title' } if defined $conf->{ 'title' };
push( @{ $self->{ 'data' } }, \%store );
return 1;
}
# override calculations to set a few calculated values, mainly for scaling
sub calculations
{
my $self = shift;
# run through the data and calculate maximum and minimum values
my ( $max_key_size, $max_x, $min_x, $max_y, $min_y, $max_z,
$max_x_label_length, $x_label );
foreach my $dataset ( @{ $self->{ data } } )
{
$max_key_size = length( $dataset->{ title } )
if ( ( !defined $max_key_size ) ||
( $max_key_size < length( $dataset->{ title } ) ) );
foreach my $pair ( @{ $dataset->{ pairs } } )
{
$max_x = $pair->[0]
if ( ( !defined $max_x ) || ( $max_x < $pair->[0] ) );
$min_x = $pair->[0]
if ( ( !defined $min_x ) || ( $min_x > $pair->[0] ) );
$max_y = $pair->[1]
if ( ( $pair->[1] ne '' ) &&
( ( !defined $max_y ) || ( $max_y < $pair->[1] ) ) );
$min_y = $pair->[1]
if ( ( $pair->[1] ne '' ) &&
( ( !defined $min_y ) || ( $min_y > $pair->[1] ) ) );
$max_z = $pair->[2]
if ( ( $pair->[2] ne '' ) &&
( ( !defined $max_z ) || ( $max_z < $pair->[2] ) ) );
$x_label = $pair->[0];
$max_x_label_length = length($x_label)
if ( ( !defined $max_x_label_length ) ||
( $max_x_label_length < length($x_label) ) );
}
}
$self->{ calc }->{ max_key_size } = $max_key_size;
$self->{ calc }->{ max_x } = $max_x;
$self->{ calc }->{ min_x } = $min_x;
$self->{ calc }->{ max_y } = $max_y;
$self->{ calc }->{ min_y } = $min_y;
$self->{ calc }->{ max_z } = $max_z;
$self->{ calc }->{ max_x_label_length } = $max_x_label_length;
# Calc the x axis scale values
$self->{ calc }->{ min_xscale_value } =
( $self->_is_valid_config('min_xscale_value') ) ?
$self->{ config }->{ min_xscale_value } :
$min_x;
$self->{ calc }->{ max_xscale_value } =
( $self->_is_valid_config('max_xscale_value') ) ?
$self->{ config }->{ max_xscale_value } :
$max_x;
$self->{ calc }->{ xscale_range } =
$self->{ calc }->{ max_xscale_value } -
$self->{ calc }->{ min_xscale_value };
# Calc the y axis scale values
$self->{ calc }->{ min_yscale_value } =
( $self->_is_valid_config('min_yscale_value') ) ?
$self->{ config }->{ min_yscale_value } :
$min_y;
$self->{ calc }->{ max_yscale_value } =
( $self->_is_valid_config('max_yscale_value') ) ?
$self->{ config }->{ max_yscale_value } :
$max_y;
$self->{ calc }->{ yscale_range } =
$self->{ calc }->{ max_yscale_value } -
$self->{ calc }->{ min_yscale_value };
my ( $range, $division, $precision );
if ( $self->_is_valid_config('yscale_divisions') )
{
$division = $self->{ config }->{ yscale_divisions };
if ( $division >= 1 )
{
$precision = 0;
}
else
{
$precision = length($division) - 2;
}
}
else
{
# Find divisions, format and range
( $range, $division, $precision ) =
$self->_range_calc( $self->{ calc }->{ yscale_range } );
# If a max value hasn't been set we can set a revised range and max value
if ( !$self->_is_valid_config('max_yscale_value') )
{
$self->{ calc }->{ max_yscale_value } =
$self->{ calc }->{ min_yscale_value } + $range;
$self->{ calc }->{ yscale_range } =
$self->{ calc }->{ max_yscale_value } -
$self->{ calc }->{ min_yscale_value };
}
}
$self->{ calc }->{ yscale_division } = $division;
$self->{ calc }->{ y_label_format } =
( $self->_is_valid_config('y_label_format') ) ?
$self->{ config }->{ y_label_format } :
"%.${precision}f";
$self->{ calc }->{ data_value_format } =
( $self->_is_valid_config('data_value_format') ) ?
$self->{ config }->{ data_value_format } :
"%.${precision}f";
# This is some sanity incase someone activates the diganonal path, and the axis
# are not the same size
if ( $self->{ config }->{ diagonal_path } &&
( $self->{ calc }->{ max_xscale_value } !=
$self->{ calc }->{ max_yscale_value } ||
$self->{ calc }->{ min_xscale_value } !=
$self->{ calc }->{ min_yscale_value } ) )
{
warn
"Using the diagonal_path in the configs assumes the axis\n will be the same size. If you want to use this.\nYou might want to set the min and max x and y sizes.";
}
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
synopsis_bar_line.pl 100644 000765 000024 1067 13617237165 20207 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/script use lib qw(lib);
use SVG::TT::Graph::BarLine;
my @fields = qw(Jan Feb Mar);
my @data_sales_02 = qw(12 45 21);
my @data_sales_03 = (24, 55, 61);
my $graph = SVG::TT::Graph::BarLine->new({
'height' => '500',
'width' => '300',
'fields' => \@fields,
'compress' => 0,
});
$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();
#my $tt = $graph->get_template();
#print "$tt\n";
generate_examples.pl 100755 000765 000024 5247 13617237165 20164 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/script #!/usr/bin/perl -w
# This module can be used to generate some examples
# More example will be added for the next release.
# See http://leo.cuckoo.org/projects/SVG-TT-Graph/ for more
use lib qw( ./lib ./blib/lib ../blib/lib );
use SVG::TT::Graph::Bar;
use SVG::TT::Graph::BarHorizontal;
use SVG::TT::Graph::Line;
use SVG::TT::Graph::Pie;
############ Create example directory
my $dir = 'examples';
mkdir($dir) unless -d $dir;
############ Some data to play with
my @fields1 = ('Januararyasdasdasd','Feb','Mar','Apr','Mayasdasdasdasdasd');
my @data_01 = qw(12 45 21 45 32);
my @data_02 = qw(12 23435 21 3445 345632);
my @fields2 = ('Oct 02','Nov 02','Dec 02','Jan 03','Feb 03','Mar 03','Apr 03','May 03','Jun 03','Jul 03','Aug 03','Sep 03');
my @data_03 = qw(0 0 0 0 0 0 0 0 0 0 1815 0);
############ Generate some bar graphs
run_bar('Bar',\@fields1,\@data_01,'small_range');
run_bar('BarHorizontal',\@fields1,\@data_02,'large_range');
run_line('Line',\@fields2,\@data_03,'default');
sub run_line {
my $type = shift;
my $fields = shift;
my $data = shift;
my $title = shift;
my $module = "SVG::TT::Graph::$type";
## Basic using default config
my $graph1 = $module->new({
'fields' => $fields,
});
$graph1->add_data({
'data' => $data,
'title' => 'Sales 2002 asdfasdfds',
});
my $outfile1 = "$dir/$type" . "_defaults_" . "$title.svg";
open(FH,">$outfile1");
print FH $graph1->burn();
close(FH);
}
sub run_bar {
my $type = shift;
my $fields = shift;
my $data = shift;
my $title = shift;
my $module = "SVG::TT::Graph::$type";
my $graph2 = $module->new({
'fields' => $fields,
'height' => '400',
'width' => '400',
'show_data_values' => 1,
'stagger_x_labels' => 1,
'bar_gap' => 0,
'show_x_labels' => 1,
'show_y_labels' => 1,
'rotate_x_labels' => 1,
'key' => 0,
'show_x_title' => 1,
'x_title' => 'Field names',
'show_y_title' => 1,
'y_title' => 'Y Scale title',
'show_graph_title' => 1,
'graph_title' => 'Graph Title',
'show_graph_subtitle' => 1,
'graph_subtitle' => 'Graph Sub Title',
});
$graph2->add_data({
'data' => $data,
'title' => 'Sales 2002 asdfasdfds',
});
my $outfile2 = "$dir/$type" . "_non-defaults_" . "$title.svg";
open(FH,">$outfile2");
print FH $graph2->burn();
close(FH);
## Basic using default config
my $graph1 = $module->new({
'fields' => $fields,
});
$graph1->add_data({
'data' => $data,
'title' => 'Sales 2002 asdfasdfds',
});
my $outfile1 = "$dir/$type" . "_defaults_" . "$title.svg";
open(FH,">$outfile1");
print FH $graph1->burn();
close(FH);
}
HeatMap.pm 100644 000765 000024 46667 13617237165 17335 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::HeatMap;
use Modern::Perl;
use Carp;
use Data::Dumper;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
=head1 NAME
SVG::TT::Graph::HeatMap - Create presentation quality SVG HeatMap graph of XYZ data points easily
=head1 SYNOPSIS
use SVG::TT::Graph::HeatMap;
my @data_cpu = (
{ x => x_point1,
y_point1 => 10,
y_point2 => 200,
y_point3 => 1000,
},
{ x => x_point2,
y_point1 => 100,
y_point2 => 400,
y_point3 => 500,
},
{ x => x_point3,
y_point1 => 1000,
y_point2 => 600,
y_point3 => 0,
},
);
my $graph = SVG::TT::Graph::HeatMap->new(
{ block_height => 24,
block_width => 24,
gutter_width => 1,
} );
$graph->add_data(
{ 'data' => \@data_cpu,
'title' => 'CPU',
} );
print "Content-type: image/svg+xml\n\n";
print $graph->burn();
=head1 DESCRIPTION
This object aims to allow you to easily create high quality
SVG HeatMap graphs of XYZ data. You can either use the default style sheet
or supply your own.
Please note, the height and width of the final image is computed from the
size of the labels, block_height/block_with and gutter_size.
=head1 METHODS
=head2 new()
use SVG::TT::Graph::HeatMap;
my $graph = SVG::TT::Graph::HeatMap->new({
# Optional - defaults shown
block_height => 24,
block_width => 24,
gutter_width => 1,
'y_axis_order' => [],
});
The constructor takes a hash reference with values defaulted to those
shown above - with the exception of style_sheet which defaults
to using the internal style sheet.
=head2 add_data()
my @data_cpu = (
{ x => x_point1,
y_point1 => 10,
y_point2 => 200,
y_point3 => 1000,
},
{ x => x_point2,
y_point1 => 100,
y_point2 => 400,
y_point3 => 500,
},
{ x => x_point3,
y_point1 => 1000,
y_point2 => 600,
y_point3 => 0,
},
);
or
my @data_cpu = ( ['x', 'y_point1', 'y_point2', 'y_point3'],
['x_point1', 10, 200, 5],
['x_point2', 100, 400, 1000],
['x_point3', 1000, 600, 0],
);
$graph->add_data({
'data' => \@data_cpu,
'title' => 'CPU',
});
This method allows you to add data to the graph object. The
data are expected to be either a array of hashes or as a 2D
matrix (array of arrays), with the Y-axis as the first arrayref,
and the X-axis values in the first element of subsequent arrayrefs.
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item block_width()
The width of the blocks in px.
=item block_height()
The height of the blocks in px.
=item gutter()
The space between the blocks in px.
=item y_axis_order()
This is order the columns are presented on the y-axis, if the data is in a Array of hashes,
this has to be set, however is the data is in an 2D matrix (array of arrays), it will use
the order presented in the header array.
If the data is given in a 2D matrix, and the y_axis_order is set, the y_axis_order will take
prescience.
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init
{
my $self = shift;
}
sub _set_defaults
{
my $self = shift;
my @fields = ();
my %default = (
'fields' => \@fields,
'block_width' => 24,
'block_height' => 24,
'gutter' => 1,
'y_axis_order' => [],
);
while ( my ( $key, $value ) = each %default )
{
$self->{ config }->{ $key } = $value;
}
}
# override this so we can pre-manipulate the data
sub add_data
{
my ( $self, $conf ) = @_;
croak 'no data provided'
unless ( defined $conf->{ 'data' } &&
ref( $conf->{ 'data' } ) eq 'ARRAY' );
# create an array
unless ( defined $self->{ 'data' } )
{
my @data;
$self->{ 'data' } = \@data;
}
else
{
croak 'There can only be a single piece of data';
}
# If there is an order this takes prescience and all data points should have this
# however if there are
my %check;
if ( 0 == scalar @{ $self->{ config }->{ y_axis_order } } )
{
if ( ref( $conf->{ 'data' }->[0] ) eq 'ARRAY' )
{
my @header = @{ $conf->{ 'data' }->[0] };
$self->{ config }->{ y_axis_order } = [@header[1 .. $#header]];
}
}
%check = map {$_, 1} @{ $self->{ config }->{ y_axis_order } };
croak
'The Data needs to have either a y_axis_order or a header array in the data'
if 0 == scalar keys %check;
# convert to sorted (by ascending numeric value) array of [ x, y ]
my @new_data = ();
my ( $i, $x );
$i = ref( $conf->{ 'data' }->[0] ) eq 'ARRAY' ? 1 : 0;
my $max = scalar @{ $conf->{ 'data' } };
while ( $i < $max )
{
my %row;
if ( ref( $conf->{ 'data' }->[$i] ) eq 'ARRAY' )
{
$row{ x } = $conf->{ 'data' }->[$i]->[0];
for my $col ( 1 .. $#{ $conf->{ 'data' }->[$i] } )
{
$row{ $conf->{ 'data' }->[0]->[$col] } =
$self->colourDecide( $conf->{ 'data' }->[$i]->[$col] )
#$conf->{ 'data' }->[$i]->[$col];
}
}
elsif ( ref( $conf->{ 'data' }->[$i] ) eq 'HASH' )
{
# check the hash to make sure make sure the data is in it
croak "row '$i' has no x value"
unless defined $conf->{ 'data' }->[$i]->{ x };
$row{ x } = $conf->{ 'data' }->[$i]->{ x };
while ( my ( $k, $v ) = each %check )
{
unless ( defined $conf->{ 'data' }->[$i]->{ $k } )
{
croak "zzz '$row{ x }' does not have a '$k' vaule"
unless ( $self->{ config }->{ include_undef_values } );
}
$row{ $k } =
$self->colourDecide( $conf->{ 'data' }->[$i]->{ $k } );
}
}
else
{
croak
'Data needs to be in an Array of Arrays or an Array of Hashes ';
}
push @new_data, \%row;
$i++;
}
my %store = ( 'pairs' => \@new_data, );
$store{ 'title' } = $conf->{ 'title' } if defined $conf->{ 'title' };
push( @{ $self->{ 'data' } }, \%store );
return 1;
}
# override calculations to set a few calculated values, mainly for scaling
sub calculations
{
my $self = shift;
# run through the data and calculate maximum and minimum values
my ( $max_key_size, $max_x, $min_x, $max_y, $min_y, $max_x_label_length,
$x_label, $max_y_label_length );
my @y_axis_order = @{ $self->{ config }->{ y_axis_order } };
for my $y_axis_label (@y_axis_order)
{
$max_y_label_length = length $y_axis_label
if ( ( !defined $max_y_label_length ) ||
( $max_y_label_length < length $y_axis_label ) );
}
foreach my $dataset ( @{ $self->{ data } } )
{
$max_key_size = length( $dataset->{ title } )
if ( ( !defined $max_key_size ) ||
( $max_key_size < length( $dataset->{ title } ) ) );
$max_x = scalar @{ $dataset->{ pairs } }
if ( ( !defined $max_x ) ||
( $max_x < scalar @{ $dataset->{ pairs } } ) );
foreach my $pair ( @{ $dataset->{ pairs } } )
{
$min_x = 0;
$max_y = scalar @y_axis_order;
for my $y_vaules (@y_axis_order)
{
$min_y = 0;
}
$x_label = $pair->{ x };
$max_x_label_length = length($x_label)
if ( ( !defined $max_x_label_length ) ||
( $max_x_label_length < length($x_label) ) );
}
}
$self->{ calc }->{ max_key_size } = $max_key_size;
$self->{ calc }->{ max_x } = $max_x;
$self->{ calc }->{ min_x } = $min_x;
$self->{ calc }->{ max_y } = $max_y;
$self->{ calc }->{ min_y } = $min_y;
$self->{ calc }->{ max_x_label_length } = $max_x_label_length;
$self->{ calc }->{ max_y_label_length } = $max_y_label_length;
$self->{ config }->{ width } =
( 10 * 2 ) + ( $max_y_label_length * 8 ) + 1 + (
$max_x * (
$self->{ config }->{ block_width } +
$self->{ config }->{ gutter_width }
) );
$self->{ config }->{ height } =
( 10 * 2 ) + ( $max_x_label_length * 8 ) + 1 + (
$max_y * (
$self->{ config }->{ block_width } +
$self->{ config }->{ gutter_width }
) );
}
sub defaultColours
{
my ($self) = @_;
my %default = (
'<=' => { 1000 => [0, 0, 255],
900 => [4, 150, 252],
800 => [4, 218, 252],
700 => [4, 200, 100],
600 => [36, 225, 36],
500 => [132, 255, 14],
400 => [244, 254, 4],
300 => [252, 190, 4],
200 => [252, 125, 4],
100 => [252, 2, 4],
},
'=' => { 0 => [0, 0, 0],
-1 => [0, 0, 0],
-2 => [0, 0, 0],
-3 => [0, 0, 0],
-4 => [0, 0, 0],
} );
return %default;
}
sub colourDecide
{
my ( $self, $score ) = @_;
my %key = $self->defaultColours;
# return the default missing colour if the score is undef
return 'rgb(255,255,255)' unless defined $score;
my @precidence = qw(< <= > >= = );
my %tests = ( '<' => sub {return 1, if $_[0] < $_[1]},
'<=' => sub {return 1, if $_[0] <= $_[1]},
'>' => sub {return 1, if $_[0] > $_[1]},
'>=' => sub {return 1, if $_[0] >= $_[1]},
'=' => sub {return 1, if $_[0] == $_[1]},
);
# set this to the default so if there are no rule matches
# we just use the default
my $colour = [0, 0, 0];
for my $symbol (@precidence)
{
next unless exists $key{ $symbol };
my @values = sort {$b <=> $a} keys %{ $key{ $symbol } };
# if we are looking for the highest we flip the order
@values = reverse @values if ( $symbol =~ /^>/ );
for my $value (@values)
{
if ( $tests{ $symbol }( $score, $value ) )
{
$colour = $key{ $symbol }{ $value };
}
}
}
return sprintf "rgb(%s,%s,%s)", @$colour;
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
BarLine.pm 100755 000765 000024 75755 13617237165 17335 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::BarLine;
use strict;
use Carp;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
=head1 NAME
SVG::TT::Graph::BarLine - Create presentation quality SVG bar-line graphs easily
=head1 SYNOPSIS
use SVG::TT::Graph::BarLine;
my @fields = qw(Jan Feb Mar);
my @data_sales_02 = qw(12 45 21);
my @data_sales_03 = (24, 55, 61);
my $graph = SVG::TT::Graph::BarLine->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();
=head1 DESCRIPTION
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.
=head1 METHODS
=head2 new()
use SVG::TT::Graph::BarLine;
# Field names along the X axis
my @fields = qw(Jan Feb Mar);
my $graph = SVG::TT::Graph::BarLine->new({
# Required
'fields' => \@fields,
# Optional - defaults shown
'height' => '500',
'width' => '300',
'show_data_values' => 1,
'scale_divisions' => '',
'min_scale_value' => '0',
'bar_gap' => 1,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'show_y_labels' => 1,
'scale_integers' => 0,
'show_secondary_y_labels' => 1,
'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,
'show_secondary_y_title' => 0,
'y_title_text_direction' => 'bt',
'secondary_y_title_text_direction' => 'bt',
'y_title' => 'Y Scale',
'secondary_y_title' => 'Y Scale 2',
'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.
=head2 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,
but the likelihood is you should be using SVG::TT::Graph::Line
as it won't look great!
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item 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.
=item width()
Set the width of the graph box, this is the total width
of the SVG box created - not the graph it self which auto
scales to fix the space.
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item 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:" 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.
=item random_colors()
Use random colors in the internal stylesheet
=item show_data_values()
Show the value of each element of data on the graph
=item bar_gap()
Whether to have a gap between the bars or not, default
is '1', set to '0' if you don't want gaps.
=item 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.
=item 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'.
=item rotate_x_labels()
This turns the X axis labels by 90 degrees.
Default it '0', to turn on set to '1'.
=item 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.
=item 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.
=item show_secondary_y_labels()
Whether to show labels on the right hand side Y axis or not, defaults
to 1, set to '0' if you want to turn them off.
=item 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.
=item 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.
=item show_x_title()
Whether to show the title under the X axis labels,
default is 0, set to '1' to show.
=item x_title()
What the title under X axis should be, e.g. 'Months'.
=item show_y_title()
Whether to show the title on the left hand side Y axis,
default is 0, set to '1' to show.
=item show_secondary_y_title()
Whether to show the title on the right hand side Y axis,
default is 0, set to '1' to show.
=item 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.
=item secondary_y_title_text_direction()
Aligns writing mode for right hand Y axis label. Defaults to 'bt' (Bottom to Top).
Change to 'tb' (Top to Bottom) to reverse.
=item y_title()
What the title under Y axis should be, e.g. 'Sales in thousands'.
=item secondary_y_title()
What the title on the right hand side Y axis should be, e.g. 'Number of deals'.
=item show_graph_title()
Whether to show a title on the graph, defaults
to 0, set to '1' to show.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph, defaults
to 0, set to '1' to show.
=item graph_subtitle()
What the subtitle on the graph should be.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_position()
Where the key should be positioned, defaults to
'right', set to 'bottom' if you want to move it.
=item x_label_formatter ()
A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
=item y_label_formatter()
A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
=item show_path_title()
Whether to add the title attribute to the data path tags,
which will show "tooltips" when hovering over the bar area.
=item show_title_fields()
Whether to show field values as title elements in path tag,
defaults to 0, set to '1' to turn on. Suggest on single
add_data graphs, for overlapping graphs leave off to see
the title value used in the add_data call.
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init {
my $self = shift;
croak "fields was not supplied or is empty"
unless defined $self->{'config'}->{fields}
&& ref($self->{'config'}->{fields}) eq 'ARRAY'
&& scalar(@{$self->{'config'}->{fields}}) > 0;
}
sub _set_defaults {
my $self = shift;
my %default = (
'width' => '500',
'height' => '300',
'style_sheet' => '',
'random_colors' => 0,
'show_data_values' => 1,
'min_scale_value' => '0',
'scale_divisions' => '',
'bar_gap' => 1,
'show_x_labels' => 1,
'rotate_x_labels' => 0,
'stagger_x_labels' => 0,
'show_y_labels' => 1,
'scale_integers' => 0,
'show_secondary_y_labels' => 1,
'x_label_formatter' => sub { return @_ },
'y_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,
'show_secondary_y_title' => 0,
'y_title_text_direction' => 'bt',
'secondary_y_title_text_direction' => 'bt',
'y_title' => 'Y Scale',
'secondary_y_title' => 'Y Scale 2',
'show_graph_title' => 0,
'graph_title' => 'Graph Title',
'show_graph_subtitle' => 0,
'graph_subtitle' => 'Graph Sub Title',
'key' => 0,
'key_position' => 'right', # bottom or right
);
while( my ($key,$value) = each %default ) {
$self->{config}->{$key} = $value;
}
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
synopsis_timeseries.pl 100644 000765 000024 1153 13617237165 20601 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/script use lib qw(lib);
use SVG::TT::Graph::TimeSeries;
my @data_cpu = ('2003-09-03 09:30:00',23,'2003-09-03 09:45:00',54,'2003-09-03 10:00:00',67,'2003-09-03 10:15:00',12);
my @data_disk = ('2003-09-03 09:00:00',12,'2003-09-03 10:00:00',26,'2003-09-03 11:00:00',23);
my $graph = SVG::TT::Graph::TimeSeries->new({
'height' => '500',
'width' => '300',
'compress' => 0,
});
$graph->add_data({
'data' => \@data_cpu,
'title' => 'CPU',
});
$graph->add_data({
'data' => \@data_disk,
'title' => 'Disk',
});
#print "Content-type: image/svg+xml\r\n\r\n";
print $graph->burn();
TimeSeries.pm 100755 000765 000024 115362 13617237165 20077 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::TimeSeries;
use strict;
use Carp;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
use Data::Dumper;
use HTTP::Date;
use DateTime;
use POSIX;
=head1 NAME
SVG::TT::Graph::TimeSeries - Create presentation quality SVG line graphs of time series easily
=head1 SYNOPSIS
use SVG::TT::Graph::TimeSeries;
my @data_cpu = ('2003-09-03 09:30:00',23,'2003-09-03 09:45:00',54,'2003-09-03 10:00:00',67,'2003-09-03 10:15:00',12);
my @data_disk = ('2003-09-03 09:00:00',12,'2003-09-03 10:00:00',26,'2003-09-03 11:00:00',23);
my $graph = SVG::TT::Graph::TimeSeries->new({
'height' => '500',
'width' => '300',
});
$graph->add_data({
'data' => \@data_cpu,
'title' => 'CPU',
});
$graph->add_data({
'data' => \@data_disk,
'title' => 'Disk',
});
print "Content-type: image/svg+xml\n\n";
print $graph->burn();
=head1 DESCRIPTION
This object aims to allow you to easily create high quality
SVG line graphs of time series. 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.
All times must be given a format parseable by L.
The L module is used for all date/time calculations.
Note that the module is currently limited to the Unix-style epoch-based
date range limited by 32 bit signed integers (around 1902 to 2038).
=head1 METHODS
=head2 new()
use SVG::TT::Graph::TimeSeries;
my $graph = SVG::TT::Graph::TimeSeries->new({
# Optional - defaults shown
'height' => 500,
'width' => 300,
'show_y_labels' => 1,
'scale_divisions' => '',
'min_scale_value' => 0,
'max_scale_value' => '',
'show_x_labels' => 1,
'timescale_divisions' => '',
'min_timescale_value' => '',
'max_timescale_value' => '',
'x_label_format' => '%Y-%m-%d %H:%M:%S',
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'y_label_formatter' => sub { return @_ },
'x_label_formatter' => sub { return @_ },
'show_data_points' => 1,
'show_data_values' => 1,
'rollover_values' => 0,
'area_fill' => 0,
'show_x_title' => 0,
'x_title' => 'X Field names',
'show_y_title' => 0,
'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 with values defaulted to those
shown above - with the exception of style_sheet which defaults
to using the internal style sheet.
=head2 add_data()
my @data_cpu = ('2003-09-03 09:30:00',23,'2003-09-03 09:45:00',54,'2003-09-03 10:00:00',67,'2003-09-03 10:15:00',12);
or
my @data_cpu = (['2003-09-03 09:30:00',23],['2003-09-03 09:45:00',54],['2003-09-03 10:00:00',67],['2003-09-03 10:15:00',12]);
or
my @data_cpu = (['2003-09-03 09:30:00',23,'23%'],['2003-09-03 09:45:00',54,'54%'],['2003-09-03 10:00:00',67,'67%'],['2003-09-03 10:15:00',12,'12%']);
$graph->add_data({
'data' => \@data_cpu,
'title' => 'CPU',
});
This method allows you to add data to the graph object. The
data are expected to be either a list of scalars (in which case
pairs of elements are taken to be time, value pairs) or a list
of array references. In the latter case, the first two
elements in each referenced array are taken to be time and
value, and the optional third element (if present) is used as
the text to display for that point for show_data_values and
rollover_values (otherwise the value itself is displayed). It
can be called several times to add more data sets in.
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item 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.
=item width()
Set the width of the graph box, this is the total width
of the SVG box created - not the graph it self which auto
scales to fix the space.
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item style_sheet()
Set the path to an external stylesheet, set to '' if
you want to revert back to using the default internal version.
Set to "inline:" 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.
=item random_colors()
Use random colors in the internal stylesheet.
=item show_data_values()
Show the value of each element of data on the graph (or
optionally a user-defined label; see add_data).
=item show_data_points()
Show a small circle on the graph where the line
goes from one point to the next.
=item rollover_values()
Shows data values and data points when the mouse is over the point.
Used in combination with show_data_values and/or show_data_points.
=item data_value_format()
Format specifier to for data values (as per printf).
=item max_time_span()
Maximum timespan for a line between data points. If this span is exceeded, the points are not connected.
This is useful for skipping missing data sections.
The expected form is:
' [years | months | days | hours | minutes | seconds]'
=item stacked()
Accumulates each data set. (i.e. Each point increased by sum of all previous series at same time). Default is 0, set to '1' to show.
All data series have the same number of points and must have the same sequence of time values
for this option.
=item 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.
=item max_scale_value()
The point at which the Y axis ends,
if set to '' it will default to the maximum data value.
=item scale_divisions()
This defines the gap between markers on the Y axis,
default is a 10th of the range, 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.
=item 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.
=item x_label_format()
Format string for presenting the X axis labels.
The POSIX strftime() function is used for formatting
after calling the POSIX tzset() function with the timezone
specified in timescale_time_zone if present
(see strftime man pages and LC_TIME locale information).
=item 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.
=item y_label_format()
Format string for presenting the Y axis labels (as per printf).
=item timescale_divisions()
This defines the gap between markers on the X axis.
Default is the entire range (only start and end axis
labels).
The expected form is:
' [years | months | days | hours | minutes | seconds]'
The default time period if not provided is 'days'.
These time periods are used by the L methods.
=item timescale_time_zone
This determines the time zone used for the date intervals on the X axis.
Values are those that L accepts for its constructor's 'time_zone'
parameter. The default is 'floating'. If passing in data for a different
timezone than that set for your system, note that you also must embed the
timezone information into the values passed to 'add_data', for example by
formatting your DateTime objects with L.
=item 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'.
=item rotate_x_labels()
This turns the X axis labels by 90 degrees.
Default it '0', to turn on set to '1'.
=item min_timescale_value()
This sets the minimum timescale value (X axis).
Any data points before this time will not be shown.
The date/time is expected in ISO format: YYYY-MM-DD hh:mm:ss.
=item max_timescale_value()
This sets the maximum timescale value (X axis).
Any data points after this time will not be shown.
The date/time is expected in ISO format: YYYY-MM-DD hh:mm:ss.
=item show_x_title()
Whether to show the title under the X axis labels,
default is 0, set to '1' to show.
=item x_title()
What the title under X axis should be, e.g. 'Months'.
=item show_y_title()
Whether to show the title under the Y axis labels,
default is 0, set to '1' to show.
=item y_title()
What the title under Y axis should be, e.g. 'Sales in thousands'.
=item show_graph_title()
Whether to show a title on the graph,
default is 0, set to '1' to show.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph,
default is 0, set to '1' to show.
=item graph_subtitle()
What the subtitle on the graph should be.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_position()
Where the key should be positioned, defaults to
'right', set to 'bottom' if you want to move it.
=item x_label_formatter ()
A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
=item y_label_formatter()
A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init {
my $self = shift;
}
sub _set_defaults {
my $self = shift;
my @fields = ();
my %default = (
'fields' => \@fields,
'width' => '500',
'height' => '300',
'style_sheet' => '',
'random_colors' => 0,
'show_data_points' => 1,
'show_data_values' => 1,
'rollover_values' => 0,
'max_time_span' => '',
'area_fill' => 0,
'show_y_labels' => 1,
'scale_divisions' => '',
'min_scale_value' => '0',
'max_scale_value' => '',
'stacked' => 0,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'rotate_x_labels' => 0,
'timescale_divisions' => '',
'timescale_time_zone' => 'floating',
'x_label_format' => '%Y-%m-%d %H:%M:%S',
'x_label_formatter' => sub { return @_ },
'y_label_formatter' => sub { return @_ },
'show_x_title' => 0,
'x_title' => 'X Field names',
'show_y_title' => 0,
'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', # bottom or right
'dateadd' => \&dateadd,
);
while( my ($key,$value) = each %default ) {
$self->{config}->{$key} = $value;
}
}
# override this so we can pre-manipulate the data
sub add_data {
my ($self, $conf) = @_;
croak 'no data provided'
unless (defined $conf->{'data'} && ref($conf->{'data'}) eq 'ARRAY');
# create an array
unless(defined $self->{'data'}) {
my @data;
$self->{'data'} = \@data;
}
# convert to sorted (by ascending time) array of [ time, value ]
my @new_data = ();
my ($i,$time,@pair);
$i = 0;
while ($i < @{$conf->{'data'}}) {
@pair = ();
if (ref($conf->{'data'}->[$i]) eq 'ARRAY') {
push @pair,@{$conf->{'data'}->[$i]};
$i++;
}
else {
$pair[0] = $conf->{'data'}->[$i++];
$pair[1] = $conf->{'data'}->[$i++];
}
$time = str2time($pair[0]);
# special case for time-only values
if ((!defined $time) && ($pair[0] =~ m/^\w*(\d+:\d+:\d+)|(\d+:\d+)\w*$/)) {
$time = str2time('1970-1-1 '.$pair[0]);
}
croak sprintf("Series %d contains an illegal datetime value %s at sample %d.",
scalar(@{$self->{'data'}}),
$pair[0],
$i / 2)
unless (defined $time);
$pair[0] = $time;
push @new_data, [ @pair ];
}
my @sorted = sort {@{$a}[0] <=> @{$b}[0]} @new_data;
# if stacked, we accumulate the
if (($self->{config}->{stacked}) && (@{$self->{'data'}})) {
my $prev = $self->{'data'}->[@{$self->{'data'}} - 1]->{pairs};
# check our length matches previous
croak sprintf("Series %d can not be stacked on previous series. Mismatched length.",
scalar(@{$self->{'data'}}))
unless (scalar(@sorted) == scalar(@$prev));
for (my $i = 0; $i < @sorted; $i++) {
# check the time value matches
croak sprintf("Series %d can not be stacked on previous series. Mismatched timestamp at sample %d (time %s).",
scalar(@{$self->{'data'}}),
$i,
HTTP::Date::time2iso($sorted[$i][0]))
unless ($sorted[$i][0] == $prev->[$i][0]);
$sorted[$i][1] += $prev->[$i][1];
}
}
my %store = (
'pairs' => \@sorted,
);
$store{'title'} = $conf->{'title'} if defined $conf->{'title'};
push (@{$self->{'data'}},\%store);
return 1;
}
#This is for internal use only, so we don't have to pass the time zone around.
#It gets local()ed below before it's used.
our $_TIME_ZONE;
sub burn {
my ($self, @args) = @_;
local $_TIME_ZONE = $self->{'config'}{'timescale_time_zone'};
return $self->SUPER::burn(@args);
}
# Helper function for doing date/time calculations
# Current implementations of DateTime can be slow :-(
sub dateadd {
my ($epoch,$value,$unit) = @_;
my $dt = DateTime->from_epoch(epoch => $epoch);
#This should have been local()ed prior to getting here.
if ( $_TIME_ZONE ) {
$dt->set_time_zone( $_TIME_ZONE );
}
$dt->add( $unit => $value );
return $dt->epoch();
}
# override calculations to set a few calculated values, mainly for scaling
sub calculations {
my $self = shift;
# Need to set the timezone in order for x-axis labels to be
# formatted correctly by strftime:
$ENV{'TZ'} = $self->{config}->{timescale_time_zone};
POSIX::tzset() if ($] lt '5.008009');
# run through the data and calculate maximum and minimum values
my ($max_key_size,$max_time,$min_time,$max_value,$min_value,$max_x_label_length,$x_label);
foreach my $dataset (@{$self->{data}}) {
$max_key_size = length($dataset->{title}) if ((!defined $max_key_size) || ($max_key_size < length($dataset->{title})));
foreach my $pair (@{$dataset->{pairs}}) {
$max_time = $pair->[0] if ((!defined $max_time) || ($max_time < $pair->[0]));
$min_time = $pair->[0] if ((!defined $min_time) || ($min_time > $pair->[0]));
$max_value = $pair->[1] if (($pair->[1] ne '') && ((!defined $max_value) || ($max_value < $pair->[1])));
$min_value = $pair->[1] if (($pair->[1] ne '') && ((!defined $min_value) || ($min_value > $pair->[1])));
$x_label = strftime($self->{config}->{x_label_format},localtime($pair->[0]));
$max_x_label_length = length($x_label) if ((!defined $max_x_label_length) || ($max_x_label_length < length($x_label)));
}
}
$self->{calc}->{max_key_size} = $max_key_size;
$self->{calc}->{max_time} = $max_time;
$self->{calc}->{min_time} = $min_time;
$self->{calc}->{max_value} = $max_value;
$self->{calc}->{min_value} = $min_value;
$self->{calc}->{max_x_label_length} = $max_x_label_length;
# Calc the x axis scale values
$self->{calc}->{min_timescale_value} = ($self->_is_valid_config('min_timescale_value')) ? str2time($self->{config}->{min_timescale_value}) : $min_time;
$self->{calc}->{max_timescale_value} = ($self->_is_valid_config('max_timescale_value')) ? str2time($self->{config}->{max_timescale_value}) : $max_time;
$self->{calc}->{timescale_range} = $self->{calc}->{max_timescale_value} - $self->{calc}->{min_timescale_value};
# Calc the y axis scale values
$self->{calc}->{min_scale_value} = ($self->_is_valid_config('min_scale_value')) ? $self->{config}->{min_scale_value} : $min_value;
$self->{calc}->{max_scale_value} = ($self->_is_valid_config('max_scale_value')) ? $self->{config}->{max_scale_value} : $max_value;
$self->{calc}->{scale_range} = $self->{calc}->{max_scale_value} - $self->{calc}->{min_scale_value};
my ($range,$division,$precision);
if ($self->_is_valid_config('scale_divisions')) {
$division = $self->{config}->{scale_divisions};
if ($division >= 1) {
$precision = 0;
}
else {
$precision = length($division) - 2;
}
}
else {
# Find divisions, format and range
($range,$division,$precision) = $self->_range_calc($self->{calc}->{scale_range});
# If a max value hasn't been set we can set a revised range and max value
if (! $self->_is_valid_config('max_scale_value')) {
$self->{calc}->{max_scale_value} = $self->{calc}->{min_scale_value} + $range;
$self->{calc}->{scale_range} = $self->{calc}->{max_scale_value} - $self->{calc}->{min_scale_value};
}
}
$self->{calc}->{scale_division} = $division;
$self->{calc}->{y_label_format} = ($self->_is_valid_config('y_label_format')) ? $self->{config}->{y_label_format} : "%.${precision}f";
$self->{calc}->{data_value_format} = ($self->_is_valid_config('data_value_format')) ? $self->{config}->{data_value_format} : "%.${precision}f";
}
1;
__DATA__
[% USE date %]
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]
synopsis_bar_horizontal.pl 100644 000765 000024 671 13617237165 21431 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/script use lib qw(lib);
use SVG::TT::Graph::BarHorizontal;
my @fields = qw(Jan Feb Mar);
my @data_sales_02 = qw(12 45 21);
my $graph = SVG::TT::Graph::BarHorizontal->new({
'height' => '500',
'width' => '300',
'fields' => \@fields,
'compress' => 0,
});
$graph->add_data({
'data' => \@data_sales_02,
'title' => 'Sales 2002',
});
#print "Content-type: image/svg+xml\r\n\r\n";
print $graph->burn();
BarHorizontal.pm 100755 000765 000024 60427 13617237165 20565 0 ustar 00leo staff 000000 000000 SVG-TT-Graph-1.04/lib/SVG/TT/Graph package SVG::TT::Graph::BarHorizontal;
use strict;
use Carp;
use SVG::TT::Graph;
use base qw(SVG::TT::Graph);
our $VERSION = $SVG::TT::Graph::VERSION;
our $TEMPLATE_FH = \*DATA;
=head1 NAME
SVG::TT::Graph::BarHorizontal - Create presentation quality SVG horizontal bar graphs easily
=head1 SYNOPSIS
use SVG::TT::Graph::BarHorizontal;
my @fields = qw(Jan Feb Mar);
my @data_sales_02 = qw(12 45 21);
my $graph = SVG::TT::Graph::BarHorizontal->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();
=head1 DESCRIPTION
This object aims to allow you to easily create high quality
SVG horitonzal 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.
=head1 METHODS
=head2 new()
use SVG::TT::Graph::BarHorizontal;
# Field names along the X axis
my @fields = qw(Jan Feb Mar);
my $graph = SVG::TT::Graph::BarHorizontal->new({
# Required
'fields' => \@fields,
# Optional - defaults shown
'height' => '500',
'width' => '300',
'show_data_values' => 1,
'scale_divisions' => '',
'min_scale_value' => '0',
'bar_gap' => 1,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'show_y_labels' => 1,
'scale_integers' => 0,
'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, only 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.
=head2 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,
but the likelihood is you should be using SVG::TT::Graph::Line
as it won't look great!
=head2 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.
=head2 burn()
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.
=head2 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.
=over 4
=item 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.
=item width()
Set the width of the graph box, this is the total width
of the SVG box created - not the graph it self which auto
scales to fix the space.
=item compress()
Whether or not to compress the content of the SVG file (Compress::Zlib required).
=item tidy()
Whether or not to tidy the content of the SVG file (XML::Tidy required).
=item 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:" 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.
=item random_colors()
Use random colors in the internal stylesheet
=item show_data_values()
Show the value of each element of data on the graph
=item bar_gap()
Whether to have a gap between the bars or not, default
is '1', set to '0' if you don't want gaps.
=item 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.
=item 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.
=item 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'.
=item 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.
=item 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.
=item scale_divisions()
This defines the gap between markers on the X axis,
default is a 10th of the max_value, e.g. you will have
10 markers on the X axis. NOTE: do not set this too
low - you are limited to 999 markers, after that the
graph won't generate.
=item show_x_title()
Whether to show the title under the X axis labels,
default is 0, set to '1' to show.
=item x_title()
What the title under X axis should be, e.g. 'Months'.
=item show_y_title()
Whether to show the title under the Y axis labels,
default is 0, set to '1' to show.
=item 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.
=item y_title()
What the title under Y axis should be, e.g. 'Sales in thousands'.
=item show_graph_title()
Whether to show a title on the graph,
default is 0, set to '1' to show.
=item graph_title()
What the title on the graph should be.
=item show_graph_subtitle()
Whether to show a subtitle on the graph,
default is 0, set to '1' to show.
=item graph_subtitle()
What the subtitle on the graph should be.
=item key()
Whether to show a key, defaults to 0, set to
'1' if you want to show it.
=item key_position()
Where the key should be positioned, defaults to
'right', set to 'bottom' if you want to move it.
=item x_label_formatter ()
A callback subroutine which will format a label on the x axis. For example:
$graph->x_label_formatter( sub { return '$' . $_[0] } );
=item y_label_formatter()
A callback subroutine which will format a label on the y axis. For example:
$graph->y_label_formatter( sub { return '$' . $_[0] } );
=item show_path_title()
Whether to add the title attribute to the data path tags,
which will show "tooltips" when hovering over the bar area.
=item show_title_fields()
Whether to show field values as title elements in path tag,
defaults to 0, set to '1' to turn on. Suggest on single
add_data graphs, for overlapping graphs leave off to see
the title value used in the add_data call.
=back
=head1 EXAMPLES
For examples look at the project home page
http://leo.cuckoo.org/projects/SVG-TT-Graph/
=head1 EXPORT
None by default.
=head1 SEE ALSO
L,
L,
L,
L,
L,
L,
L,
L,
L
=cut
sub _init {
my $self = shift;
croak "fields was not supplied or is empty"
unless defined $self->{'config'}->{fields}
&& ref($self->{'config'}->{fields}) eq 'ARRAY'
&& scalar(@{$self->{'config'}->{fields}}) > 0;
}
sub _set_defaults {
my $self = shift;
my %default = (
'width' => '500',
'height' => '300',
'random_colors' => 0,
'style_sheet' => '',
'show_data_values' => 1,
'min_scale_value' => '0',
'scale_divisions' => '',
'bar_gap' => 1,
'show_x_labels' => 1,
'stagger_x_labels' => 0,
'show_y_labels' => 1,
'scale_integers' => 0,
'x_label_formatter' => sub { return @_ },
'y_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', # bottom or right
);
while( my ($key,$value) = each %default ) {
$self->{config}->{$key} = $value;
}
}
1;
__DATA__
[% stylesheet = 'included' %]
[% IF config.style_sheet && config.style_sheet != '' && config.style_sheet.substr(0,7) != 'inline:' %]
[% ELSIF config.style_sheet && config.style_sheet.substr(0,7) == 'inline:'%]
[% stylesheet = 'inline'
style_inline = config.style_sheet.substr(7) %]
[% ELSE %]
[% stylesheet = 'excluded' %]
[% END %]