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.
#!/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;
Comments