Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Little more complicated because you have to define an order for the categories, I have chosen a alphabetic sort.
#perl use strict; use GD::Graph; use GD::Graph::bars; my $YEAR1 = 2013; my @period=(); my %y_data=(); my @x_legend=(); my %x_data=(); # store data while (<DATA>){ chomp; my ($m,$y,$cat,$n) = split ',',$_; # convert year/month to period number my $pd = ym_to_pd($y,$m); $period[$pd] = "$m/$y"; $y_data{$cat}[$pd] = $n; ++$x_data{$pd}; } # graph data my @y_plotdata; my @categories = sort keys %y_data; push @y_plotdata,[@categories]; for my $pd (sort {$a<=>$b} keys %x_data){ push @x_legend,$period[$pd]; my @y_data=(); for my $cat (@categories){ push @y_data,$y_data{$cat}[$pd]; } push @y_plotdata,[ @y_data ]; } # create graph my $my_graph = GD::Graph::bars->new(800,600); $my_graph->set( x_label => 'X Label', y_label => 'Y label', title => 'Title', ); $my_graph->set_legend(@x_legend); my $img = $my_graph->plot(\@y_plotdata) or die $my_graph->error; # save image open(IMG,'>','plot.gif') or die "$!"; binmode IMG; print IMG $img->png; close IMG; # convert year,month to period sub ym_to_pd { my ($yr,$mth) = @_; return ($yr - $YEAR1) * 12 + $mth; } # convert period to year.month sub pd_to_ym { my $ix = shift; my $yr = int(($ix-1)/12)+$YEAR1; my $mth = $ix-12*($yr-$YEAR1); return ($yr,$mth); } #test_periods(); sub test_periods { for my $y (2013..2015){ for my $m (1..12){ my $pd = ym_to_pd($y,$m); my ($yr,$mth) = pd_to_ym($pd); print "$y $m => $pd => $yr $mth\n"; } } } __DATA__ 3,2013,Address,1 2,2013,Equipment,13 3,2013,Equipment,18 4,2013,Equipment,17 5,2013,Equipment,8 3,2013,Database Reconciliation,3 5,2013,Database Reconciliation,3 2,2013,Design/Process,123 3,2013,Design/Process,74 4,2013,Design/Process,42 5,2013,Design/Process,30 1,2014,Design/Process,30
Updated : period loop uses new %x_data :
for my $pd (sort {$a<=>$b} keys %x_data){ push @x_legend,$period[$pd];
poj

In reply to Re^5: Parsing an SQL table using an array of hashes? by poj
in thread Parsing an SQL table using an array of hashes? by chareTX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-03-19 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found