eyncydious has asked for the wisdom of the Perl Monks concerning the following question:

Here's the deal: I have 22 routers all updating a data repository. I would like to chart a variable number (user selected) of those routers' data. Typical plotting would be something like: From 1200 to 1900, show me all the counters for OID 1.1.1.1.1.1 for interface Fastethernet1/1.

I have the core of that program built but I'm having major issues plotting multiple lines to the same graph from a loop. Here's a terrible example of what I'm trying to gather.

use strict; use Chart::Gnuplot; # Date array my @x = qw( 2007-01-01 2007-01-10 2007-01-24 2007-02-01 2007-02-14 ); my @b = ([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]); my @p; my @n; # Create the chart object my $chart = Chart::Gnuplot->new( output => 'datetime_1.png', xlabel => + 'Date axis', timeaxis => "x", ); # Data set object for my $i ( 1 .. 1) { @n = (); for my $j ( 0 .. $#{$b[$i]}) { print "b[$i][$j] is $b[$i][$j]\n"; push(@n,$b[$i][$j]); } my $plot = Chart::Gnuplot::DataSet->new( xdata => \@x, ydata => \@n, style => 'linespoints', timefmt => '%Y-%m-%d', ); $chart->plot2d($plot); }

The example is full of hackery and not very well built, I fully admit that. I think the issue is the chart object is receiving a new bout of state on each iteration of the loop and isn't incrementing in step with the loop. Short of manually scripting out:

$chart->plot2d($val1) if $val == 1; $chart->plot2d($val1,$val2) if $val == 2; $chart->plot2d($val1,$val2,$val3) if $val == 3; etc...

I'm not sure this is even possible. Appreciate any thoughts or suggestions.

Replies are listed 'Best First'.
Re: Populate 2d Chart Using Chart::Gnuplot from a Loop
by kcott (Archbishop) on May 13, 2013 at 04:51 UTC

    G'day eyncydious,

    Welcome to the monastery.

    Short of manually scripting out:

    $chart->plot2d($val1) if $val == 1; $chart->plot2d($val1,$val2) if $val == 2; $chart->plot2d($val1,$val2,$val3) if $val == 3; etc...

    To achieve this, I'd write something like:

    my @datasets; for my $i (...) { .... push @datasets, Chart::Gnuplot::DataSet->new(...); $chart->plot2d(@datasets); }

    -- Ken

      Worked like a charm for my exact same problem. Only negative is all the plots are the same color. Short of cycling through hex values based on the for loop counter, any idea how to alternate colors of the different dataset points?
        Did you get how to give different colors for data-sets in same plot? It would be grateful if you have an answer. Thanks
      Do you know how to give different colors for data-sets in same plot? It would be grateful if you have an answer. Thanks. Sorry to ask to two people at a time. Something urgent has to be done. Thanks Goutham

        Not being a user of this module, the short answer is no.

        What have you tried?

        The Dataset Option color would seem like a good place to start your research.

        -- Ken