Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-28-2009, 10:41 AM   PM User | #1
vinodkaushik
New to the CF scene

 
Join Date: Jul 2009
Location: india
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vinodkaushik is an unknown quantity at this point
Using Perl, how can I create charts using values in a CSV file?

I am new to this and need a clue on how to do this task. I have a csv file with following sample data:

site,type,2009-01-01,2009-01-02,....
X,A,12,10,...
X,B,10,23,...
Y,A,20,33,...
Y,B,3,12,...

and so on....


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..
vinodkaushik is offline   Reply With Quote
Old 08-28-2009, 12:29 PM   PM User | #2
Jolle
New Coder

 
Join Date: Aug 2009
Location: in front of the keyboard
Posts: 17
Thanks: 1
Thanked 1 Time in 1 Post
Jolle is an unknown quantity at this point
To get your file, you can try following :

Code:
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.
Jolle is offline   Reply With Quote
Old 08-28-2009, 12:59 PM   PM User | #3
vinodkaushik
New to the CF scene

 
Join Date: Jul 2009
Location: india
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vinodkaushik is an unknown quantity at this point
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.
vinodkaushik is offline   Reply With Quote
Old 08-28-2009, 01:14 PM   PM User | #4
Jolle
New Coder

 
Join Date: Aug 2009
Location: in front of the keyboard
Posts: 17
Thanks: 1
Thanked 1 Time in 1 Post
Jolle is an unknown quantity at this point
Quote:
Originally Posted by vinodkaushik View Post
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 :
Code:
my @grapharray = (\@dates,$myfile{$site}{$label})
Jolle is offline   Reply With Quote
Old 09-01-2009, 10:52 AM   PM User | #5
vinodkaushik
New to the CF scene

 
Join Date: Jul 2009
Location: india
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vinodkaushik is an unknown quantity at this point
Hi,

I have created an array for dates as below.
Code:
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.
vinodkaushik is offline   Reply With Quote
Old 09-01-2009, 05:28 PM   PM User | #6
Jolle
New Coder

 
Join Date: Aug 2009
Location: in front of the keyboard
Posts: 17
Thanks: 1
Thanked 1 Time in 1 Post
Jolle is an unknown quantity at this point
Quote:
Originally Posted by vinodkaushik View Post
Hi,

I have created an array for dates as below.
Code:
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,...
Jolle is offline   Reply With Quote
Old 09-02-2009, 06:51 AM   PM User | #7
vinodkaushik
New to the CF scene

 
Join Date: Jul 2009
Location: india
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vinodkaushik is an unknown quantity at this point
I need the all values for each type per site.
vinodkaushik is offline   Reply With Quote
Old 09-03-2009, 09:07 AM   PM User | #8
vinodkaushik
New to the CF scene

 
Join Date: Jul 2009
Location: india
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
vinodkaushik is an unknown quantity at this point
Just to update here. I found the solution using

Code:
use List::Util qw(sum)
my @sums = map { sum(@{$_}) } @array;
my $sum_of_sums = sum(@sums);
my @percentages = map { $_ / $sum_of_sums } @sums;
vinodkaushik is offline   Reply With Quote
Reply

Bookmarks

Tags
perl

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:52 PM.


Advertisement
Log in to turn off these ads.