in reply to Re^4: problem with show_values => 1, while drawing a bar graph with variables
in thread problem with show_values => 1, while drawing a bar graph with variables

How can change font size of values on the top of the bar.
my $font = '/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf'; $graph->set_values_font($font, 12);
update

If you split the cgi into 2 scripts you can show the graphic without creating a file.

#!/usr/bin/perl # page.cgi use strict; use CGI ':standard'; print header(); print start_html(-title =>'Global Mail Statistics', -bgcolor=>'#95B8DB'); print qq!<br/><center> <h1>Global Mail Statistics</h1> <img src = "bargraph.cgi"></center>!; print end_html;
#!/usr/bin/perl # bargraph.cgi use strict; use warnings; use CGI qw(:standard); use GD::Graph; use GD::Graph::bars; use CGI::Carp 'fatalsToBrowser'; # use only for testing my $logfile = '/var/log/maillog'; open $fh, '<', $logfile or die "Could not open $logfile : $!"; my %count=(); while (<$fh>){ if (/amavis.*(Passed|Blocked) (CLEAN|SPAM|INFECTED|BANNED)/){ ++$count{$2}; } elsif (/amavis.*(BAD-HEADER)-/) { ++$count{$1}; } elsif (/reject/){ ++$count{'REJECTED'}; } } close $fh; my @data = map{ $count{$_} || 0 } qw(CLEAN SPAM INFECTED BANNED BAD-HE +ADER REJECTED); my $data = GD::Graph::Data->new([ ["Cleaned","Spam","Virus","Banned","BadHeader","Rejected"], [@data], ]) or die GD::Graph::Data->error; my $graph = GD::Graph::bars->new(900,600); $graph->set( x_label => 'Category', y_label => 'Count', title => 'Global Mail Statistics for '.localtime, show_values => 1, y_tick_number => 8, bar_spacing => 10, shadow_depth => -4, shadowclr => 'dred', transparent => 0, ) or die $graph->error; my $font = '/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf'; $graph->set_title_font($font,14); $graph->set_values_font($font, 12); $graph->set_x_axis_font($font,10); $graph->set_y_axis_font($font,10); $graph->set_x_label_font($font,12); $graph->set_y_label_font($font,12); print header('image/png'); $graph->plot($data) or die $graph->error; binmode STDOUT; print $graph->gd->png;
poj

