1#!/usr/local/bin/perl 2use perlchartdir; 3use strict; 4 5my $filename; 6my $title; 7my $topN=0; 8my $print=0; 9 10&init; 11 12my %count; 13while (<STDIN>) { 14 chomp; 15 split; 16 $count{$_[0]}+=$_[1]; 17} 18 19my @twoD; 20my $i=0; 21for my $a (keys %count) { 22 $twoD[$i][0]=$a; 23 $twoD[$i++][1]=$count{$a}; 24} 25 26 27# sort by label: @labels = sort {$a <=> $b} @labels; 28 29@twoD = reverse sort { $a->[1] <=> $b->[1] } @twoD ; 30 31if ($topN>0) { 32 @twoD = @twoD[0..$topN-1]; 33} 34 35my @data = map $_->[ 1 ], @twoD; 36my @labels = map $_->[ 0 ], @twoD; 37 38if ($print) { 39 print join("\n",@labels); 40} 41 42my $c = new XYChart(800, 800,0xffffff,-1,-1); 43$c->swapXY(1); 44$c->setPlotArea(100, 45, 650, 700, 0xffffff, -1, 0xffffff, $perlchartdir::Transparent, $perlchartdir::Transparent); 45$c->addTitle($title, "arialb.ttf", 14); 46$c->xAxis()->setLabels(\@labels); 47$c->yAxis()->setLogScale(); 48$c->xAxis()->setColors($perlchartdir::Transparent, 0); 49 50my $layer = $c->addBarLayer(\@data,0x888888); 51$layer->setBorderColor($perlchartdir::Transparent); 52$layer->setBarGap(0.1); 53 54$c->makeChart($filename); 55 56exit; 57 58sub init() { 59 my %opt; 60 use Getopt::Std; 61 getopts("hpf:n:t:", \%opt ) or usage(); 62 63 # Help? 64 usage() if $opt{h}; 65 66 $filename = $opt{f} if $opt{f}; 67 $title = $opt{t} if $opt{t}; 68 $topN = $opt{n} if $opt{n}; 69 $print = 1 if $opt{p}; 70} 71 72sub usage() { 73 74 print STDERR << "EOF"; 75 76Usage: cat file.csv | bar2.pl [-hp] -f outputfile [-t title] [-n topN]"); 77 78-f : output file name, ending in .PNG 79-h : this (help) message 80-n : only show the top N entires 81-p : print the labels 82-t : chart title 83 84EOF 85 exit; 86} 87 88 89