Subject: | PATCH - reverse legend order in cumulative graphs |
Hi,
I recently used GD::Graph to create a cumulative (stacked) bar graph and
got the feedback that the legend appeared in the opposite order of the
bars, which readers find visually disturbing. This makes sense to me
that it is better to see the legend fields in the same order they appear
in the stack of bars.
Attached is a patch that changes this behavior for if the cumulate
attribute is true. It may make sense to just have this as a parameter
instead.
I have also attached a simple script that generates a graph that
illustrates my point.
Let me know what you think about this feature, and thanks in advance for
your time and consideration,
Don
--- System info, for completeness -----
This seems to be present in all versions of GD::Graph that support
cumulate. The patch is against 1.44.
I tested this on perl, v5.8.8 built for i486-linux-gnu-thread-multi
on an x86 Ubuntu (edgy) system running the 2.6.17-11-generic kernel.
Subject: | graph-test.pl |
#!/usr/bin/perl
use strict;
use warnings;
use lib '.';
use GD;
use GD::Graph::bars;
# Make the graph
##################
my $graph = GD::Graph::bars->new(400,300);
# Reorganize the data
my @data = (
["A", "B"],
[2, 4],
[4, 2]
);
# Set the options
$graph->set(
x_label => 'Benchmark',
y_label => 'Time(s)',
y_max_value => 6,
bar_spacing => 5,
box_axis => 0,
y_long_ticks => 1,
x_ticks => 0,
# No bargroup spacing in dapper, boo!
#bargroup_spacing => 5,
legend_placement => 'RT',
x_labels_vertical => 1,
cumulate => 1,
legendclr => "black",
textclr => "black",
fgclr => "black",
x_label_position => .5,
) or die $graph->error;
$graph->set_text_clr("black");
$graph->set_legend("spins", "retries");
my $fontfile = '/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf';
my $fontsize = 14;
$graph->set_legend_font($fontfile, $fontsize);
$graph->set_title_font($fontfile, $fontsize);
$graph->set_x_label_font($fontfile, $fontsize);
$graph->set_x_axis_font($fontfile, $fontsize);
$graph->set_y_label_font($fontfile, $fontsize);
$graph->set_y_axis_font($fontfile, $fontsize);
# Plot that bad boy
my $gd = $graph->plot(\@data) or die $graph->error;
open(IMG, '>test.png') or die $!;
binmode IMG;
print IMG $gd->png;
close IMG;
Subject: | legend-reverse.patch |
diff -ur GDGraph-1.44/Graph/axestype.pm GD/Graph/axestype.pm
--- GDGraph-1.44/Graph/axestype.pm 2007-04-25 22:16:09.000000000 -0500
+++ GD/Graph/axestype.pm 2007-05-16 11:30:13.047800000 -0500
@@ -2057,7 +2057,11 @@
my $row = 1;
my $x = $xl; # start position of current element
- foreach my $legend (@{$self->{legend}})
+ # Reverse the legend order for cumulative graphs
+ my @legend_list = $self->{cumulate} ?
+ reverse @{$self->{legend}} : @{$self->{legend}};
+
+ foreach my $legend (@legend_list)
{
$i++;
last if $i > $self->{_data}->num_sets;
@@ -2066,7 +2070,9 @@
next unless defined($legend) && $legend ne "";
- $self->draw_legend_marker($i, $xe, $y);
+ $self->{cumulate}
+ ? $self->draw_legend_marker($self->{_data}->num_sets + 1 - $i, $xe, $y)
+ : $self->draw_legend_marker($i, $xe, $y);
$xe += $self->{legend_marker_width} + $self->{legend_spacing};
my $ys = int($y + $self->{lg_el_height}/2 - $self->{lgfh}/2);