Replies are listed 'Best First'.
Re^6: problem with show_values => 1, while drawing a bar graph with variables
by theravadamonk (Scribe) on Mar 16, 2018 at 09:30 UTC

    Hi, thanks for your reply. I am ok with this font size. i.e - $graph->set_values_font($font, 8). My graph is PERFECT now. I thank you again for your wonderful support. Anyway, I would like to learn below codes since I want to draw in that way. because It's a very perl way. Could you pls explain below codes.

    my %count=(); # A hash -it is bcos of of key/value pairs? while (<$fh>){ if (/amavis.*(Passed|Blocked) (CLEAN|SPAM|INFECTED|BANNED)/){ ++$count{$2}; # Why ++$count? why {$2} here? is it bcos of (Passed +|Blocked) ? } elsif (/amavis.*(BAD-HEADER)-/) { ++$count{$1}; # why {$1} here? is it bcos of (BAD-HEADER) } elsif (/reject/){ ++$count{'REJECTED'}; #Hmm, Why {'REJECTED'}? } }

    Now I want to learn next code

    my @data = map{ $count{$_} || 0 } qw(CLEAN SPAM INFECTED BANNED BAD-HE +ADER REJECTED); Why map{ $count{$_} || 0 } ? qw(CLEAN SPAM INFECTED BANNED BAD-HEADER REJECTED) # here you create a +n array for count values?
    my $data = GD::Graph::Data->new([ ["Cleaned","Spam","Virus","Banned","BadHeader","Rejected"], [@data], ]) or die GD::Graph::Data->error; [@data] has count values coming from line qw(CLEAN SPAM INFECTED BANN +ED BAD-HEADER REJECTED)

    Could you pls explain me these, Since I PREFER this method and I can learn a lot of perl as well.

      my %count=();  # A hash -it is bcos of of key/value pairs?

      Yes, this declared a hash to store the results of counting the lines. The key will be one of either 'CLEAN','SPAM','INFECTED','BANNED','BAD_HEADER','REJECTED' and the value is the count.

      while (<$fh>){

      Read each line of the file into $_

        if (/amavis.*(Passed|Blocked) (CLEAN|SPAM|INFECTED|BANNED)/){

      Match the line read (in $_) against the regex pattern like grep. The first word captured (Passed or Block) will be in $1 and the second (CLEAN or SPAM or INFECTED or BANNED) will be in $2

      ++$count{$2}; # Why ++$count? why {$2} here? is it bcos of (Passed +|Blocked) ?

      For example, if $2 is the word CLEAN increment the hash $count{'CLEAN'}

      } elsif (/amavis.*(BAD-HEADER)-/) { ++$count{$1}; # why {$1} here? is it bcos of (BAD-HEADER)

      Yes, this regex has only 1 set of brackets so if it matches the word BAD-HEADER will be in $1

      } elsif (/reject/){ ++$count{'REJECTED'}; #Hmm, Why {'REJECTED'}? } }

      I chose REJECTED because 'rejected' was the label in your graph and all the other keys are upper case

      my @data = map{ $count{$_} || 0 } qw(CLEAN SPAM INFECTED BANNED BAD-HEADER REJECTED);

      map is a convenient way of creating an array from another another array. It is the same as

      my @data=(); $data[0] = $count{'CLEAN'} || 0; # use 0 if $count{'CLEAN'} is undefin +ed $data[1] = $count{'SPAM'} || 0; $data[2] = $count{'INFECTED'} || 0; $data[3] = $count{'BANNED'} || 0; $data[4] = $count{'BAD-HEADER'} || 0; $data[5] = $count{'REJECTED'} || 0;
      poj

        Thanks for your reply. I realized the code due to your kind help. my last code was Global mail stats. It has BOTH Inbound and Outbound mails. Now I want to write separate codes for Inbound and Outbound mails. I wrote below code for Inbound mails. It works. I hope it is OK. If there are any problems with my code, Pls enlighten me. Below code with grep Is what I want.

        #Passed CLEAN - Clean Mails my $clean_total = qx(sudo grep -Ec 'amavis.*Passed CLEAN.*RelayedInbou +nd' /var/log/maillog); chomp($clean_total); #$clean_total =~ s/^\s+|\s+$//g; #Blocked SPAM - Spam Mails my $spam_total = qx(sudo grep -Ec 'amavis.*Blocked SPAM.*DiscardedInbo +und' /var/log/maillog); chomp($spam_total); #$spam_total =~ s/^\s+|\s+$//g; #Blocked INFECTED - Virus Mails my $virus_total = qx(sudo grep -Ec 'amavis.*Blocked INFECTED.*Discarde +dInbound' /var/log/maillog); chomp($virus_total); #$virus_total =~ s/^\s+|\s+$//g; #Blocked BANNED - Banned Mails exe,scr etc. my $banned_total = qx(sudo grep -Ec 'amavis.*Blocked BANNED.*Discarded +Inbound' /var/log/maillog); chomp($banned_total); #$banned_total =~ s/^\s+|\s+$//g; #BAD-HEADER- - Bad Header Mails my $badheader_total = qx(sudo grep -Ec 'amavis.*BAD-HEADER-.*RelayedIn +bound' /var/log/maillog); chomp($badheader_total); #$badheader_total =~ s/^\s+|\s+$//g; #REJECT - REJECT Mails my $reject_total = qx(sudo grep -c 'smtpd.*reject' /var/log/maillog); chomp($reject_total); #$reject_total =~ s/^\s+|\s+$//g;

        But, Since it is NOT very professional perl method, I wrote below code. It works well. pls see it and give your comments

        #!/usr/bin/perl # inbound.cgi use CGI qw(:standard); #use CGI ':standard'; use strict; use warnings; use GD::Graph; use GD::Graph::bars; use CGI::Carp 'fatalsToBrowser'; # use only for testing my $logfile = '/var/log/maillog'; open my $fh, '<', $logfile or die "Could not open $logfile : $!"; my %count=(); while (<$fh>){ if (/amavis.*(Passed|Blocked) (CLEAN|SPAM|INFECTED|BANNED).*(Relayed +Inbound|DiscardedInbound)/){ ++$count{$2}; } elsif (/amavis.*(BAD-HEADER)-.*RelayedInbound/) { ++$count{$1}; } elsif (/smtpd.*reject/){ ++$count{'REJECTED'}; } } close $fh; my @data = map{ $count{$_} || 0 } qw(CLEAN SPAM INFECTED BANNED BAD-HE +ADER REJECTED); my $data = GD::Graph::Data->new([ ["Cleaned","Spam","Virus","Banned","BadHeader","Rejected"], [@data], ]) or die GD::Graph::Data->error; my $graph = GD::Graph::bars->new(600,300); $graph->set( x_label => 'Category', y_label => 'Count', title => 'Inbound Mail Statistics '.localtime, show_values => 1, y_tick_number => 8, bar_spacing => 10, shadow_depth => -4, shadowclr => 'dred', transparent => 0, ) or die $graph->error; my $font = '/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf'; $graph->set_title_font($font,12); $graph->set_values_font($font, 8); $graph->set_x_axis_font($font,8); $graph->set_y_axis_font($font,8); $graph->set_x_label_font($font,12); $graph->set_y_label_font($font,12); print header('image/png'); $graph->plot($data) or die $graph->error; binmode STDOUT; print $graph->gd->png;

        I want to generate both graphs withing page.cgi. So I rearranged it.

        #!/usr/bin/perl # page.cgi use strict; use CGI ':standard'; print header(); print start_html(-title =>'Global Mail Statistics', -bgcolor=>'#95B8DB'); print qq!<br/><center> <h1>Mail Statistics</h1> <img src = "bargraph.cgi"></center>!; print qq!<br/><br/><center> <img src = "inbound.cgi"></center>!; print end_html;

        pls advice me if it is OK? Next, I am going to write outbound.cgi. I will let you know if I I have any issues? Sir, Thanks you very much for your meritorious deed. Hope to hear from you.