in reply to Re^3: GD::Graph creating a clustered bar chart while some dataset are cumulative
in thread GD::Graph creating a clustered bar chart while some dataset are cumulative

But i want multi-stacked graph, like you have said before i.e in my 1st set of x-axis it should contain both the bars, with different colors. i.e ABCD all with different colors in 1 set as your example shows ABCD all in 4th set.
25| | 20| B | B 15| B | B 10| A D | A D 5| A D | A C 0+------------------------------------------------- 1 2 3 4 5 6 7 8 9 10 11 12
  • Comment on Re^4: GD::Graph creating a clustered bar chart while some dataset are cumulative
  • Download Code

Replies are listed 'Best First'.
Re^5: GD::Graph creating a clustered bar chart while some dataset are cumulative
by BrowserUk (Patriarch) on Aug 28, 2013 at 17:52 UTC

    Try this (produces this):

    use Data::Dump qw[ pp ]; use GD::Graph::bars; use GD::Graph::hbars; my @data = ( [ qw[Jan _ Feb _ Mar _ Apr _ May _ Jun _ Jul _ Aug _ Sep _ Oct _ N +ov _ Dec _ ] ], [ map rand( 25 ), 1 .. 24 ], [ map rand( 25 ), 1 .. 24 ], ); for my $my_graph (GD::Graph::hbars->new( 800, 600 ) ) { $my_graph->set( title => 'Stacked Bars (incremental)', x_label => 'X Label', y_label => 'Y label', cumulate => 1, overwrite => 1, borderclrs => $my_graph->{dclrs}, bar_spacing => 20, bar_width => 10, transparent => 0, ); $my_graph->set_legend( qw(offset increment more)); $my_graph->plot(\@data); save_chart($my_graph, "test.png"); } system 1, 'test.png'; sub save_chart { my $chart = shift or die "Need a chart!"; my $name = shift or die "Need a name!"; local(*OUT); open(OUT, ">$name") or die "Cannot open $name.$ext for write: $!"; binmode OUT; print OUT $chart->gd->png; close OUT; }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: GD::Graph creating a clustered bar chart while some dataset are cumulative
by poj (Abbot) on Aug 28, 2013 at 20:33 UTC

    If you want different colours then you could create a 4 stack bar but use only 2 of the stack, the other 2 set to zero and alternate the 2 used. For example ;

    #!perl use GD::Graph::bars; use GD::Graph::hbars; my @data = ( ['Mar1','Mar2','Apr1','Apr2','May1','May2'], [1,0,2,0,4,0], [2,0,1,0,5,0], [0,3,0,3,0,6], [0,4,0,4,0,7], ); my @names = qw/sample15 sample15-h/; for my $my_graph (GD::Graph::bars->new, GD::Graph::hbars->new) { my $name = shift @names; print STDERR "Processing $name\n"; $my_graph->set( x_label => 'X Label', y_label => 'Y label', title => 'Stacked Bars (incremental)', cumulate => 1, overwrite => 1, borderclrs => $my_graph->{dclrs}, bar_spacing => 5, transparent => 0, ); $my_graph->set_legend( qw(A B C D)); my $gd = $my_graph->plot(\@data); open(IMG, '>',$name.'.gif') or die $!; binmode IMG; print IMG $gd->gif; close IMG; }
    poj
Re^5: GD::Graph creating a clustered bar chart while some dataset are cumulative
by huzefa52 (Novice) on Aug 29, 2013 at 09:01 UTC
    Thanks a lot, your solution works for me. Just i am making the graph more attractive now, I am putting values on the top of each bar. But for some values when your run below code you will see like 30, 50 and 90 these numbers are not visible properly. And also i don't want 0 to show there. Can you please help me in this
    #!/usr/bin/perl use strict; use GD; use GD::Graph::mixed; use GD::Graph::colour; use GD::Graph::bars; use GD::Graph::hbars; use GD::Graph::Data; use GD::Text; use GD::Graph::pie; #use GD::Font; my @data = ( ['Mon','-','Tues','-','Wed','-'], [10,0,40,0,80,0], [30,0,50,0,90,0], [0,100,0,60,0,30], [0,60,0,70,0,50], ); my @names = qw/sample15 sample15-h/; for my $my_graph (GD::Graph::bars->new, GD::Graph::hbars->new) { my $name = shift @names; print STDERR "Processing $name\n"; $my_graph->set( x_label => 'Days', y_label => 'CPU Utilization', y_min_value => 0, y_max_value => 200, #title => 'Stacked Bars (incremental)', cumulate => 1, #overwrite => 1, x_label_position => 1/3, #borderclrs => $my_graph->{dclrs}, dclrs => [ qw( green dgreen yellow dyellow ) ], transparent => 0, show_values => 1, values_space => 1, bar_spacing => 2, bargroup_spacing => 4, accent_treshold => 200, ); $my_graph->set_values_font('/usr/lib/perl5/5.8.8/fonts/nimbus_roman_ +i.pfb', 8); $my_graph->set_legend( qw(E-Avg E-Max T-Avg T-Max)); my $gd = $my_graph->plot(\@data); open(IMG, '>',$name.'.gif') or die $!; binmode IMG; print IMG $gd->gif; close IMG; }

      Use undef in place of zero in your data

      my @data = ( ['Mon','-','Tues','-','Wed','-'], [10,undef,40,undef,80,undef], [30,undef,50,undef,90,undef], [undef,100,undef,60,undef,30], [undef,60,undef,70,undef,50], );
      poj
        Hi Poj, that works for me, thanks. Now, I want 1 more graph with 3 stack bars. I have tried this by providing data in below format but its not working. Can u please help
        my @data = ( ['Mar1','Mar2','Mar3','Apr1','Apr2','Apr3','May1','May2','May3], [1,0,2,0,4,0], [2,0,1,0,5,0], [0,3,0,3,0,6], [0,4,0,4,0,7], [3,0,4,0,5,0], [0,0,0,0,0,0], );