I want to create a perl script to read data from the csv file (as per the given user input) and create XY(scatter) charts. Let's say that I want to create a chart for type B. The user should input something like "A" OR "B", and the chart should be created with the values from CSV file. Dates in X axis and Value numbers on Y axis with chart lines of values for each type A and B
I have searched for the modules in CPAN and I think below can help me but not sure how to start with.
#!/usr/bin/perl -w
use strict;
use Text::CSV;
use GD;
use Getopt::Long;
Can anyone please suggest me some code to start with?
Last edited by vinodkaushik; 08-28-2009 at 12:55 PM..
use Text::CSV
use IO::File
my @array;
my $fh = new IO::File($file_name,"r") or die "Couldn't open $file_name : $! \n";
my $csv = Text::CSV -> new();
$csv -> column_names ($csv -> getline($fh));
while ($csv ->getline_hr($fh)){
push (@array,$_);
}
This returns an array with hashrefs, that would allow you to do something like :
Code:
my @graph_array;
foreach (@array){
if (%{$_}{type} eq "B") {
push (@graph_array[0],%{$_}{"site"})
push ( @graph_array[1],%{$_}{"2009-01-01"})
}
}
* do something with the array to plot *
You need to construct an array of arrays if you want to use GD. Now depending on what you want to plot, you'll have to do something extra with it. Personally, I'm a bit baffled. You can't make an XY scatter plot with only one set of scalars. Please elaborate.
Thanks for your comments. I have edited my question and hope this is clear now. Actually I want to create graphs between dates and values. The user should input "type" i.e. A or B.
Thanks for your comments. I have edited my question and hope this is clear now. Actually I want to create graphs between dates and values. The user should input "type" i.e. A or B.
Still, the problem remains. Should he do that for one site only, sum up the numbers for all sites, take the average,...
How to tackle the problem really depends on this. And actually, I wouldn't even go through the hassle of using csv. It's far easier to just load the file using the classic file handles, and construct a hash of arrays with keys "A","B","Date" and use the split() function to construct the arrays from the lines. From the data you have, I would even say to construct such a hash for every site, and combine them in a hash of hashes.
Then you get something like :
Code:
my %myfile;
while (<IN>){
chomp $_;
@line = split (",",$_);
my $site = shift @line;
my $label = shift @line;
$myfile{$site}{$label} = \@line;
}
If you have an array @dates with the dates (easily obtained from the first line), you can then construct an array of arrays for the GD functions :
open(my $csv_fh, '<', $csv_file) or die $!;
my $parser = Text::CSV->new();
my @dates = $parser->column_names( $parser->getline($csv_fh) );
my @dt = splice(@dates, 2);
but still need help to get values corresponding to given type. Please help.
open(my $csv_fh, '<', $csv_file) or die $!;
my $parser = Text::CSV->new();
my @dates = $parser->column_names( $parser->getline($csv_fh) );
my @dt = splice(@dates, 2);
but still need help to get values corresponding to given type. Please help.
Please specify the question : you need them for each type over all sites, per site, want to average, sum, need all values,...