Subject: | [PATCH] Independent number formats for dual axes graphs (y1_number_format, y2_number_format) |
This patch implements independent number formats for graphs with, for example, two y axes. It applies to GDGraph-1.43.
Small code modifications were made to Graph/axestype.pm and Graph.pm, documentation was added to Graph.pm and a new sample19 was created (based on sample14) to test this. This meant minor changes to MANIFEST and samples/Makefile as well.
Tested under perl 5.6.1.
diff -rNu --exclude debian GDGraph-1.43.orig/Graph/axestype.pm GDGraph-1.43/Graph/axestype.pm
--- GDGraph-1.43.orig/Graph/axestype.pm Tue Jul 1 15:04:10 2003
+++ GDGraph-1.43/Graph/axestype.pm Tue Feb 10 16:52:33 2004
@@ -124,6 +124,8 @@
# Format of the numbers on the x and y axis
y_number_format => undef,
+ y1_number_format => undef, # CONTRIB Andrew OBrien
+ y2_number_format => undef, # CONTRIB Andrew OBrien
x_number_format => undef, # CONTRIB Scott Prahl
# and some attributes without default values
@@ -728,11 +730,11 @@
$self->{y_values}[$axis][$t] = $label;
- if (defined $self->{y_number_format})
+ if (my ($fmt) = grep defined, map($self->{"y${_}_number_format"},'',$axis))
{
- $label = ref $self->{y_number_format} eq 'CODE' ?
- &{$self->{y_number_format}}($label) :
- sprintf($self->{y_number_format}, $label);
+ $label = ref $fmt eq 'CODE' ?
+ $fmt->($label) :
+ sprintf($fmt, $label);
}
$self->{gdta_y_axis}->set_text($label);
diff -rNu --exclude debian GDGraph-1.43.orig/Graph.pm GDGraph-1.43/Graph.pm
--- GDGraph-1.43.orig/Graph.pm Tue Jul 1 15:02:10 2003
+++ GDGraph-1.43/Graph.pm Tue Feb 10 16:52:33 2004
@@ -967,6 +967,17 @@
Default: undef.
+=item y1_number_format, y2_number_format
+
+As for I<y_number_format> these can be either a string, or a reference
+to a subroutine. These are used as fallback formats for graphs with
+two y-axes scales so that independent formats can be used.
+
+For compatibility purposes if I<y_number_format> is specified at all
+it will I<always> take precedence on both axes.
+
+Default: undef for both.
+
=item x_label_skip, y_label_skip
Print every I<x_label_skip>th number under the tick on the x axis, and
diff -rNu --exclude debian GDGraph-1.43.orig/MANIFEST GDGraph-1.43/MANIFEST
--- GDGraph-1.43.orig/MANIFEST Thu Jun 19 16:31:19 2003
+++ GDGraph-1.43/MANIFEST Tue Feb 10 17:08:51 2004
@@ -34,6 +34,7 @@
samples/sample16.pl
samples/sample17.pl
samples/sample18.pl
+samples/sample19.pl
samples/sample21.pl
samples/sample22.pl
samples/sample23.pl
diff -rNu --exclude debian GDGraph-1.43.orig/samples/Makefile GDGraph-1.43/samples/Makefile
--- GDGraph-1.43.orig/samples/Makefile Fri Jun 20 13:05:37 2003
+++ GDGraph-1.43/samples/Makefile Tue Feb 10 16:52:33 2004
@@ -24,9 +24,9 @@
SAMPLES := \
sample11 sample12 sample13 sample14 sample15 \
- sample16 sample17 sample18 \
+ sample16 sample17 sample18 sample19 \
sample11-h sample12-h sample13-h sample14-h sample15-h \
- sample16-h sample17-h sample18-h \
+ sample16-h sample17-h sample18-h sample19-h \
sample21 sample22 sample23 \
sample31 \
sample41 sample42\
diff -rNu --exclude debian GDGraph-1.43.orig/samples/sample19.pl GDGraph-1.43/samples/sample19.pl
--- GDGraph-1.43.orig/samples/sample19.pl Thu Jan 1 10:00:00 1970
+++ GDGraph-1.43/samples/sample19.pl Tue Feb 10 16:52:33 2004
@@ -0,0 +1,97 @@
+use strict;
+use GD::Graph::bars;
+use GD::Graph::hbars;
+use GD::Graph::Data;
+require 'save.pl';
+
+$GD::Graph::Error::Debug = 5;
+
+my $data = GD::Graph::Data->new(
+[
+ ["2004/2/1","2004/2/2","2004/2/3","2004/2/4","2004/2/5","2004/2/6","2004/2/7", "2004/2/8", "2004/2/9"],
+ [ 50000, 120000, 240000, 330000, 190000, 80000, 60000, 150000, 210000],
+ [1033101,2200100,5300300,6400400,3192192,1600600, 900900,3333333,4444444],
+]
+) or die GD::Graph::Data->error;
+
+my $values = $data->copy();
+$values->set_y(1, 7, undef) or warn $data->error;
+$values->set_y(2, 7, undef) or warn $data->error;
+
+my @names = qw/sample19 sample19-h/;
+
+for my $my_graph (GD::Graph::bars->new(600,400),
+ GD::Graph::hbars->new(600,400))
+{
+ my $name = shift @names;
+ print STDERR "Processing $name\n";
+
+ $my_graph->set(
+ x_label => 'Date',
+ y1_label => 'Hits',
+ y2_label => 'Megabytes',
+ title => 'Using two axes with different formats',
+ #y1_max_value => 40,
+ #y2_max_value => 8,
+ y_tick_number => 8,
+ y_label_skip => 2,
+ #y_number_format => '%d', ### <-- this will override y{1,2}_number_format
+ y1_number_format => sub { # from perlfaq5
+ local $_ = shift;
+ 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
+ $_;
+ },
+ y2_number_format => sub { my $v = shift; sprintf("%.2f",$v/1024) },
+ long_ticks => 1,
+ two_axes => 1,
+ legend_placement => 'RT',
+ x_label_position => 1/2,
+
+ bgclr => 'white',
+ fgclr => 'white',
+ boxclr => 'dblue',
+ accentclr => 'dblue',
+ valuesclr => '#ffff77',
+ dclrs => [qw(lgreen lred)],
+
+ bar_spacing => 1,
+
+ logo => 'logo.' . GD::Graph->export_format,
+ logo_position => 'BR',
+
+ transparent => 0,
+
+ l_margin => 10,
+ b_margin => 10,
+ r_margin => 10,
+ t_margin => 10,
+
+ show_values => 1,
+ values_format => "%4.1f",
+
+ ) or warn $my_graph->error;
+
+ if ($name =~ /-h$/)
+ {
+ $my_graph->set(x_labels_vertical => 0, values_vertical => 0);
+ $my_graph->set_legend('bottom axis', 'top axis');
+ }
+ else
+ {
+ $my_graph->set(x_labels_vertical => 1, values_vertical => 1);
+ $my_graph->set_legend('left axis', 'right axis');
+ }
+
+ my $font_spec = "../Dustismo_Sans";
+
+ $my_graph->set_y_label_font($font_spec, 12);
+ $my_graph->set_x_label_font($font_spec, 12);
+ $my_graph->set_y_axis_font($font_spec, 10);
+ $my_graph->set_x_axis_font($font_spec, 10);
+ $my_graph->set_title_font($font_spec, 18);
+ $my_graph->set_legend_font($font_spec, 8);
+ $my_graph->set_values_font($font_spec, 8);
+
+ $my_graph->plot($data) or die $my_graph->error;
+ save_chart($my_graph, $name);
+}