"...Remember being a youngster, mom or dad telling you "close the door"? Well the same applies to HTML ...."

Go Back   Webmaster Forums > Code Forum > CGI Perl Forum

Reply
 
Thread Tools Rate this Webmaster Discussion
Old 09-01-2009, 12:57 AM   #1
octopus
New Member
 
Join Date: Aug 2009
Webmaster Discussions: 1
Rep Power: 7
octopus is on a distinguished road
Read Values from CSV and create charts

Hi,

I am new to perl. I have to create charts (XY scatter) from the values from CSV file. Below is the sample file I have.
Code:
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 "B", and the chart should be created with the corresponding values from CSV file.

Can anyone please suggest me some code to start with?


octopus is offline   Reply With Quote
Old 11-20-2009, 09:18 AM   #2
xmassey
Senior Member
 
Join Date: Apr 2007
Webmaster Discussions: 98
Rep Power: 27
xmassey is a name known to allxmassey is a name known to allxmassey is a name known to allxmassey is a name known to allxmassey is a name known to allxmassey is a name known to all
Re: Read Values from CSV and create charts

Hey,

I see that you have 4 headings...

site
type
date1
date2

I assume the ...'s indicate there are further dates. Is there a set number of dates or do you need the script to be able to count the number of headings - 2 (therefore the number of dates/headings)?

You should first parse the data into a hash table (use the split function) taking into account which type should be included. Then you should use the GD module to produce a graph of the data.

A very basic example of parsing the data (I did not test this, and its very rough)...

Code:
my $chosentype = 'A';
my $row = 1;
my @headings;
my %hash;
while(<FILE>){
     #split the data up by the comma
     my ($site,$type,@data) = split(/\,/,$_);
     #if the 1st row in the file then use this row for the headings
     if ($row == 1){
          @headings = @data;
     }
     #else its actual data
     else {
          #if the type is the type you want to parse
          if ($type eq $chosentype) {
                # build the hash (using the row number as a unique identifyer of each row
                $hash{$row}{'site'} = $site;
                $hash{$row}{'type'} = $type;
                foreach (@headings) {
                      $hash{$row}{$_} = shift(@data);
                }
          }
     }
     $row++;
}
That should give you an idea how to parse the data before using the GD module to produce a graph. You may probably want to change the way the hash is built to suit your data. You would probably also want to count the size of @headings/@data to ensure there are the same number of headings as there data, if not then pass into an error hash or something.

Chris
xmassey is offline   Reply With Quote
Reply

Bookmarks

Tags
perl

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 Off
HTML code is Off


Similar Webmaster Discussions
Thread Webmaster Discussion Starter Forum Replies Last Post
Charts lumpuk HTML Forum 0 10-25-2007 03:03 AM
read file davidmook CGI Perl Forum 0 06-29-2006 04:49 AM
Read before posting. HTML AHFB Feedback and Announcements 0 11-07-2005 09:40 AM
Creating Charts in HTML gdmay HTML Forum 4 11-11-2002 12:23 AM


All times are GMT -5. The time now is 08:57 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.