0

I am trying to make a web page that displays a graph about the netapp filer usage.

I read the data from the file which is stored in the following fashion

Name   usedSpace   available_space    kbytes       quota_volume   capacity
Juno   889347800   1795006760         2684354560   1698693120     33%

This is my Perl code

use CGI;
use Time::Local;
use List::MoreUtils qw(uniq);
use GD;
use GD::Graph::bars;
use GD::Graph::lines; 
use GD::Graph::area;

open (MYFILE, 'my_data.csv');

@filesystem_name = ();
@my_date = ();
@my_used = ();
@my_avail = ();
@my_kbytes = ();
@my_quota_volume = ();
@my_capacity = ();

#read all the data and store fields into arrays
while (<MYFILE>) {
    if(/$user_selected_filer/) {
    ($dummy,$current_line_date,$dummy,$dummy,$dummy,$dummy,$dummy) =  split (/,/, $_);     
    if(($current_line_timestamp ge $start && $current_line_timestamp le $end)){

            ($name,$date_after,$used,$avail, $kbytes, $quota_volume, $capacity) = split  (/,/, $_);
            ($month_after,$day_of_month_after,$year_after) = split ('/', $date_after);
            push (@filesystem_name,$name);
            push (@my_used,$used/1000000);
            push (@my_avail,$avail/1000000);
            push (@my_kbytes,$kbytes/1000000);
            push (@my_quota_volume,$quota_volume/1000000);
            push (@my_capacity,$capacity);
            push (@my_date_after, $month_after . "/" . $day_of_month_after . "/" . $year_after);
            $count++;
    }
    }

}

These arrays are good for making graphs using Perl as long as I am using GD graphs. But I want to do these graphs on a web page which requires JavaScript arrays.

I am kinda lost with this whole JSON thing.

5
  • Are you planning on using a charting library to generate the graphs in JavaScript? If so, which one? I'm sure they all require different formats of input data; you should know what format to use before crafting your JSON. Commented Mar 19, 2014 at 21:24
  • I want to do Google charts which uses JavaScript. Here is the link to it developers.google.com/chart/interactive/docs/gallery/linechart Commented Mar 19, 2014 at 21:59
  • See, vote to close because this script does nothing with your input data. The script splits at ,, your data is <tab> separated (or spaces?) so don't saying about it usefulness... Why didn't show 1.) your REAL input, 2.) the WANTED output 3.) your REAL actual tryings?! Commented Mar 19, 2014 at 22:10
  • The data is separated by commas. Commented Mar 19, 2014 at 22:14
  • @user3353628 jm666's point is that the data you included in your question is not separated by commas. Regardless, friedo gave a good example of converting a hash of arrays to JSON in your previous question. Your JavaScript code will need to make an AJAX call to your Perl script to get the JSON data. You will then have to convert that to an array of arrays, which is what Google Charts expects. That is enough material for multiple Stack Overflow questions, so I'd recommend working one step at a time and if you get stuck, ask a narrower question. Commented Mar 19, 2014 at 22:20

2 Answers 2

0

I'm not sure what your problem is, but these ideas may help

JSON data is a language-independent restriction of JavaScript's objects and arrays.

JSON is a way of representing a data structure using a simple character string.

Because the ideas of hashes and arrays are common, JSON can be used to move information between different languages and different platforms.

It's not clear from your question what data you want to move, and where. But you can create a JSON string from Perl using the JSON module's encode_json method, which will give you a string that you can send anywhere by any means.

Correspondingly, you can use JSON.parse to build a JavaScript object that mirrors the original Perl data.

Sign up to request clarification or add additional context in comments.

Comments

0

Because don't know what you really want, here is an onliner:

perl -MJSON::XS -MText::CSV::Slurp -E 'say JSON::XS->new->utf8->encode(Text::CSV::Slurp->load(filehandle => *STDIN))' < file.csv

From this data:

Name,usedSpace,available_space,kbytes,quota_volume,capacity
Juno,889347800,1795006760,2684354560,1698693120,33%
Juno2,89347800,795006760,684354560,698693120,3%
Juno3,9347800,95006760,84354560,98693120,1%

creates this:

[{"usedSpace":"889347800","quota_volume":"1698693120","kbytes":"2684354560","available_space":"1795006760","capacity":"33%","Name":"Juno"},{"usedSpace":"89347800","quota_volume":"698693120","kbytes":"684354560","available_space":"795006760","capacity":"3%","Name":"Juno2"},{"usedSpace":"9347800","quota_volume":"98693120","kbytes":"84354560","available_space":"95006760","capacity":"1%","Name":"Juno3"}]

or more pretty, this:

perl -MJSON::XS -MText::CSV::Slurp -E 'say JSON::XS->new->utf8->pretty->encode(Text::CSV::Slurp->load(filehandle => *STDIN))' < file.csv

creates:

[
   {
      "usedSpace" : "889347800",
      "quota_volume" : "1698693120",
      "kbytes" : "2684354560",
      "available_space" : "1795006760",
      "capacity" : "33%",
      "Name" : "Juno"
   },
   {
      "usedSpace" : "89347800",
      "quota_volume" : "698693120",
      "kbytes" : "684354560",
      "available_space" : "795006760",
      "capacity" : "3%",
      "Name" : "Juno2"
   },
   {
      "usedSpace" : "9347800",
      "quota_volume" : "98693120",
      "kbytes" : "84354560",
      "available_space" : "95006760",
      "capacity" : "1%",
      "Name" : "Juno3"
   }
]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.