in reply to Re: I need to create a graph from a hash using GD else GD::Graph
in thread I need to create a graph from a hash using GD else GD::Graph

I tried using XML modules but the parsed output gives only iostat as the tag and rest as the data which I need to remove. The original iostat data parsed to XML is in the form

<iostat> <data avgqu="0" avgrq="0" await="0" device="sdb" elapsed_time="0" r= +"0" rmb="0" rrqm="0" svctm="0" util="0" w="0" wmb="0" wrqm="0" /> <data avgqu="0" avgrq="0" await="0" device="sda" elapsed_time="0" r= +"0" rmb="0" rrqm="0" svctm="0" util="0" w="0" wmb="0" wrqm="0" /> .........................(truncated) </iostat>
Here I need to remove data and other special characters to get the data. From this data I need to draw a graph. For Drawing the Graph I need to use GD::Graph module which requires to use array of arrays. I am able to build a hash from the XML file which I have provided earlier but I am not able to convert it to array of arrays. Is it also possible to covert the hash to Excel, if yes how do I do that?

  • Comment on Re^2: I need to create a graph from a hash using GD else GD::Graph
  • Download Code

Replies are listed 'Best First'.
Re^3: I need to create a graph from a hash using GD else GD::Graph
by poj (Abbot) on Sep 11, 2013 at 09:20 UTC

    The Test::Parser::Iostat has a data method which provides a hash, no need to parse the XML. This example plots a histogram of util against elapsed time for sda2 from a file created with iostat sda2 -x 1 10 > iostat.txt.

    #!/usr/bin/perl use strict; use Test::Parser::Iostat; use GD::Graph::bars; my $parser = new Test::Parser::Iostat(); open IN,'iostat.txt' or die "$!"; $parser->parse(\*IN) or die $parser->error(), "\n"; my $hash = $parser->data(); # create plot data my @plot=([],[]); for my $ar (@{$hash->{'iostat'}{'data'}}){ if ($ar->{'device'} eq 'sda2'){ push @plot[0],$ar->{'elapsed_time'}; push @plot[1],$ar->{'util'}; } } # create graph my $graph = GD::Graph::bars->new(400, 300); $graph->set( x_label => 'Elapsed time', y_label => 'Util', title => 'sda2 util', y_max_value => 100, ) or die $graph->error; my $gd = $graph->plot(\@plot) or die $graph->error; open(IMG, '>iostat.gif') or die $!; binmode IMG; print IMG $gd->gif;
    poj
Re^3: I need to create a graph from a hash using GD else GD::Graph
by Laurent_R (Canon) on Sep 11, 2013 at 06:36 UTC

    The hash that you extracted really contains only one linear array, which is probably not suitable for what you want to do. On the other hand, I provided you with a means to get an array of arrays, which is what you asked for in the first place, did you look at the AoA I obtained? Is it suitable for your purpose? As for GD::xxx, I do not know these modules nand can't help further.