|
addBoxWhiskerLayer in perl |
Posted by kyle on Jun-01-2012 03:58 |
|
I havent been able to find any example for creating a box whisker graph, when you are getting data from a file. I feel like i have everything set up but when I look at the final image that is created the graph is there and correct except the whisker boxes aren't being shown. The code is:(some of it is unnessesary code because i have tried a 1000 ways to display it) I have also sent the information that is in the arrays. Help would be appreciated
#!/usr/bin/perl
use perlchartdir;
use strict;
use Win32::ODBC;
use User::pwent;
use File::stat;
use Fcntl;
use Scalar::Util qw(looks_like_number);
my @Q1; my @Q2; my @Q3; my @Q4; my @Q5;my @names;
my $sample_time;
my $host_name;
my $bytes_in;
my $bytes_out;
my $cpu_system;
my $smallest = 1000000; my $largest = 0;
my $cpu_user;
my $cpu_wio;
my $load_fifteen;
my $name;
my $mem_used;
my $load_five;
my $load_one;
my $mem_buffers;
my $mem_cache;
my $line;
my $mem_free;
my $mem_shared;
my $disk_io_wr;
my $mem_pgm;
my $swap_used;
my $disk_used;
my $total_memory;
my @tot_mem;
my @rows;
my @result;
my $setter = 0;
sysopen CTLFILE,"inputdata1.csv", O_RDONLY ## Open the data file for read
or die ## to get the message
"Can not read input file inputdata.csv- $!\\n";
while (defined($line=<CTLFILE>)) {
chomp($line);
@result = split(",",$line);
$name = $result[0];
if($setter == 0){
$setter =1;
next;
}
elsif(@names == ()){
$total_memory = $result[11]+$result[12]+$result[17];
push(@tot_mem, $total_memory);
unshift(@names, $name);
}
elsif($names[0] ne $name && $setter==1 ){
&calculation(@tot_mem);
@tot_mem = ();
$total_memory = $result[11]+$result[12]+$result[17];
push(@tot_mem, $total_memory);
unshift(@names,$name);
}elsif($name eq $names[0] && $setter ==1 ){
$total_memory = $result[11]+$result[12]+$result[17];
push(@tot_mem, $total_memory);
}
}
&makeGraph();
sub calculation(@){
my $i; my $arraysize;
@_=sort(@_);
my $min = $_[0];
push(@Q1, $min);
if($min < $smallest){
$smallest=$min;
}
my $max = $_[-1];
push(@Q5, $max);
if($max > $largest){
$largest=$max;
}
#print($max."\\t".($max-3000000)."\\n");
my $firstQ; my $median; my $thirdQ;
my $count = scalar(@_);
for($i = 0; $i < $count; $i++){
$arraysize++;
}
if($arraysize%2 == 1){
$i = ($arraysize+1)/2;
$median = $_[$i-1];
push(@Q3, $median);
$i = (.25)*($arraysize + 1);
$firstQ = $_[$i-1];
push(@Q2, $firstQ);
$i = (.75)*($arraysize + 1);
$thirdQ =$_[$i-1];
push(@Q4, $thirdQ);
}
elsif($arraysize%2==0){
$i = ($arraysize)/2;
$median = $_[$i-1];
push(@Q3, $median);
$i = (.25)*($arraysize );
$firstQ = $_[$i-1];
push(@Q2, $firstQ);
$i = (.75)*($arraysize );
$thirdQ =$_[$i-1];
push(@Q4, $thirdQ);
}
}
sub makeGraph(){
my $r=0;
my $Q1Data= ("@Q1");
my $Q2Data= ("@Q2");
my $Q3Data= ("@Q3");
my $Q4Data= ("@Q4");
my $Q5Data= ("@Q5");
my $Q1string = join(", ",@Q1);#print("MIN: ".$Q1string."\\n");
my $Q2string = join(", ",@Q2);#print("FirstQ: ".$Q2string."\\n");
my $Q3string = join(", ",@Q3);#print("Median: ".$Q3string."\\n");
my $Q4string = join(", ",@Q4);#print("ThirdQ: ".$Q4string."\\n");
my $Q5string = join(", ",@Q5);#print("Max: ".$Q5string."\\t");
@Q1 = join(", ",@Q1);
@Q2 = join(", ",@Q2);
@Q3 = join(", ",@Q3);
@Q4 = join(", ",@Q4);
@Q5 = join(", ",@Q5);
print("Min: ".$Q1[$r]."\\n");
print("First Q:".$Q2[$r]."\\n");
print("Median".$Q3[$r]."\\n");
print("Third Q:".$Q4[$r]."\\n");
print("Max: ".$Q5[$r]."\\n");
#if(looks_like_number($Q1Data)){print"Now thats a number";}
#else{print"that's no number";}
#
my $labels =["group_one","group_two","group_three","group_four"];
my $scale = [3000000,3250000,3500000,3750000,4000000,4250000];
my $c = new XYChart(550, 250);
$c->setPlotArea(50, 25, 450, 200)->setGridColor(0xc0c0c0, 0xc0c0c0);
$c->addTitle("Memory Usage for each Server");
$c->xAxis()->setLabels($labels)->setFontStyle("arialbd.ttf");
$c->yAxis()->setLabelStyle("arialbd.ttf");
$c->yAxis()->setLabels2($scale);
foreach $r(@Q1){
$c->addBoxWhiskerLayer($Q4[$r],$Q2[$r] ,$Q5[$r] , $Q1[$r] ,$Q3[$r], 0x9999ff,
0x0080000)->setLineWidth(2);}
$c->makeChart("BOXwhisker3.png");
close CTLFILE or die "We still have one last problem"; }
|
Re: addBoxWhiskerLayer in perl |
Posted by Peter Kwan on Jun-02-2012 09:04 |
|
Hi kyle,
The box-whisker graph is empty because your code does not pass any data to ChartDirector. You can try to print your data out to confirm it is empty:
foreach $r(@Q1){
print $Q4[$r];
print $Q2[$r];
print $Q5[$r];
print $Q1[$r];
print $Q3[$r];
$c->addBoxWhiskerLayer($Q4[$r],$Q2[$r] ,$Q5[$r] , $Q1[$r] ,$Q3[$r], 0x9999ff,
0x0080000)->setLineWidth(2);
}
The data are empty because your code indexes the arrays incorrectly.
Also, ChartDirector expects the data to be array references, not a scalar number, so you do not need to use a loop at all. You may simply pass the array references to ChartDirector without using any loop:
$c->addBoxWhiskerLayer(\\@Q4, \\@Q2, \\@Q5, \\@Q1, \\@Q3, 0x9999ff, 0x0080000)->setLineWidth(2);
Hope this can help.
Regards
Peter Kwan |
|