SVG-TT-Graph-1.04000755000765000024 013617237165 12537 5ustar00leostaff000000000000README100644000765000024 56413617237165 13465 0ustar00leostaff000000000000SVG-TT-Graph-1.04This 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. Changes100755000765000024 1505713617237165 14146 0ustar00leostaff000000000000SVG-TT-Graph-1.04Revision 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [%# include default stylesheet if none specified %] [% END %] [% IF config.key %] [% END %] [%# get dimensions of actual graph area (NOT SVG area) %] [% w = config.width %] [% h = config.height %] [%# set start/default coords of graph %] [% x = 0 %] [% y = 0 %] [% char_width = 8 %] [% half_char_height = 2.5 %] [%# reduce height of graph area if there is labelling on x axis %] [% IF config.show_x_labels %][% h = h - 20 %][% END %] [%# reduce height if x labels are rotated %] [% x_label_allowance = 0 %] [% IF config.rotate_x_labels %] [% x_label_allowance = (calc.max_x_label_length * char_width) - 20 %] [% h = h - x_label_allowance %] [% END %] [%# stagger x labels if overlapping occurs %] [% stagger = 0 %] [% IF config.show_x_labels && config.stagger_x_labels %] [% stagger = 17 %] [% h = h - stagger %] [% END %] [% IF config.show_x_title %][% h = h - 25 - stagger %][% END %] [%# pad top of graph if y axis has data labels so labels do not get chopped off %] [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %] [%# reduce height if graph has title or subtitle %] [% IF config.show_graph_title %][% h = h - 25 %][% y = y + 25 %][% END %] [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %] [%# reduce graph dimensions if there is a KEY %] [% key_box_size = 12 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% w = w - (calc.max_key_size * (char_width - 1)) - (key_box_size * 3 ) %] [% ELSIF config.key && config.key_position == 'bottom' %] [% IF data.size < 4 %] [% h = h - ((data.size + 1) * (key_box_size + key_padding))%] [% ELSE %] [% h = h - (4 * (key_box_size + key_padding))%] [% END %] [% END %] [%# base line %] [% base_line = h + y %] [%# find the string length of max value %] [% max_y_length = calc.max_yscale_value.length %] [%# label width in pixels %] [% max_y_length_px = max_y_length * char_width %] [%# If the y labels are shown but the size of the x labels are small, pad for y labels %] [%# reduce width of graph area if there is large labelling on x axis %] [% space_b4_y_axis = (calc.min_xscale_value.length / 2) * char_width %] [% IF config.show_x_labels %] [% IF config.key && config.key_position == 'right' %] [% w = w - space_b4_y_axis %] [% ELSE %] [% w = w - (space_b4_y_axis * 2) %] [% END %] [% x = x + space_b4_y_axis %] [% ELSIF config.show_data_values %] [% w = w - (max_y_length_px * 2) %] [% x = x + max_y_length_px %] [% END %] [% IF config.show_y_labels && space_b4_y_axis < max_y_length_px %] [% IF max_y_length < 2 %] [% w = w - (max_y_length * (char_width * 2)) %] [% x = x + (max_y_length * (char_width * 2)) %] [% ELSE %] [% w = w - max_y_length_px %] [% x = x + max_y_length_px %] [% END %] [% ELSIF config.show_y_labels && !config.show_x_labels %] [% w = w - max_y_length_px %] [% x = x + max_y_length_px %] [% END %] [% IF config.show_y_title %] [% w = w - 25 %] [% x = x + 25 %] [% END %] [%# Missing data spans %] [% max_x_span = 0 %] [% IF config.max_x_span %] [% max_x_span = config.max_x_span %] [% END %] [%# graph bg and clipping regions for lines/fill and clip extended to included data labels %] [% dx = calc.xscale_range %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% dw = w / dx %] [% IF config.show_x_labels %] [% x_value_txt = config.x_label_formatter(calc.min_xscale_value) %] [% x_value_txt %] [% last_label = calc.min_xscale_value %] [% IF config.xscale_divisions %] [% xscale_division = config.xscale_divisions %] [% x_value = calc.min_xscale_value + xscale_division %] [% count = 0 %] [% WHILE ((x_value > calc.min_xscale_value) && ((x_value < calc.max_xscale_value))) %] [% x_value_txt = config.x_label_formatter(x_value) %] [% xpos = (dw * (x_value - calc.min_xscale_value)) + x %] [% IF (config.stagger_x_labels && ((count % 2) == 0)) %] [% x_value_txt %] [% ELSE %] [% x_value_txt %] [% END %] [% last_label = x_value %] [% x_value = x_value + xscale_division %] [% count = count + 1 %] [% LAST IF (count >= 999) %] [% END %] [% END %] [% IF calc.max_xscale_value != last_label %] [% x_value_txt = config.x_label_formatter(calc.max_xscale_value) %] [% IF (config.stagger_x_labels && ((count % 2) == 0)) %] [% x_value_txt %] [% ELSE %] [% x_value_txt %] [% END %] [% END %] [% END %] [%# how much padding between largest bar and top of graph %] [% top_pad = h / 40 %] [% dy = calc.yscale_range %] [% IF dy == 0 %] [% dy = 1 %] [% END %] [% dh = (h - top_pad) / dy %] [% count = 0 %] [% last_label = '' %] [% IF (calc.min_yscale_value > calc.max_yscale_value) %] [% y_value = calc.max_yscale_value %] [% reversed = 1 %] [% ELSE %] [% y_value = calc.min_yscale_value %] [% reversed = 0 %] [% END %] [% IF config.show_y_labels %] [% WHILE ((y_value == calc.min_yscale_value) || (y_value == calc.max_yscale_value) || ((y_value > calc.min_yscale_value) && (y_value < calc.max_yscale_value)) || ((y_value > calc.max_yscale_value) && (y_value < calc.min_yscale_value) && reversed )) %] [%- next_label = y_value FILTER format(calc.y_label_format) -%] [%- next_label = config.y_label_formatter(next_label) -%] [%- IF ((count == 0) && (reversed == 0)) -%] [%# no stroke for first line unless reversed %] [% next_label %] [%- ELSE -%] [% IF next_label != last_label %] [% next_label %] [% END %] [%- END -%] [%- y_value = y_value + calc.yscale_division -%] [%- last_label = next_label -%] [%- count = count + 1 -%] [%- LAST IF (count >= 999) -%] [% END %] [% END %] [% IF config.show_x_title %] [% IF !config.show_x_labels %] [% y_xtitle = 15 %] [% ELSE %] [% y_xtitle = 35 %] [% END %] [% config.x_title %] [% END %] [% IF config.show_y_title %] [% config.y_title %] [% END %] [% line = data.size %] [% FOREACH dataset = data.reverse %] [% IF config.area_fill %] [%# create alternate fill first (so line can overwrite if necessary) %] [% xcount = 0 %] [% FOREACH pair = dataset.pairs %] [%- IF ((pair.0 >= calc.min_xscale_value) && (pair.0 <= calc.max_xscale_value)) -%] [%- IF xcount == 0 -%][% lastx = pair.0 %] [% END %] [% END %] [% xcount = 0 %] [% FOREACH pair = dataset.pairs %] [% IF ((pair.0 >= calc.min_xscale_value) && (pair.0 <= calc.max_xscale_value)) %] [%- IF xcount == 0 -%][%- lastx = pair.0 -%] [% END %] [% IF config.show_data_points || config.show_data_values %] [% FOREACH pair = dataset.pairs %] [% IF ((pair.0 >= calc.min_xscale_value) && (pair.0 <= calc.max_xscale_value)) %] [% IF config.show_data_points %] [% END %] [% IF config.show_data_values %] [%# datavalue shown %] [% IF (pair.2.defined) && (pair.2 != '') %][% point_label = pair.2 %][% ELSE %][% point_label = pair.1 FILTER format(calc.data_value_format) %][% END %] [% point_label %] [% END %] [% END %] [% END %] [% END %] [% line = line - 1 %] [% END %] [% key_count = 1 %] [% IF config.key && config.key_position == 'right' %] [% FOREACH dataset = data %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [%# calc y position of start of key %] [% y_key = base_line %] [%# consider x title %] [% IF config.show_x_title %][% y_key = base_line + 25 %][% END %] [%# consider x label rotation and stagger %] [% IF config.rotate_x_labels && config.show_x_labels %] [% y_key = y_key + x_label_allowance %] [% ELSIF config.show_x_labels && stagger < 1 %] [% y_key = y_key + 20 %] [% END %] [% y_key_start = y_key %] [% x_key = x %] [% FOREACH dataset = data %] [% IF key_count == 4 || key_count == 7 || key_count == 10 %] [%# wrap key every 3 entries %] [% x_key = x_key + 200 %] [% y_key = y_key - (key_box_size * 4) - 2 %] [% END %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %] synopsis_line.pl100644000765000024 104113617237165 17353 0ustar00leostaff000000000000SVG-TT-Graph-1.04/scriptuse 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.pm100755000765000024 5526613617237165 16531 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [% END %] [% IF config.rollover_values %] [% END %] [% w = config.width %] [% h = config.height %] [% Pi = 3.14159 %] [% total = 0 %] [% count = 0 %] [% min_value = 99999999999 %] [% max_value = 0 %] [% FOREACH field = config.fields %] [% total = total + data.0.data.$field %] [% count = count + 1 %] [% IF min_value > data.0.data.$field && data.0.data.$field != '' %] [% min_value = data.0.data.$field %] [% END %] [% IF max_value < data.0.data.$field && data.0.data.$field != '' %] [% max_value = data.0.data.$field %] [% END %] [% END %] [% IF config.show_graph_title %][% h = h - 25 %][% END %] [% IF config.show_graph_subtitle %][% y = y + 10 %][% END %] [% IF config.show_graph_subtitle %][% y = y + 10 %][% END %] [% x = w / 2 %] [% y = h / 2 %] [% IF config.show_graph_title %][% y = y + 15 %][% END %] [% IF config.show_graph_subtitle %][% y = y + 10 %][% END %] [% padding = 30 %] [% IF w >= h %] [% r = (h / 2) - padding %] [% IF config.key %] [% key_position = 'h' %] [% x_key_start = 30 %] [% IF config.key_placement == 'R' %] [% x = x - r / 3 %] [% IF x < r %] [% r = r - (w / 8) %] [% END %] [% ELSE %] [% x = x + r / 3 %] [% IF r > (w - x) && x > (w / 2) %] [% r = r - (w / 8) %] [% END %] [% END %] [% END %] [% ELSE %] [% r = (w / 2) - padding %] [% IF config.key %] [% key_position = 'v' %] [% y_key_start = 40 %] [% IF config.key_placement == 'B' %] [% y = y - (r / 2) %] [% IF y < r %] [% r = r - (h / 8) %] [% END %] [% ELSE %] [% y = y + (r / 2) %] [% IF r > (h - y) && y > (h / 2) %] [% r = r - (h / 8) %] [% END %] [% END %] [% END %] [% END %] [% IF config.expanded OR config.expand_greatest %] [% e = 10 %] [% ELSE %] [% e = 0 %] [% END %] [% IF config.show_shadow %] [% IF config.shadow_size && config.shadow_size != '' %] [% shadow_size = r + ((r / 100) * config.shadow_size) %] [% ELSE %] [% shadow_size = r %] [% END %] [% IF !config.expanded && !config.expand_greatest %] [% END %] [% END %] [% px_start = x + r %] [% pmin_scale_value = y %] [% values = 0 %] [% values_half = 0 %] [% last_value_half = 0 %] [% IF config.show_percent && config.show_data_labels %] [% wedge_text_pad = 20 %] [% ELSE %] [% wedge_text_pad = 5 %] [% END %] [% count = 1 %] [% FOREACH field = config.fields %] [% FOREACH dataset = data %] [% value = data.0.data.$field %] [% value_half = data.0.data.$field / 2 %] [% IF total == 0 %] [% percent = 0 %] [% ELSE %] [% percent = (100 / total) * value FILTER format('%2.0f')%] [% END %] [% values = values + value %] [% IF count == 1 %] [% values_half = values_half + value_half %] [% ELSE %] [% values_half = values_half + last_value_half + value_half %] [% END %] [% IF total == 0 %] [% degrees = 0 %] [% degrees_half = 0 %] [% ELSE %] [% degrees = (values / total) * 360 %] [% degrees_half = (values_half / total) * 360 %] [% END %] [% radians = degrees * (Pi / 180) %] [% radians_half = degrees_half * (Pi / 180) %] [% px_end = r * cos(radians) FILTER format('%02.10f') %] [% py_end = r * sin(radians) FILTER format('%02.10f') %] [% px_mid = r * cos(radians_half) FILTER format('%02.10f') %] [% py_mid = r * sin(radians_half) FILTER format('%02.10f') %] [% IF config.expanded && !config.expand_greatest %] [% re = r / e %] [% xe = re * cos(radians_half) FILTER format('%02.10f') %] [% ye = re * sin(radians_half) FILTER format('%02.10f') %] [% data.0.data.$field %] - [% field %] [% ELSE %] >[% data.0.data.$field %] - [% data.0.title %] [% END %] [% ELSE %] /> [% END %] [% text_x_offset = 0 %] [% text_y_offset = 0 %] [% IF px_mid >= x && px_mid <= y %] [% text_x_offset = wedge_text_pad %] [% text_y_offset = wedge_text_pad %] [% ELSIF px_mid <= x && py_mid <= y %] [% text_x_offset = 0-wedge_text_pad %] [% text_y_offset = wedge_text_pad %] [% ELSIF px_mid <= x && py_mid >= y %] [% text_x_offset = wedge_text_pad %] [% text_y_offset = 0-wedge_text_pad %] [% ELSE %] [% text_x_offset = 0-wedge_text_pad %] [% text_y_offset = 0-wedge_text_pad %] [% END %] [% IF config.show_data_labels %][% field %][% END %][% IF config.show_actual_values %][[% data.0.data.$field %]][% END %][% IF config.show_percent %][% percent %]%[% END %] [% IF config.rollover_values %] [% field %] [% END %] [% px_start = x + px_end %] [% pmin_scale_value = y + py_end %] [% last_value_half = value_half %] [% count = count + 1 %] [% END %] [% END %] [% IF config.key %] [% key_box_size = 12 %] [% key_count = 1 %] [% key_padding = 5 %] [% x_off = 0 %] [% y_off = 0 %] [% IF key_position == 'h' %] [% IF config.key_placement == 'R' %] [% x_off = x + r + x_key_start %] [% y_off = (y - r) %] [% ELSE %] [% x_off = x_key_start %] [% y_off = (y - r) %] [% END %] [% ELSIF key_position == 'v' %] [% IF w < h && config.key_placement == 'R' OR config.key_placement == 'L' %] [% config.key_placement = 'T' %] [% END %] [% y_key = padding %] [% x_key = padding %] [% IF key_count == 7 || key_count == 13 %] [% x_key = x_key + (w / 3) %] [% y_key = y_key - (key_box_size * 8) - 6 %] [% END %] [% IF config.key_placement == 'T' %] [% x_off = x_key %] [% y_off = y_key %] [% ELSE %] [% x_off = x_key %] [% y_off = (r * 2) + (padding * 2) + y_key %] [% END %] [% END %] [% FOREACH field = config.fields %] [% IF total == 0 %] [% percent = 0 %] [% ELSE %] [% percent = (100 / total) * data.0.data.$field FILTER format('%2.0f')%] [% END %] [% IF config.show_key_data_labels %][% field %][% END %] [% IF config.show_key_actual_values %][[% data.0.data.$field %]][% END %] [% IF config.show_key_percent %][% percent %]%[% END %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %] Bar.pm100755000765000024 6412613617237165 16513 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [% END %] [% w = config.width %] [% h = config.height %] [% x = 0 %] [% y = 0 %] [% char_width = 9 %] [% half_char_height = 2.5 %] [% min_value = 99999999999 %] [% max_value = 0 %] [% max_key_size = 0 %] [% max_x_label_size = 0 %] [% FOREACH field = config.fields %] [% IF max_x_label_size < field.length %] [% max_x_label_size = field.length %] [% END %] [% max_y_value = 0 %] [% FOREACH dataset = data %] [% IF dataset.data.$field != '' %] [% max_y_value = max_y_value + dataset.data.$field %] [% END %] [% IF dataset.title %] [% IF max_key_size < dataset.title.length %] [% max_key_size = dataset.title.length %] [% END %] [% END %] [% END %] [% IF min_value > max_y_value %] [% min_value = max_y_value %] [% END %] [% IF max_value < max_y_value %] [% max_value = max_y_value %] [% END %] [% END %] [% IF config.show_x_labels %][% h = h - 20 %][% END %] [% max_x_label_length = 0 %] [% IF config.rotate_x_labels %] [% max_x_label_length = max_x_label_size * char_width %] [% h = h - max_x_label_length %] [% END %] [% stagger = 0 %] [% IF config.stagger_x_labels %] [% stagger = 17 %] [% h = h - stagger %] [% END %] [% IF config.show_x_title %][% h = h - 25 - stagger %][% END %] [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %] [% IF config.show_graph_title %][% h = h - 25 %][% y = y + 25 %][% END %] [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %] [% key_box_size = 12 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% w = w - (max_key_size * (char_width - 1)) - (key_box_size * 3 ) %] [% ELSIF config.key && config.key_position == 'bottom' %] [% IF data.size < 4 %] [% h = h - ((data.size + 1) * (key_box_size + key_padding))%] [% ELSE %] [% h = h - (4 * (key_box_size + key_padding))%] [% END %] [% END %] [% IF config.min_scale_value || config.min_scale_value == '0' %] [% min_scale_value = config.min_scale_value %] [% ELSE %] [% min_scale_value = min_value %] [% END %] [% IF config.max_scale_value || config.max_scale_value == '0' %] [% max_scale_value = config.max_scale_value %] [% ELSE %] [% max_scale_value = max_value %] [% END %] [% base_line = h + y %] [% IF (max_scale_value - min_scale_value) == 0 %] [% top_pad = 10 %] [% ELSE %] [% top_pad = (max_scale_value - min_scale_value) / 20 %] [% END %] [% scale_range = (max_scale_value + top_pad) - min_scale_value %] [% IF config.scale_divisions %] [% scale_division = config.scale_divisions %] [% ELSE %] [% scale_division = scale_range / 10 FILTER format('%2.01f') %] [% END %] [% IF config.scale_integers %] [% IF scale_division < 1 %] [% scale_division = 1 %] [% ELSIF scale_division.match('.') %] [% scale_division = scale_division FILTER format('%2.0f') %] [% END %] [% END %] [% dx = config.fields.size %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% data_widths_x = w / dx %] [% dw = data_widths_x FILTER format('%2.02f') %] [% half_label_width = (config.fields.0.length / 2) * char_width %] [% space_b4_y_axis = 0 %] [% IF half_label_width > (dw / 2) %] [% space_b4_y_axis = half_label_width - (dw / 2) %] [% IF config.key && config.key_position == 'right' %] [% w = w - space_b4_y_axis %] [% ELSE %] [% w = w - (space_b4_y_axis * 2) %] [% END %] [% x = x + space_b4_y_axis %] [% END %] [% max_value_length = max_value.length %] [% max_value_length_px = max_value_length * char_width %] [% IF config.show_y_labels && space_b4_y_axis < max_value_length_px %] [% IF max_value_length < 2 %] [% w = w - (max_value_length * (char_width * 2)) - char_width %] [% x = x + (max_value_length * (char_width * 2)) + char_width %] [% ELSE %] [% w = w - max_value_length_px + char_width %] [% x = x + max_value_length_px + char_width %] [% END %] [% END %] [% IF config.show_y_title && space_b4_y_axis < max_value_length_px %] [% w = w - 25 %] [% x = x + 25 %] [% END %] [% dx = config.fields.size %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% data_widths_x = w / dx %] [% dw = data_widths_x.match('(\d+[\.\d\d])').0 %] [% i = dw %] [% count = 0 %] [% IF config.bar_gap %] [% bar_gap = 10 %] [% IF dw < bar_gap %] [% bar_gap = dw / 2 %] [% END %] [% ELSE %] [% bar_gap = 0 %] [% END %] [% stagger_count = 0 %] [% IF config.show_x_labels %] [% FOREACH field = config.fields %] [% field_txt = config.x_label_formatter(field) %] [% IF count == 0 %] [% field_txt %] [% i = i - dw %] [% ELSE %] [% IF stagger_count == 2 %] [% field_txt %] [% stagger_count = 0 %] [% ELSE %] [% field_txt %] [% END %] [% END %] [% i = i + dw %] [% count = count + 1 %] [% stagger_count = stagger_count + 1 %] [% END %] [% END %] [% dy = scale_range / scale_division %] [% IF dy == 0 %] [% dy = 1 %] [% END %] [% y_marker_height = h / dy %] [% dy = y_marker_height.match('(\d+[\.\d\d])').0 %] [% count = 0 %] [% y_value = min_scale_value %] [% IF config.show_y_labels %] [% WHILE (dy * count) < h %] [% y_value_txt = config.y_label_formatter(y_value) %] [% IF count == 0 %] [% y_value_txt %] [% ELSE %] [% y_value_txt %] [% END %] [% y_value = y_value + scale_division %] [% count = count + 1 %] [% END %] [% END %] [% IF config.show_x_title %] [% IF !config.show_x_labels %] [% y_xtitle = 15 %] [% ELSE %] [% y_xtitle = 35 %] [% END %] [% config.x_title %] [% END %] [% IF config.show_y_title %] [% IF config.y_title_text_direction == 'tb' %] [% config.y_title %] [% ELSE %] [% config.y_title %] [% END %] [% END %] [% bar_width = dw - bar_gap %] [% divider = dy / scale_division %] [% xcount = 0 %] [% FOREACH field = config.fields %] [% dcount = 1 %] [% start_x = base_line %] [% FOREACH dataset = data %] [% IF config.show_path_title %] [% IF config.show_title_fields %] [% dataset.data.$field %] - [% field %] [% ELSE %] [% dataset.data.$field %] - [% dataset.title %] [% END %] [% ELSE %] [% END %] [% IF config.show_data_values %] [% dataset.data.$field %] [% END %] [% start_x = start_x - (dataset.data.$field * divider) %] [% dcount = dcount + 1 %] [% END %] [% xcount = xcount + 1 %] [% END %] [% key_count = 1 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% FOREACH dataset = data %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [% y_key = base_line %] [% IF config.show_x_title %][% y_key = y_key + 25 %][% END %] [% IF config.rotate_x_labels && config.show_x_labels %] [% y_key = y_key + max_x_label_length %] [% ELSIF config.show_x_labels && stagger < 1 %] [% y_key = y_key + 20 %] [% END %] [% y_key_start = y_key %] [% x_key = x %] [% FOREACH dataset = data %] [% IF key_count == 4 || key_count == 7 || key_count == 10 %] [% x_key = x_key + 200 %] [% y_key = y_key - (key_box_size * 4) - 2 %] [% END %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %] Line.pm100755000765000024 6367013617237165 16701 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [% END %] [% w = config.width %] [% h = config.height %] [% x = 0 %] [% y = 0 %] [% char_width = 9 %] [% half_char_height = 2.5 %] [% IF config.stacked %] [% FOREACH field = config.fields %] [% cumulative = 0 %] [% FOREACH dataset = data %] [% IF dataset.data.$field != '' %] [% cumulative = cumulative + dataset.data.$field %] [% END %] [% dataset.data.$field = cumulative %] [% END %] [% END %] [% END %] [% min_value = 99999999999 %] [% max_value = 0 %] [% max_key_size = 0 %] [% max_x_label_size = 0 %] [% FOREACH field = config.fields %] [% IF max_x_label_size < field.length %] [% max_x_label_size = field.length %] [% END %] [% FOREACH dataset = data %] [% IF min_value > dataset.data.$field && dataset.data.$field != '' %] [% min_value = dataset.data.$field %] [% END %] [% IF max_value < dataset.data.$field && dataset.data.$field != '' %] [% max_value = dataset.data.$field %] [% END %] [% IF dataset.title %] [% IF max_key_size < dataset.title.length %] [% max_key_size = dataset.title.length %] [% END %] [% END %] [% END %] [% END %] [% IF config.show_x_labels %][% h = h - 20 %][% END %] [% max_x_label_length = 0 %] [% IF config.rotate_x_labels %] [% max_x_label_length = max_x_label_size * char_width %] [% h = h - max_x_label_length %] [% END %] [% stagger = 0 %] [% IF config.show_x_labels && config.stagger_x_labels %] [% stagger = 17 %] [% h = h - stagger %] [% END %] [% IF config.show_x_title %][% h = h - 25 - stagger %][% END %] [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %] [% IF config.show_graph_title %][% h = h - 25 %][% y = y + 25 %][% END %] [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %] [% key_box_size = 12 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% w = w - (max_key_size * (char_width - 1)) - (key_box_size * 3 ) %] [% ELSIF config.key && config.key_position == 'bottom' %] [% IF data.size < 4 %] [% h = h - ((data.size + 1) * (key_box_size + key_padding))%] [% ELSE %] [% h = h - (4 * (key_box_size + key_padding))%] [% END %] [% END %] [% IF config.min_scale_value || config.min_scale_value == '0' %] [% min_scale_value = config.min_scale_value %] [% ELSE %] [% min_scale_value = min_value %] [% END %] [% IF config.max_scale_value || config.max_scale_value == '0' %] [% max_scale_value = config.max_scale_value %] [% ELSE %] [% max_scale_value = max_value %] [% END %] [% base_line = h + y %] [% IF (max_scale_value - min_scale_value) == 0 %] [% top_pad = 10 %] [% ELSE %] [% top_pad = (max_scale_value - min_scale_value) / 20 %] [% END %] [% scale_range = (max_scale_value + top_pad) - min_scale_value %] [% IF config.scale_divisions %] [% scale_division = config.scale_divisions %] [% ELSE %] [% scale_division = scale_range / 10 FILTER format('%2.01f') %] [% END %] [% IF config.scale_integers %] [% IF scale_division < 1 %] [% scale_division = 1 %] [% ELSIF scale_division.match('.') %] [% scale_division = scale_division FILTER format('%2.0f') %] [% END %] [% END %] [% max_value_length = max_value.length %] [% max_value_length_px = max_value_length * char_width %] [% space_b4_y_axis = (config.fields.0.length / 2) * char_width %] [% IF config.show_x_labels %] [% IF config.key && config.key_position == 'right' %] [% w = w - space_b4_y_axis %] [% ELSE %] [% w = w - (space_b4_y_axis * 2) %] [% END %] [% x = x + space_b4_y_axis %] [% ELSIF config.show_data_values %] [% w = w - (max_value_length_px * 2) %] [% x = x + max_value_length_px %] [% END %] [% IF config.show_y_labels && space_b4_y_axis < max_value_length_px %] [% IF max_value_length < 2 %] [% w = w - (max_value_length * (char_width * 2)) %] [% x = x + (max_value_length * (char_width * 2)) %] [% ELSE %] [% w = w - max_value_length_px %] [% x = x + max_value_length_px %] [% END %] [% ELSIF config.show_y_labels && !config.show_x_labels %] [% w = w - max_value_length_px %] [% x = x + max_value_length_px %] [% END %] [% IF config.show_y_title %] [% w = w - 25 %] [% x = x + 25 %] [% END %] [% dx = config.fields.size %] [% dx = dx - 1 %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% data_widths_x = w / dx %] [% dw = data_widths_x FILTER format('%2.02f') %] [% i = dw %] [% count = 0 %] [% stagger_count = 0 %] [% IF config.show_x_labels %] [% FOREACH field = config.fields %] [% field_txt = config.x_label_formatter(field) %] [% IF count == 0 %] [% field_txt %] [% i = i - dw %] [% ELSE %] [% IF stagger_count == 2 %] [% field_txt %] [% stagger_count = 0 %] [% ELSE %] [% field_txt %] [% END %] [% END %] [% i = i + dw %] [% count = count + 1 %] [% stagger_count = stagger_count + 1 %] [% END %] [% END %] [% dy = scale_range / scale_division %] [% IF dy == 0 %] [% dy = 1 %] [% END %] [% y_marker_height = h / dy %] [% dy = y_marker_height.match('(\d+[\.\d\d])').0 %] [% count = 0 %] [% y_value = min_scale_value %] [% IF ((min_scale_value > max_scale_value) && (scale_division > 0)) %] [% scale_division = -1 * scale_division %] [% END %] [% IF config.show_y_labels %] [% WHILE (dy * count) < h %] [% y_value_txt = config.y_label_formatter(y_value) %] [% IF count == 0 %] [% y_value_txt %] [% ELSE %] [% y_value_txt %] [% END %] [% y_value = y_value + scale_division %] [% count = count + 1 %] [% END %] [% END %] [% IF config.show_x_title %] [% IF !config.show_x_labels %] [% y_xtitle = 15 %] [% ELSE %] [% y_xtitle = 35 %] [% END %] [% config.x_title %] [% END %] [% IF config.show_y_title %] [% IF config.y_title_text_direction == 'tb' %] [% config.y_title %] [% ELSE %] [% config.y_title %] [% END %] [% END %] [% divider = dy / scale_division %] [% line = data.size %] [% FOREACH dataset = data.reverse %] [% IF config.area_fill %] [% END %] [% IF config.show_data_points || config.show_data_values%] [% xcount = 0 %] [% FOREACH field = config.fields %] [% IF config.show_data_points %] [% END %] [% IF config.show_data_values %] [% dataset.data.$field %] [% END %] [% xcount = xcount + 1 %] [% END %] [% END %] [% line = line - 1 %] [% END %] [% key_count = 1 %] [% IF config.key && config.key_position == 'right' %] [% FOREACH dataset = data %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [% y_key = base_line %] [% IF config.show_x_title %] [% y_key = y_key + 25 %] [% END %] [% IF config.rotate_x_labels && config.show_x_labels %] [% y_key = y_key + max_x_label_length %] [% ELSIF config.show_x_labels && stagger < 1 %] [% y_key = y_key + 20 %] [% END %] [% y_key_start = y_key %] [% x_key = x %] [% FOREACH dataset = data %] [% IF key_count == 4 || key_count == 7 || key_count == 10 %] [% x_key = x_key + 200 %] [% y_key = y_key - (key_box_size * 4) - 2 %] [% END %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %] synopsis_bubble.pl100644000765000024 430013617237165 17660 0ustar00leostaff000000000000SVG-TT-Graph-1.04/scriptuse 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.pl100644000765000024 176113617237165 20054 0ustar00leostaff000000000000SVG-TT-Graph-1.04/scriptuse 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.pm100644000765000024 10356313617237165 17216 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [%# include default stylesheet if none specified %] [% END %] [% IF config.key %] [% END %] [%# get dimensions of actual graph area (NOT SVG area) %] [% w = config.width %] [% h = config.height %] [%# set start/default coords of graph %] [% x = 0 %] [% y = 0 %] [% char_width = 8 %] [% half_char_height = 2.5 %] [%# reduce height of graph area if there is labelling on x axis %] [% IF config.show_x_labels %][% h = h - 20 %][% END %] [%# reduce height if x labels are rotated %] [% x_label_allowance = 0 %] [% IF config.rotate_x_labels %] [% x_label_allowance = (calc.max_x_label_length * char_width) - 20 %] [% h = h - x_label_allowance %] [% END %] [%# stagger x labels if overlapping occurs %] [% stagger = 0 %] [% IF config.show_x_labels && config.stagger_x_labels %] [% stagger = 17 %] [% h = h - stagger %] [% END %] [% IF config.show_x_title %][% h = h - 25 - stagger %][% END %] [%# pad top of graph if y axis has data labels so labels do not get chopped off %] [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %] [%# reduce height if graph has title or subtitle %] [% IF config.show_graph_title %][% h = h - 25 %][% y = y + 25 %][% END %] [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %] [%# reduce graph dimensions if there is a KEY %] [% key_box_size = 12 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% w = w - (calc.max_key_size * (char_width - 1)) - (key_box_size * 3 ) %] [% ELSIF config.key && config.key_position == 'bottom' %] [% IF data.size < 4 %] [% h = h - ((data.size + 1) * (key_box_size + key_padding))%] [% ELSE %] [% h = h - (4 * (key_box_size + key_padding))%] [% END %] [% END %] [%# base line %] [% base_line = h + y %] [%# find the string length of max value %] [% max_y_length = calc.max_yscale_value.length %] [%# label width in pixels %] [% max_y_length_px = max_y_length * char_width %] [%# If the y labels are shown but the size of the x labels are small, pad for y labels %] [%# reduce width of graph area if there is large labelling on x axis %] [% space_b4_y_axis = (calc.min_xscale_value.length / 2) * char_width %] [% IF config.show_x_labels %] [% IF config.key && config.key_position == 'right' %] [% w = w - space_b4_y_axis %] [% ELSE %] [% w = w - (space_b4_y_axis * 2) %] [% END %] [% x = x + space_b4_y_axis %] [% ELSIF config.show_data_values %] [% w = w - (max_y_length_px * 2) %] [% x = x + max_y_length_px %] [% END %] [% IF config.show_y_labels && space_b4_y_axis < max_y_length_px %] [% IF max_y_length < 2 %] [% w = w - (max_y_length * (char_width * 2)) %] [% x = x + (max_y_length * (char_width * 2)) %] [% ELSE %] [% w = w - max_y_length_px %] [% x = x + max_y_length_px %] [% END %] [% ELSIF config.show_y_labels && !config.show_x_labels %] [% w = w - max_y_length_px %] [% x = x + max_y_length_px %] [% END %] [% IF config.show_y_title %] [% w = w - 25 %] [% x = x + 25 %] [% END %] [%# Missing data spans %] [% max_x_span = 0 %] [% IF config.max_x_span %] [% max_x_span = config.max_x_span %] [% END %] [%# scaling for bubbles %] [% wpad = w / 10 %] [% w = w - wpad * 2 %] [% h = h - wpad %] [% x = x + wpad %] [% y = y + wpad %] [%# graph bg and clipping regions for lines/fill and clip extended to included data labels %] [% IF config.diagonal_path %] [% END %] [% dx = calc.xscale_range %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% dw = w / dx %] [% IF config.show_x_labels %] [% x_value_txt = config.x_label_formatter(calc.min_xscale_value) %] [% x_value_txt %] [% last_label = calc.min_xscale_value %] [% IF config.xscale_divisions %] [% xscale_division = config.xscale_divisions %] [% x_value = calc.min_xscale_value + xscale_division %] [% count = 0 %] [% WHILE ((x_value > calc.min_xscale_value) && ((x_value < calc.max_xscale_value))) %] [% x_value_txt = config.x_label_formatter(x_value) %] [% xpos = (dw * (x_value - calc.min_xscale_value)) + x %] [% IF (config.stagger_x_labels && ((count % 2) == 0)) %] [% x_value_txt %] [% ELSE %] [% x_value_txt %] [% END %] [% last_label = x_value %] [% x_value = x_value + xscale_division %] [% count = count + 1 %] [% LAST IF (count >= 999) %] [% END %] [% END %] [% IF calc.max_xscale_value != last_label %] [% x_value_txt = config.x_label_formatter(calc.max_xscale_value) %] [% IF (config.stagger_x_labels && ((count % 2) == 0)) %] [% x_value_txt %] [% ELSE %] [% x_value_txt %] [% END %] [% END %] [% END %] [%# how much padding between largest bar and top of graph %] [% top_pad = config.pad_top_y_axis ? h / 40 : 0 %] [% dy = calc.yscale_range %] [% IF dy == 0 %] [% dy = 1 %] [% END %] [% dh = (h - top_pad) / dy %] [% count = 0 %] [% last_label = '' %] [% IF (calc.min_yscale_value > calc.max_yscale_value) %] [% y_value = calc.max_yscale_value %] [% reversed = 1 %] [% ELSE %] [% y_value = calc.min_yscale_value %] [% reversed = 0 %] [% END %] [% IF config.show_y_labels %] [% WHILE ((y_value == calc.min_yscale_value) || (y_value == calc.max_yscale_value) || ((y_value > calc.min_yscale_value) && (y_value < calc.max_yscale_value)) || ((y_value > calc.max_yscale_value) && (y_value < calc.min_yscale_value) && reversed )) %] [%- next_label = y_value FILTER format(calc.y_label_format) -%] [%- next_label = config.y_label_formatter(next_label) -%] [%- IF ((count == 0) && (reversed == 0)) -%] [%# no stroke for first line unless reversed %] [% next_label %] [%- ELSE -%] [% IF next_label != last_label %] [% next_label %] [% END %] [%- END -%] [%- y_value = y_value + calc.yscale_division -%] [%- last_label = next_label -%] [%- count = count + 1 -%] [%- LAST IF (count >= 999) -%] [% END %] [% END %] [% IF config.show_x_title %] [% IF !config.show_x_labels %] [% y_xtitle = 15 %] [% ELSE %] [% y_xtitle = 35 %] [% END %] [% config.x_title %] [% END %] [% IF config.show_y_title %] [% config.y_title %] [% END %] [% line = data.size %] [% FOREACH dataset = data.reverse %] [% FOREACH pair = dataset.pairs %] [% IF ((pair.0 >= calc.min_xscale_value) && (pair.0 <= calc.max_xscale_value)) %] [% IF config.show_data_values %] [%# datavalue shown %] [% IF (pair.2.defined) && (pair.2 != '') %][% point_label = pair.2 %][% ELSE %][% point_label = pair.1 FILTER format(calc.data_value_format) %][% END %] [% point_label %] [% END %] [% END %] [% END %] [% line = line - 1 %] [% END %] [% key_count = 1 %] [% IF config.key && config.key_position == 'right' %] [% FOREACH dataset = data %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [%# calc y position of start of key %] [% y_key = base_line %] [%# consider x title %] [% IF config.show_x_title %][% y_key = base_line + 25 %][% END %] [%# consider x label rotation and stagger %] [% IF config.rotate_x_labels && config.show_x_labels %] [% y_key = y_key + x_label_allowance %] [% ELSIF config.show_x_labels && stagger < 1 %] [% y_key = y_key + 20 %] [% END %] [% y_key_start = y_key %] [% x_key = x %] [% FOREACH dataset = data %] [% IF key_count == 4 || key_count == 7 || key_count == 10 %] [%# wrap key every 3 entries %] [% x_key = x_key + 200 %] [% y_key = y_key - (key_box_size * 4) - 2 %] [% END %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %] synopsis_bar_line.pl100644000765000024 106713617237165 20207 0ustar00leostaff000000000000SVG-TT-Graph-1.04/scriptuse 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.pl100755000765000024 524713617237165 20164 0ustar00leostaff000000000000SVG-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.pm100644000765000024 4666713617237165 17335 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'excluded' %] [%# include default stylesheet if none specified %] [% END %] [% IF config.key %] [% END %] [%# get dimensions of actual graph area (NOT SVG area) %] [% w = config.width %] [% h = config.height %] [%# set start/default coords of graph %] [% x = 0 %] [% y = 0 %] [% char_width = 8 %] [% half_char_height = 2.5 %] [%# reduce height and width of graph area for padding %] [% h = h - 20 %] [% w = w - 20 %] [% x = x + 10 %] [% y = y + 10 %] [% max_x_label_char = calc.max_x_label_length * char_width %] [% max_y_label_char = calc.max_y_label_length * char_width %] [% w = w - max_y_label_char %] [% x = x + max_y_label_char %] [% h = h - max_x_label_char %] [%# y = y + max_x_label_char %] [%# graph bg and clipping regions for lines/fill and clip extended to included data labels %] [% base_line = h + y %] [%# TODO %] [%# TODO %] [% FOREACH dataset = data.reverse %] [% xx = 0 %] [% yy = 0 %] [% FOREACH y_data = config.y_axis_order %] [% y_data %] [% yy = yy + 1 %] [% END %] [% FOREACH pair = dataset.pairs %] [% yy = 0 %] [% block_start_x = x + 1 + (xx * (config.block_width + config.gutter_width)) %] [% IF config.debug %] [% END %] [% textx = block_start_x %] [% texty = (base_line + 1) %] [% pair.x %] [% FOREACH y_data = config.y_axis_order %] [% yy = yy + 1 %] [% END %] [% xx = xx + 1 %] [% END %] [% END %] [% IF config.debug %] [% END %] BarLine.pm100755000765000024 7575513617237165 17335 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [% END %] [% w = config.width %] [% h = config.height %] [% x = 0 %] [% y = 0 %] [% char_width = 9 %] [% half_char_height = 2.5 %] [% title_height = 25 %] [% min_value = 99999999999 %] [% max_value = 0 %] [% secondary_max_value = 0 %] [% secondary_max_y_label_length = 0 %] [% secondary_max_y_label_size = 0 %] [% max_key_size = 0 %] [% max_x_label_size = 0 %] [% FOREACH field = config.fields %] [% IF max_x_label_size < field.length %] [% max_x_label_size = field.length %] [% END %] [% IF min_value > data.0.data.$field && data.0.data.$field != '' %] [% min_value = data.0.data.$field %] [% END %] [% IF max_value < data.0.data.$field && data.0.data.$field != '' %] [% max_value = data.0.data.$field %] [% END %] [% IF secondary_max_value < data.1.data.$field && data.1.data.$field != '' %] [% secondary_max_value = data.1.data.$field %] [% END %] [% IF secondary_max_y_label_size < data.1.data.$field.length %] [% secondary_max_y_label_size = data.1.data.$field.length %] [% END %] [% IF max_key_size < data.0.title.length %] [% max_key_size = data.0.title.length %] [% END %] [% END %] [% secondary_max_y_label_length = secondary_max_y_label_size * char_width %] [% IF config.show_x_labels %][% h = h - 20 %][% END %] [% max_x_label_length = 0 %] [% IF config.rotate_x_labels %] [% max_x_label_length = max_x_label_size * char_width %] [% h = h - max_x_label_length %] [% END %] [% stagger = 0 %] [% IF config.stagger_x_labels %] [% stagger = 17 %] [% h = h - stagger %] [% END %] [% IF config.show_x_title %][% h = h - title_height - stagger %][% END %] [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %] [% IF config.show_graph_title %][% h = h - title_height %][% y = y + title_height %][% END %] [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %] [% key_box_size = 12 %] [% IF config.key && config.key_position == 'right' %] [% IF config.show_secondary_y_title %] [% w = w - (max_key_size * (char_width - 1)) - (key_box_size * 3 ) %] [% ELSE %] [% w = w - (max_key_size * (char_width - 1)) - (key_box_size * 3 ) - title_height %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [% IF data.size < 4 %] [% h = h - ((data.size + 1) * (key_box_size + key_padding))%] [% ELSE %] [% h = h - (4 * (key_box_size + key_padding))%] [% END %] [% END %] [% IF config.min_scale_value || config.min_scale_value == '0' %] [% min_scale_value = config.min_scale_value %] [% ELSE %] [% min_scale_value = min_value %] [% END %] [% base_line = h + y %] [% IF (max_value - min_scale_value) == 0 %] [% top_pad = 10 %] [% ELSE %] [% top_pad = (max_value - min_scale_value) / 20 %] [% END %] [% IF (secondary_max_value - min_scale_value) == 0 %] [% secondary_top_pad = 10 %] [% ELSE %] [% secondary_top_pad = (secondary_max_value - min_scale_value) / 20 %] [% END %] [% scale_range = (max_value + top_pad) - min_scale_value %] [% secondary_scale_range = (secondary_max_value + secondary_top_pad) - min_scale_value %] [% IF config.scale_divisions %] [% scale_division = config.scale_divisions %] [% ELSE %] [% scale_division = scale_range / 10 FILTER format('%2.01f') %] [% END %] [% IF config.scale_divisions %] [% secondary_scale_division = config.scale_divisions %] [% ELSE %] [% secondary_scale_division = secondary_scale_range / 10 FILTER format('%2.01f') %] [% END %] [% IF config.scale_integers %] [% IF scale_division < 1 %] [% scale_division = 1 %] [% ELSIF scale_division.match('.') %] [% scale_division = scale_division FILTER format('%2.0f') %] [% END %] [% END %] [% dx = config.fields.size %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% secondary_title_padding = 0 %] [% IF config.show_secondary_y_labels %] [% w = w - secondary_max_y_label_length - title_height %] [% secondary_title_padding = secondary_title_padding + secondary_max_y_label_length + 10 %] [% END %] [% IF config.show_secondary_y_title %] [% w = w - title_height - char_width %] [% secondary_title_padding = secondary_title_padding + title_height %] [% END %] [% data_widths_x = w / dx %] [% dw = data_widths_x FILTER format('%2.02f') %] [% half_label_width = (config.fields.0.length / 2) * char_width %] [% space_b4_y_axis = 0 %] [% IF half_label_width > (dw / 2) %] [% space_b4_y_axis = half_label_width - (dw / 2) %] [% IF config.key && config.key_position == 'right' %] [% w = w - space_b4_y_axis %] [% ELSE %] [% w = w - (space_b4_y_axis * 2) %] [% END %] [% x = x + space_b4_y_axis %] [% END %] [% max_value_length = max_value.length %] [% max_value_length_px = max_value_length * char_width %] [% IF config.show_y_labels && space_b4_y_axis < max_value_length_px %] [% IF max_value_length < 2 %] [% w = w - (max_value_length * (char_width * 2)) %] [% x = x + (max_value_length * (char_width * 2)) %] [% ELSE %] [% w = w - max_value_length_px %] [% x = x + max_value_length_px %] [% END %] [% END %] [% IF config.show_y_title && space_b4_y_axis < max_value_length_px %] [% w = w - title_height %] [% x = x + title_height %] [% END %] [% dx = config.fields.size %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% data_widths_x = w / dx %] [%# dw = data_widths_x.match('(\d+[\.\d\d])').0 %] [% dw = data_widths_x %] [% IF config.bar_gap %] [% bar_gap = 10 %] [% IF dw < bar_gap %] [% bar_gap = dw / 2 %] [% END %] [% ELSE %] [% bar_gap = 0 %] [% END %] [% i = dw %] [% count = 0 %] [% stagger_count = 0 %] [% IF config.show_x_labels %] [% FOREACH field = config.fields %] [% field_txt = config.x_label_formatter(field) %] [% IF count == 0 %] [% field %] [% i = i - dw %] [% ELSE %] [% IF stagger_count == 2 %] [% field_txt %] [% stagger_count = 0 %] [% ELSE %] [% field_txt %] [% END %] [% END %] [% i = i + dw %] [% count = count + 1 %] [% stagger_count = stagger_count + 1 %] [% END %] [% END %] [% dy = scale_range / scale_division %] [% IF dy == 0 %] [% dy = 1 %] [% END %] [% y_marker_height = h / dy %] [% dy = y_marker_height.match('(\d+[\.\d\d])').0 %] [% count = 0 %] [% y_value = min_scale_value %] [% IF config.show_y_labels %] [% WHILE (dy * count) < h %] [% y_value_txt = config.y_label_formatter(y_value) %] [% IF count == 0 %] [% y_value_txt %] [% ELSE %] [% y_value_txt %] [% END %] [% y_value = y_value + scale_division %] [% count = count + 1 %] [% END %] [% END %] [% secondary_y_marker_height = h / dy %] [% s_count = 0 %] [% secondary_y_value = min_scale_value %] [% IF config.show_secondary_y_labels %] [% WHILE (dy * s_count) < h %] [% secondary_y_value %] [% secondary_y_value = secondary_y_value + secondary_scale_division %] [% s_count = s_count + 1 %] [% END %] [% END %] [% IF config.show_x_title %] [% IF !config.show_x_labels %] [% y_xtitle = 15 %] [% ELSE %] [% y_xtitle = 35 %] [% END %] [% config.x_title %] [% END %] [% IF config.show_y_title %] [% IF config.y_title_text_direction == 'tb' %] [% config.y_title %] [% ELSE %] [% config.y_title %] [% END %] [% END %] [% IF config.show_secondary_y_title %] [% IF config.secondary_y_title_text_direction == 'tb' %] [% config.secondary_y_title %] [% ELSE %] [% config.secondary_y_title %] [% END %] [% END %] [% bar_width = dw - bar_gap %] [% divider = dy / scale_division %] [% secondary_divider = dy / secondary_scale_division %] [% xcount = 0 %] [% line = data.size %] [% FOREACH field = config.fields %] [% dcount = 1 %] [% IF config.show_path_title %] [% IF config.show_title_fields %] [% data.0.data.$field %] - [% field %] [% ELSE %] [% data.0.data.$field %] - [% data.0.title %] [% END %] [% ELSE %] [% END %] [% IF config.show_data_values %] [% data.0.data.$field %] [% END %] [% dcount = dcount + 1 %] [% xcount = xcount + 1 %] [% END %] [% xcount = 0 %] [% FOREACH field = config.fields %] [% IF config.show_data_points %] [% END %] [% IF config.show_data_values %] [% data.1.data.$field %] [% END %] [% xcount = xcount + 1 %] [% END %] [% key_count = 1 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% FOREACH dataset = data %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [% y_key = base_line %] [% IF config.show_x_title %][% y_key = y_key + title_height %][% END %] [% IF config.rotate_x_labels && config.show_x_labels %] [% y_key = y_key + max_x_label_length %] [% ELSIF config.show_x_labels && stagger < 1 %] [% y_key = y_key + 20 %] [% END %] [% y_key_start = y_key %] [% x_key = x %] [% FOREACH dataset = data %] [% IF key_count == 4 || key_count == 7 || key_count == 10 %] [% x_key = x_key + 200 %] [% y_key = y_key - (key_box_size * 4) - 2 %] [% END %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %] synopsis_timeseries.pl100644000765000024 115313617237165 20601 0ustar00leostaff000000000000SVG-TT-Graph-1.04/scriptuse 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.pm100755000765000024 11536213617237165 20077 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [%# include default stylesheet if none specified %] [% END %] [% IF config.key %] [% END %] [%# get dimensions of actual graph area (NOT SVG area) %] [% w = config.width %] [% h = config.height %] [%# set start/default coords of graph %] [% x = 0 %] [% y = 0 %] [% char_width = 8 %] [% half_char_height = 2.5 %] [%# reduce height of graph area if there is labelling on x axis %] [% IF config.show_x_labels %][% h = h - 20 %][% END %] [%# reduce height if x labels are rotated %] [% x_label_allowance = 0 %] [% IF config.rotate_x_labels %] [% x_label_allowance = (calc.max_x_label_length * char_width) - 20 %] [% h = h - x_label_allowance %] [% END %] [%# stagger x labels if overlapping occurs %] [% stagger = 0 %] [% IF config.show_x_labels && config.stagger_x_labels %] [% stagger = 17 %] [% h = h - stagger %] [% END %] [% IF config.show_x_title %][% h = h - 25 - stagger %][% END %] [%# pad top of graph if y axis has data labels so labels do not get chopped off %] [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %] [%# reduce height if graph has title or subtitle %] [% IF config.show_graph_title %][% h = h - 25 %][% y = y + 25 %][% END %] [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %] [%# reduce graph dimensions if there is a KEY %] [% key_box_size = 12 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% w = w - (calc.max_key_size * (char_width - 1)) - (key_box_size * 3 ) %] [% ELSIF config.key && config.key_position == 'bottom' %] [% IF data.size < 4 %] [% h = h - ((data.size + 1) * (key_box_size + key_padding))%] [% ELSE %] [% h = h - (4 * (key_box_size + key_padding))%] [% END %] [% END %] [%# base line %] [% base_line = h + y %] [%# find the string length of max value %] [% max_value_length = calc.max_scale_value.length %] [%# label width in pixels %] [% max_value_length_px = max_value_length * char_width %] [%# If the y labels are shown but the size of the x labels are small, pad for y labels %] [%# reduce width of graph area if there is large labelling on x axis %] [% space_b4_y_axis = (date.format(calc.min_timescale_value,config.x_label_format).length / 2) * char_width %] [% IF config.show_x_labels %] [% IF config.key && config.key_position == 'right' %] [% w = w - space_b4_y_axis %] [% ELSE %] [% w = w - (space_b4_y_axis * 2) %] [% END %] [% x = x + space_b4_y_axis %] [% ELSIF config.show_data_values %] [% w = w - (max_value_length_px * 2) %] [% x = x + max_value_length_px %] [% END %] [% IF config.show_y_labels && space_b4_y_axis < max_value_length_px %] [% IF max_value_length < 2 %] [% w = w - (max_value_length * (char_width * 2)) %] [% x = x + (max_value_length * (char_width * 2)) %] [% ELSE %] [% w = w - max_value_length_px %] [% x = x + max_value_length_px %] [% END %] [% ELSIF config.show_y_labels && !config.show_x_labels %] [% w = w - max_value_length_px %] [% x = x + max_value_length_px %] [% END %] [% IF config.show_y_title %] [% w = w - 25 %] [% x = x + 25 %] [% END %] [%# Missing data spans %] [% max_time_span = 0 %] [% IF (matches = config.max_time_span.match('(\d+) ?(\w+)?')) %] [% max_time_span = matches.0 %] [% IF (matches.1) %] [% max_time_span_units = matches.1 %] [% ELSE %] [% max_time_span_units = 'days' %] [% END %] [% END %] [%# graph bg and clipping regions for lines/fill and clip extended to included data labels %] [% dx = calc.timescale_range %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% dw = w / dx %] [% IF config.show_x_labels %] [% x_value_txt = config.x_label_formatter(date.format(calc.min_timescale_value,config.x_label_format)) %] [% x_value_txt %] [% last_label = date.format(calc.min_timescale_value,config.x_label_format) %] [% IF config.timescale_divisions %] [% IF (matches = config.timescale_divisions.match('(\d+) ?(\w+)?')) %] [% timescale_division = matches.0 %] [% IF (matches.1) %] [% timescale_division_units = matches.1 %] [% ELSE %] [% timescale_division_units = 'days' %] [% END %] [% x_value = config.dateadd(calc.min_timescale_value,timescale_division,timescale_division_units) %] [% count = 0 %] [% WHILE ((x_value > calc.min_timescale_value) && ((x_value < calc.max_timescale_value))) %] [% x_value_txt = config.x_label_formatter(date.format(x_value,config.x_label_format)) %] [% xpos = (dw * (x_value - calc.min_timescale_value)) + x %] [% IF (config.stagger_x_labels && ((count % 2) == 0)) %] [% x_value_txt %] [% ELSE %] [% x_value_txt %] [% END %] [% last_label = date.format(x_value,config.x_label_format) %] [% x_value = config.dateadd(x_value,timescale_division,timescale_division_units) %] [% count = count + 1 %] [% LAST IF (count >= 999) %] [% END %] [% END %] [% END %] [% IF date.format(calc.max_timescale_value,config.x_label_format) != last_label %] [% x_value_txt = config.x_label_formatter(date.format(calc.max_timescale_value,config.x_label_format)) %] [% IF (config.stagger_x_labels && ((count % 2) == 0)) %] [% x_value_txt %] [% ELSE %] [% x_value_txt %] [% END %] [% END %] [% END %] [%# how much padding between largest bar and top of graph %] [% top_pad = h / 40 %] [% dy = calc.scale_range %] [% IF dy == 0 %] [% dy = 1 %] [% END %] [% dh = (h - top_pad) / dy %] [% count = 0 %] [% last_label = '' %] [% IF (calc.min_scale_value > calc.max_scale_value) %] [% y_value = calc.max_scale_value %] [% reversed = 1 %] [% ELSE %] [% y_value = calc.min_scale_value %] [% reversed = 0 %] [% END %] [% IF config.show_y_labels %] [% WHILE ((y_value == calc.min_scale_value) || (y_value == calc.max_scale_value) || ((y_value > calc.min_scale_value) && (y_value < calc.max_scale_value)) || ((y_value > calc.max_scale_value) && (y_value < calc.min_scale_value) && reversed )) %] [%- next_label = y_value FILTER format(calc.y_label_format) -%] [%- next_label = config.y_label_formatter(next_label) -%] [%- IF ((count == 0) && (reversed == 0)) -%] [%# no stroke for first line unless reversed %] [% next_label %] [%- ELSE -%] [% IF next_label != last_label %] [% next_label %] [% END %] [%- END -%] [%- y_value = y_value + calc.scale_division -%] [%- last_label = next_label -%] [%- count = count + 1 -%] [%- LAST IF (count >= 999) -%] [% END %] [% END %] [% IF config.show_x_title %] [% IF !config.show_x_labels %] [% y_xtitle = 15 %] [% ELSE %] [% y_xtitle = 35 %] [% END %] [% config.x_title %] [% END %] [% IF config.show_y_title %] [% config.y_title %] [% END %] [% line = data.size %] [% FOREACH dataset = data.reverse %] [% IF config.area_fill %] [%# create alternate fill first (so line can overwrite if necessary) %] [% xcount = 0 %] [% FOREACH pair = dataset.pairs %] [%- IF ((pair.0 >= calc.min_timescale_value) && (pair.0 <= calc.max_timescale_value)) -%] [%- IF xcount == 0 -%][% lasttime = pair.0 %] [% END %] [% END %] [% xcount = 0 %] [% FOREACH pair = dataset.pairs %] [% IF ((pair.0 >= calc.min_timescale_value) && (pair.0 <= calc.max_timescale_value)) %] [%- IF xcount == 0 -%][%- lasttime = pair.0 -%] [% END %] [% IF config.show_data_points || config.show_data_values %] [% FOREACH pair = dataset.pairs %] [% IF ((pair.0 >= calc.min_timescale_value) && (pair.0 <= calc.max_timescale_value)) %] [% IF config.show_data_points %] [% END %] [% IF config.show_data_values %] [%# datavalue shown %] [% IF (pair.2.defined) && (pair.2 != '') %][% point_label = pair.2 %][% ELSE %][% point_label = pair.1 FILTER format(calc.data_value_format) %][% END %] [% point_label %] [% END %] [% END %] [% END %] [% END %] [% line = line - 1 %] [% END %] [% key_count = 1 %] [% IF config.key && config.key_position == 'right' %] [% FOREACH dataset = data %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [%# calc y position of start of key %] [% y_key = base_line %] [%# consider x title %] [% IF config.show_x_title %][% y_key = base_line + 25 %][% END %] [%# consider x label rotation and stagger %] [% IF config.rotate_x_labels && config.show_x_labels %] [% y_key = y_key + x_label_allowance %] [% ELSIF config.show_x_labels && stagger < 1 %] [% y_key = y_key + 20 %] [% END %] [% y_key_start = y_key %] [% x_key = x %] [% FOREACH dataset = data %] [% IF key_count == 4 || key_count == 7 || key_count == 10 %] [%# wrap key every 3 entries %] [% x_key = x_key + 200 %] [% y_key = y_key - (key_box_size * 4) - 2 %] [% END %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %] synopsis_bar_horizontal.pl100644000765000024 67113617237165 21431 0ustar00leostaff000000000000SVG-TT-Graph-1.04/scriptuse 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.pm100755000765000024 6042713617237165 20565 0ustar00leostaff000000000000SVG-TT-Graph-1.04/lib/SVG/TT/Graphpackage 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 %] [% IF stylesheet == 'inline' %] [% style_inline %] [% ELSIF stylesheet == 'excluded' %] [% END %] [% w = config.width %] [% h = config.height %] [% x = 0 %] [% y = 0 %] [% char_width = 7 %] [% IF config.show_y_title %][% w = w - 20 %][% x = x + 20 %][% END %] [% IF config.show_y_labels %] [% max_ylabel_length = 0 %] [% FOREACH y_label = config.fields %] [% IF max_ylabel_length < y_label.length %] [% max_ylabel_length = y_label.length %] [% END %] [% END %] [% space_b4_axis = char_width * max_ylabel_length %] [% w = w - space_b4_axis %] [% x = x + space_b4_axis %] [% END %] [% IF max_ylabel_length == 1 %] [% w = w - 5 %] [% x = x + 5 %] [% END %] [% IF config.show_x_labels %] [% w = w - 20 %] [% IF !config.show_y_labels && !config.show_y_title %] [% w = w - 10 %] [% x = x + 10 %] [% END %] [% IF config.min_scale_value.length > 1 %] [% padding_for_labels = char_width * config.min_scale_value.length %] [% w = w - (padding_for_labels / 2) %] [% x = x + (padding_for_labels / 2) %] [% END %] [% END %] [% IF config.show_x_labels %][% h = h - 20 %][% END %] [% stagger = 0 %] [% IF config.stagger_x_labels %] [% stagger = 17 %] [% h = h - stagger %] [% END %] [% IF config.show_x_title %][% h = h - 25 - stagger %][% END %] [% IF config.show_y_labels %][% h = h - 10 %][% y = y + 10 %][% END %] [% IF config.show_graph_title %][% h = h - 25 %][% y = y + 25 %][% END %] [% IF config.show_graph_subtitle %][% h = h - 10 %][% y = y + 10 %][% END %] [% max_key_size = 0 %] [% FOREACH y_label = config.fields %] [% IF max_key_size < data.0.title.length %] [% max_key_size = data.0.title.length %] [% END %] [% END %] [% key_box_size = 12 %] [% IF config.key && config.key_position == 'right' %] [% w = w - (max_key_size * (char_width - 1)) - (key_box_size * 3 ) %] [% ELSIF config.key && config.key_position == 'bottom' %] [% IF data.size < 4 %] [% h = h - ((data.size + 1) * (key_box_size + key_padding))%] [% ELSE %] [% h = h - (4 * (key_box_size + key_padding))%] [% END %] [% END %] [% min_value = 99999999999 %] [% max_value = 0 %] [% FOREACH field = config.fields %] [% FOREACH dataset = data %] [% IF min_value > dataset.data.$field && dataset.data.$field != '' %] [% min_value = dataset.data.$field %] [% END %] [% IF max_value < dataset.data.$field && dataset.data.$field != '' %] [% max_value = dataset.data.$field %] [% END %] [% END %] [% END %] [% IF config.min_scale_value || config.min_scale_value == '0' %] [% min_scale_value = config.min_scale_value %] [% ELSE %] [% min_scale_value = min_value %] [% END %] [% base_line = h + y %] [% IF (max_value - min_scale_value) == 0 %] [% top_pad = 10 %] [% ELSE %] [% top_pad = (max_value - min_scale_value) / 20 %] [% END %] [% scale_range = (max_value + top_pad) - min_scale_value %] [% IF config.scale_divisions %] [% scale_division = config.scale_divisions %] [% ELSE %] [% scale_division = scale_range / 10 %] [% END %] [% IF config.scale_integers %] [% IF scale_division < 1 %] [% scale_division = 1 %] [% ELSIF scale_division.match('.') %] [% scale_division = scale_division FILTER format('%2.0f') %] [% END %] [% END %] [% dy = config.fields.size %] [% IF dy == 0 %] [% dy = 1 %] [% END %] [% data_widths_y = h / dy %] [% dh = data_widths_y.match('(\d+[\.\d\d])').0 %] [% i = dh %] [% count = 0 %] [% IF config.show_y_labels %] [% FOREACH field = config.fields %] [% field_txt = config.y_label_formatter(field) %] [% IF count == 0 %] [% field_txt %] [% i = i - dh %] [% ELSE %] [% field_txt %] [% END %] [% i = i + dh %] [% count = count + 1 %] [% END %] [% END %] [% dx = scale_range / scale_division %] [% IF dx == 0 %] [% dx = 1 %] [% END %] [% scale_division_height = w / dx %] [% dx = scale_division_height.match('(\d+[\.\d\d])').0 %] [% count = 0 %] [% y_value = min_scale_value %] [% stagger_count = 0 %] [% IF config.show_x_labels %] [% WHILE (dx * count) < w %] [% IF count == 0 %] [% y_value_txt = y_value FILTER format('%2.0f') %] [% y_value_txt = config.y_label_formatter(y_value_txt) %] [% y_value_txt %] [% ELSE %] [% IF stagger_count == 2 %] [% y_value_txt = y_value FILTER format('%2.01f') %] [% y_value_txt = config.y_label_formatter(y_value_txt) %] [% y_value_txt %] [% stagger_count = 0 %] [% ELSE %] [% y_value_txt = y_value FILTER format('%2.01f') %] [% y_value_txt = config.y_label_formatter(y_value_txt) %] [% y_value_txt %] [% END %] [% END %] [% y_value = y_value + scale_division %] [% count = count + 1 %] [% stagger_count = stagger_count + 1 %] [% END %] [% END %] [% IF config.show_x_title %] [% IF !config.show_x_labels %] [% y_xtitle = 15 %] [% ELSE %] [% y_xtitle = 35 %] [% END %] [% config.x_title %] [% END %] [% IF config.show_y_title %] [% IF config.y_title_text_direction == 'tb' %] [% config.y_title %] [% ELSE %] [% config.y_title %] [% END %] [% END %] [% IF config.bar_gap %] [% bar_gap = 10 %] [% ELSE %] [% bar_gap = 0 %] [% END %] [% bar_width = dh - bar_gap %] [% divider = dx / scale_division %] [% xcount = 0 %] [% FOREACH field = config.fields %] [% dcount = 1 %] [% FOREACH dataset = data %] [% IF config.show_path_title %] [% IF config.show_title_fields %] [% dataset.data.$field %] - [% field %] [% ELSE %] [% dataset.data.$field %] - [% dataset.title %] [% END %] [% ELSE %] [% END %] [% IF config.show_data_values %] [% dataset.data.$field %] [% END %] [% dcount = dcount + 1 %] [% END %] [% xcount = xcount + 1 %] [% END %] [% key_box_size = 12 %] [% key_count = 1 %] [% key_padding = 5 %] [% IF config.key && config.key_position == 'right' %] [% FOREACH dataset = data %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% ELSIF config.key && config.key_position == 'bottom' %] [% y_key = base_line %] [% IF config.show_x_labels %][% y_key = base_line + 20 %][% END %] [% IF config.show_x_title %][% y_key = base_line + 25 %][% END %] [% y_key_start = y_key %] [% x_key = x %] [% FOREACH dataset = data %] [% IF key_count == 4 || key_count == 7 || key_count == 10 %] [% x_key = x_key + 200 %] [% y_key = y_key - (key_box_size * 4) - 2 %] [% END %] [% dataset.title %] [% key_count = key_count + 1 %] [% END %] [% END %] [% IF config.show_graph_title %] [% config.graph_title %] [% END %] [% IF config.show_graph_subtitle %] [% IF config.show_graph_title %] [% y_subtitle = 30 %] [% ELSE %] [% y_subtitle = 15 %] [% END %] [% config.graph_subtitle %] [% END %]