使用数据图表处理工具gnuplot,结合Perl脚本,可以随意绘制自己的柱状图。
1. mkbar --- Perl script
2. mkbar.dat --- Config file
3. README --- readme
* mkbar
#!/usr/bin/perl
############################################################################### # COPYRIGHT GPLv2 2008-2010 # File name: mkbar # Summary: Create bar chart. # Author: poseidonyu <> ###############################################################################
use strict; use vars qw($opt_h $opt_s $opt_o $opt_t $opt_l $opt_r); use Getopt::Std;
my $format = "png"; my $source_file = "mkbar.dat"; my $output_file = "mkbar.$format"; my $gnuplot_file = "mkbar.gp"; my $data_file = "data"; my $title = ""; my $bcinfo = {};
my $encoding = "iso_8859_1"; my $solid = 0.5; my $border = 0; my $xlabel = ""; my $ylabel = ""; my $keyp = 0;
my $mkbar_help = " Usage: perl mkbar [-h] [-s ] [-o ] [-t ] [-l] [-r]
[-h] Display this message. [-s ] Specify the source file contains the data. [-o ] Specify the output file. [-t ] Specify the title of bar chart. [-l] Set key on left. [-r] Set key on right.
";
sub get_info($); sub create_dataf($); sub create_gpf($); sub create_barchart($); sub help($);
getopts("hs:o:t:lr") or die "ERROR: invalid option, -h for help.\n";
if ($opt_h) { help($mkbar_help); } if (defined($opt_s)) { $source_file = "$opt_s"; } if (defined($opt_o)) { $output_file = "$opt_o.$format"; } if (defined($opt_t)) { $title = "$opt_t"; } if ($opt_l && $opt_r) { print STDERR "ERROR: -l and -r can not be used simultaneous, -h for help.\n"; } if ($opt_l) { $keyp = 0; } if ($opt_r) { $keyp = 1; }
get_info($source_file); create_dataf($data_file); create_gpf($gnuplot_file); create_barchart($gnuplot_file); unlink "$data_file"; unlink "$gnuplot_file";
exit 0;
sub get_info($) { my $sfile = shift;
open(SFILE, "$sfile") or die "ERROR: source file \"$sfile\" does not exist.\n";
while (my $line = <SFILE>) { chomp $line; next if (! $line);
if ($. == 1) { my @objects = split(/\s+/, $line); $bcinfo->{'objects_aref'} = \@objects; } elsif ($. == 2) { my @labels = split(/\s+/, $line); $bcinfo->{'labels_aref'} = \@labels; } else { my @desc = split(/\s+/, $line); my $one_desc = {}; $one_desc->{'class'} = shift @desc; $one_desc->{'class_value_aref'} = \@desc;
push @{$bcinfo->{'desc_aref'}}, $one_desc; } }
close(LOG); }
sub create_dataf($) { my $dfile = shift;
open(DFILE, "> $dfile") or die "ERROR: data file \"$dfile\" does not exist.\n";
my $i = 0; foreach my $one_desc (@{$bcinfo->{'desc_aref'}}) { print DFILE "$i\t$one_desc->{'class'}";
foreach my $one_class_value (@{$one_desc->{'class_value_aref'}}) { print DFILE "\t$one_class_value"; }
print DFILE "\n";
$i++; }
close(DFILE); }
sub create_gpf($) { my $gpfile = shift;
open(GPFILE, "> $gpfile") or die "ERROR: gnuplot file \"$gpfile\" does not exist.\n";
print GPFILE "#!/usr/bin/gnuplot\n";
print GPFILE "set encoding $encoding\n"; print GPFILE "set style data boxes\n"; print GPFILE "set style fill solid $solid border $border\n";
print GPFILE "set terminal $format small xfdf5e6\n"; print GPFILE "set output '$output_file'\n"; print GPFILE "set title '$title'\n";
print GPFILE "set grid ytics\n"; print GPFILE "set xlabel '$bcinfo->{'labels_aref'}[0]'\n"; print GPFILE "set ylabel '$bcinfo->{'labels_aref'}[1]'\n";
my $keyp_string = ""; if (! $keyp) { $keyp_string = "left"; } else { $keyp_string = "right"; }
print GPFILE "set key top $keyp_string\n"; print GPFILE "set key box\n";
my $xrangemax = scalar @{$bcinfo->{'desc_aref'}}; print GPFILE "set xrange [0:$xrangemax]\n"; my $xtic_string = "set xtic ("; my $xrange = 0; while ($xrange <= $xrangemax) { my $xtic_tmp_string = "'' $xrange,"; $xtic_string .= $xtic_tmp_string; $xrange++; } chop($xtic_string); $xtic_string =~ s/,/, /g; $xtic_string .= ")";
print GPFILE "$xtic_string\n";
my $xlabel_excursion = "-0.03"; my $xlabel = 1; while ($xlabel <= $xrangemax) { my $xposition = $xlabel - 0.5; print GPFILE "set label $xlabel '$bcinfo->{'desc_aref'}->[$xlabel - 1]->{'class'}' at $xposition, graph $xlabel_excursion, 0 centre norotate\n"; $xlabel++; } print GPFILE "set yrange [0:]\n";
my $classbar_number = scalar @{$bcinfo->{'objects_aref'}};
my $boxwidth = 1 / ($classbar_number + 2); print GPFILE "set boxwidth $boxwidth absolute\n";
print GPFILE "plot "; my $i = 1; my $color = 3; my $bar_string = ""; while ($i <= $classbar_number) { my $linenum = $i + 2; my $bar_excursion = $boxwidth * $i + $boxwidth / 2; my $bar_tmp_string = "'$data_file' using (\$1+$bar_excursion):$linenum title '$bcinfo->{'objects_aref'}->[$i - 1]' linetype $color,"; $bar_string .= $bar_tmp_string; $color++; $i++; } chop($bar_string); $bar_string =~ s/,/, /g; print GPFILE "$bar_string\n";
close(GPFILE); }
sub create_barchart($) { my $gpfile = shift;
system("gnuplot $gpfile"); }
sub help($) { my $msg = shift; print STDERR $msg;
exit 0; }
|
* mkbar.dat
fedora9 fedora10 fedora11 fedora12
Processes Times(Sec)
20 0.5576 0.5972 0.5872
40 0.7305 0.7717
60 0.9294 0.9524 0.9124 0.9374
80 1.1275 1.1557
100 1.3324 1.3640 1.3540 1.3824
* README
1. Usage:
=====Don't use options=====
# ls
mkbar mkbar.dat README
# ./mkbar
# ls
mkbar mkbar.dat mkbar.png README
=====Use options=====
# cp mkbar.dat hackbench.dat
# ./mkbar -s hackbench.dat -o hackbench -t "HackBench BarChart" -l
# ls
hackbench.dat hackbench.png mkbar mkbar.dat mkbar.png README
2. Format
*****The Format of Datafile*****
fedora9 fedora10 fedora11 fedora12 --> Line 1 must be items which will be compared
Processes Times(Sec) --> Line 2 must be xlabel and ylabel
--> Can be blank from line 3, will be ignored
20 0.5576 0.5972 0.5872 --> Category and values of items
40 0.7305 0.7717 --> Had better compatible with Line 1
60 0.9294 0.9524 0.9124 0.9374
80 1.1275 1.1557
100 1.3324 1.3640 1.3540 1.3824
阅读(767) | 评论(0) | 转发(0) |