37 users online (1 members and 36 guests)  


  Results 1 to 2 of 2

Related

  1. table row values    Forum: HTML Forum
    Replies: 1
  2. Charts    Forum: HTML Forum
    Replies: 0
  3. Getting min/max values from an array    Forum: CGI Perl Forum
    Replies: 1
  4. adding up my values    Forum: Javascript Forum
    Replies: 1
  5. Creating Charts in HTML    Forum: HTML Forum
    Replies: 4
  1. #1
    octopus's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2009
    Posts
    1

    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?

  2. #2
    xmassey's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    100

    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



Tags for this Thread