CSV to Graph - Convert Siege Log into Bar Graph

CSV to Graph - Convert Siege Log into bar Graph - a simple perl script that reads a csv file and converts it into bar graph.

CSV to Graph, Convert Siege Log into Bar Graph


#!/usr/bin/perl

use strict;
use Text::ParseWords;
use GD::Graph::bars;
use Data::Dumper;

#my $file = 'siege.csv';
my $file  = $ARGV[0];
my ($output_file) = ($file =~ /(.*)\./);

my @data;
my @legends;

# parse csv
open(my $fh, '<', $file) or die "Can't read csv file '$file' [$!]\n";

my $countlines = 0;

while (my $line = <$fh>) {
    chomp $line;
    my @fields = Text::ParseWords::parse_line(',', 0, $line);

    my @field = (@fields[1],@fields[2],@fields[3],@fields[4],@fields[5],@fields[6],@fields[7],@fields[8],@fields[9]);
    push @data, \@field;

    if($countlines >= 1){
        push @legends, @fields[0];
    }
    $countlines++;
}

# max 7 day siege log
splice @data, 1, -7;
splice @legends, 0, -7;

# plot to graph

my $mygraph = GD::Graph::bars->new(1024, 768);

$mygraph->set(
    y_tick_number => 1,
    values_vertical => 1,
    bargroup_spacing => 10,
    show_values => 1,
) or warn $mygraph->error;

$mygraph->set_legend(@legends);

my $myimage = $mygraph->plot(\@data) or die $mygraph->error;

my $format = $mygraph->export_format;
open(IMG, ">$output_file.$format") or die $!;
binmode IMG;
print IMG $myimage->gif;
close IMG;

1 comment:

Anonymous said...

Thank you for the code to graph siege logs. I prefer to use the label function with siege. I have included a simple next if to prevent parsing of the label (****)

Kindest regards.

--- ../siege_bar 2012-09-27 15:19:02.000000000 -0700
+++ siege_bar 2012-09-27 15:15:25.000000000 -0700
@@ -18,6 +18,9 @@
while (my $line = <$fh>) {
chomp $line;
my @fields = Text::ParseWords::parse_line(',', 0, $line);
+
+ #Do not parse the Label tag if it exists
+ next if ($line =~ /\*\*\*\*/);
my @field = (@fields[1],@fields[2],@fields[3],@fields[4],@fields[5],@fields[6],@fields[7],@fields[8],@fields[9]);
push @data, \@field;