poss has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script using CGI to create a stacked Bar graph. I am plotting Build Times and Kitting times per platform. my data plot looks like.

@data2 = (['Windows', 'Linux', 'Solaris'], [150,250,350], [100,200,300]);

However if I want to reverse the way it is displayed, the plotting works ok, but the values do not follow the bar. So reversing the above data points to
@data2 = (['Windows', 'Linux', 'Solaris'], [100,200,300], [150,250,350]);

The values remain the same. I have cumaulate => 1 and show_values=> 1. How do I get the values to follow the bars? Here is my graphing points.
my $graph = new GD::Graph::bars(900,600); $graph->set( x_label => $x_Label, x_label_position => 1/4, x_label_vertical => 1, y_label => 'Minutes', y_max_value => 300, y_tick_number => 10, title => $Graph_Title, bar_width => 35, cumulate => 1, dclrs => ['lblue', 'cyan'], bgclr => 'white', fgclr => 'lred', values_vertical => 0, valuesclr => 'black', accentclr => 'red', shadowclr => '#F7E7CE', shadow_depth => 0, legendclr => 'blue', show_values => 1);


I hope I provided enough information. This is driving me nuts! Thanks

Replies are listed 'Best First'.
Re: perl bar graph
by roboticus (Chancellor) on Feb 27, 2015 at 17:16 UTC

    poss:

    I tried your example (including the lines you left out) and see the difference between the two graphs. However, I can't figure out what you mean when you ask "How do I get the values to follow the bars?" Care to elaborate?

    In the future, you'll get better answers if you make your code function so people can try it without having to finish and/or debug it. (The only reason I bothered is that I need to generate some bar charts for my current project at work.)

    A working example, if anyone is interested:

    #!/usr/bin/env perl use strict; use warnings; use GD::Graph; use GD::Graph::bars; my @data = ( [ 'Windows', 'Linux', 'Solaris' ], # Swap the next two lines to switch versions of the graph [ 150, 250, 350 ], [ 100, 200, 300 ], ); my $x_Label = "XLabel"; my $Graph_Title = "Graph Title"; my $graph = new GD::Graph::bars(900,600); $graph->set( x_label => $x_Label, x_label_position => 1/4, x_label_vertical => 1, y_label => 'Minutes', y_max_value => 300, y_tick_number => 10, title => $Graph_Title, bar_width => 35, cumulate => 1, dclrs => [ 'blue','cyan' ], bgclr => 'white', fgclr => 'lred', values_vertical => 0, valuesclr => 'black', accentclr => 'red', shadowclr => '#F7E7CE', shadow_depth => 0, legendclr => 'blue', show_values => 1 ); my $gd = $graph->plot(\@data) or die $graph->error; open my $IMG, '>', 'file2.png' or die $!; binmode $IMG; print $IMG $gd->png;

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks robitcus for your prompt reply. I appreciate. I will use your example, since you already have the example working. Your comment says.
      my @data = ( [ 'Windows', 'Linux', 'Solaris' ], # Swap the next two lines to switch versions of the graph [ 150, 250, 350 ], [ 100, 200, 300 ], );
      This show exactly what I am trying to resolve. By switching
      [ 150, 250, 350 ], [ 100, 200, 300 ],
      to
      [ 100, 200, 300 ], [ 150, 250, 350 ],

      Does indeed swap the stacked bars, however the values do not follow the bars. So, stacked bar 1 has values
      100 for bottom bar 150 for top bar. If I swap them, the vales 100,150 remain and do not follow the bars it should be referencing.
      I may not be understand perl and/or Graphs as I am very new to this. Thanks for any pointers you can provide.
      Poss

        poss:

        Sorry for the delay in replying, but I had to wait until I got back to my work computer to review the code & figures.

        Anyway, when you say "the values do not follow the bars", do you mean that the numbers aren't shown immediately above each bar? When I run it, the values seem to follow nicely. If your numbers are appearing elsewhere, or aren't changing positions with the tops of the bars, then I'd have to guess that there are some module/library version differences going on.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: perl bar graph
by poj (Abbot) on Feb 27, 2015 at 17:54 UTC

    Are you creating both graphs from the same $graph object ?. This code creates a new one for each plot

    #!perl use strict; use GD::Graph::bars; my @data1 = (['Windows', 'Linux', 'Solaris'], [150,250,350],[100,200,3 +00]); my @data2 = (['Windows', 'Linux', 'Solaris'], [100,200,300],[150,250,3 +50]); create_graph('file1.gif',\@data1); create_graph('file2.gif',\@data2); sub create_graph { my ($file,$data) = @_; my $graph = new GD::Graph::bars(900,600); $graph->set( x_label => 'x_Label', x_label_position => 1/4, x_label_vertical => 1, y_label => 'Minutes', y_max_value => 700, y_tick_number => 14, title => 'Graph_Title '.$file, bar_width => 35, cumulate => 1, dclrs => ['yellow', 'cyan'], bgclr => 'white', fgclr => 'lred', values_vertical => 0, valuesclr => 'black', accentclr => 'red', shadowclr => '#F7E7CE', shadow_depth => 0, legendclr => 'lblue', show_values => 1); # graph my $gd = $graph->plot($data) or die $graph->error; open IMG, '>',$file or die $!; binmode IMG; print IMG $gd->gif; close IMG; }
    poj