Skip to content

Instantly share code, notes, and snippets.

@ryan5500
Created September 3, 2009 13:42
Show Gist options
  • Save ryan5500/180301 to your computer and use it in GitHub Desktop.
Save ryan5500/180301 to your computer and use it in GitHub Desktop.
use strict;
use GD::Graph::bars;
use Path::Class qw(dir);
my $dir = dir('.');
my $handle = $dir->open;
while (my $file = $handle->read) {
next unless $_ =‾ /log$/;
my $data = get_data($filename);
make_graph($data, $filename, "'Before' state histgram", 'before state', 'person number');
}
sub get_data {
my $filename = shift;
open(my $fh, $filename);
my @labels;
my @dataset;
while(<$fh>) {
next if $_ =‾ /^#/;
my @strs = split ',', $_;
push @labels, $strs[0];
$strs[1] =‾ s/¥s*$//;
push @dataset, $strs[1];
}
my @data = ( ¥@labels, ¥@dataset);
return ¥@data;
}
sub make_graph {
my ($data, $filename, $title, $x_label, $y_label) = @_;
my $graph = GD::Graph::bars->new( 800, 600 );
$graph->set(title => $title,
x_label => $x_label,
y_label => $y_label);
$graph->set_title_font( "GOTHIC_FONT", 14 );
$graph->set_legend_font( "GOTHIC_FONT", 8 );
$graph->set_x_axis_font( "GOTHIC_FONT", 8 );
$graph->set_x_label_font( "GOTHIC_FONT", 10 );
$graph->set_y_axis_font( "GOTHIC_FONT", 8 );
$graph->set_y_label_font( "GOTHIC_FONT", 8 );
my $image = $graph->plot( $data );
open( OUT, "> $filename.jpg") or die( "Cannot open file: $filename.jpg" );
binmode OUT;
print OUT $image->jpeg();
close OUT;
}
use strict;
use GD::Graph::bars;
my $filename = $ARGV[0];
if (!$filename) {
die('please input data filename.');
}
my $graph_filename = $ARGV[1] ? $ARGV[1] : 'graph';
my $data = get_data($filename);
make_graph($data, $graph_filename, 'stay time histgram (stay near bench)', 'stay duration(second)', 'stayed person number');
sub get_data {
my $filename = shift;
open(my $fh, $filename);
my @labels;
my @dataset;
while(<$fh>) {
my @strs = split ',', $_;
push @labels, $strs[0];
$strs[1] =‾ s/¥s*$//;
push @dataset, $strs[1];
}
my @data = ( ¥@labels, ¥@dataset);
return ¥@data;
}
sub make_graph {
my ($data, $filename, $title, $x_label, $y_label) = @_;
my $graph = GD::Graph::bars->new( 800, 600 );
$graph->set(title => $title,
x_label => $x_label,
y_label => $y_label);
$graph->set_title_font( "GOTHIC_FONT", 14 );
$graph->set_legend_font( "GOTHIC_FONT", 8 );
$graph->set_x_axis_font( "GOTHIC_FONT", 8 );
$graph->set_x_label_font( "GOTHIC_FONT", 10 );
$graph->set_y_axis_font( "GOTHIC_FONT", 8 );
$graph->set_y_label_font( "GOTHIC_FONT", 8 );
my $image = $graph->plot( $data );
open( OUT, "> $filename.jpg") or die( "Cannot open file: $filename.jpg" );
binmode OUT;
print OUT $image->jpeg();
close OUT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment