Opps, I forgot to include the code :-)

#!/usr/local/bin/perl -w
use strict;
use warnings;
use Win32::GUI;
use Win32::GUI::Resizer;


my $wWinPlot = 400;
my $hWinPlot = 250;
my $xBar;


#### Fill out the array.  blank  means no data received
my $samplesToChart = 100; # How many samples to chart
my @graphData;
for (my $i = 0; $i < $samplesToChart - 1; $i++) {
    $graphData[$i] = "";
}
my $winPlot = winCreate();


my $objResizer = Win32::GUI::Resizer->new($winPlot);
$objResizer->raRelations([
                          'winWidth' => [
                                         ['$winResize->grCanvas->Width()'],
                                     ],
                          'winHeight' => [
        
['$winResize->grCanvas->Height()'],
                                          ],
                          ]);

$objResizer->memorize();


#### Testing; start off with a random value

push @graphData, rand 2;

sub winPlot_Resize {
    defined($objResizer) and $objResizer->resize();
}

sub winCreate {
         my $winPlot = Win32::GUI::Window->new(
               -left   => 100,
               -top    => 100,
               -width  => $wWinPlot + 10, # Added 10 pixs so the graph would
show
               -height => $hWinPlot + 45, # Added 45 pixs so the graph would
show
               -name   => "winPlot",
               -text   => "Plot test",
               -minheight        => 10,
               -minwidth         => 10,
               );

         my $grCanvas = Win32::GUI::Graphic->new($winPlot,
                                                 -left   => 0,
                                                 -top    => 0,
                                                 -width  => $wWinPlot,
                                                 -height => $hWinPlot,
                                                 -name   => "grCanvas",
                                                 -interactive => 0,
                                                 );
         my $tim = $winPlot->AddTimer("timTimer", 1000);

         $winPlot->Show();

         return($winPlot);
}

#### Will be called when the window needs to be painted,
#### caused either by Windows, or the InvalidateRect of the entire window
sub grCanvas_Paint {
    my $win = $winPlot;
    my($dcDev) = @_;
    my $ave;
    my $sampleCount = 0;
    my $firstSample = 0;
    my $maxSample = 0;

    return(0) if(!$dcDev);

    $dcDev->TextColor([255, 0, 0]);           #Red

# Adjust the width scale to fix the full width of the chart, regardless of
the number of samples
    my $step = $winPlot->grCanvas->Width / $samplesToChart;

# Get the Maximum value
    for (my $i = 0; $i < $samplesToChart; $i++) {
        next if $graphData[$i] eq "";
        $maxSample = $graphData[$i] if $graphData[$i] > $maxSample;
    }

    my $chartHeight = $winPlot->grCanvas->Height;
    my $hStep = $chartHeight / $maxSample; # Adjust the height scale to fit
the max value in the chart

     for (my $i = 0; $i < $samplesToChart; $i++) {
         next if $graphData[$i] eq "";
        if (!$firstSample) {
            # The first valid sample.  Start the line
            my $x = $chartHeight - $graphData[$i];
            $dcDev->MoveTo($i * $step, $chartHeight - ($graphData[$i] *
$hStep));
            $firstSample = 1;
        } else {
            # Continue the line
            $dcDev->LineTo($i * $step, $chartHeight - ($graphData[$i] *
$hStep));
        }
         $ave += $graphData[$i];
         $sampleCount++;
    }
    $dcDev->TextOut ($winPlot->grCanvas->Width - 100, 10, "Current: " .
sprintf("%.2f", $graphData[$samplesToChart - 1]));
    $dcDev->TextOut (10, 10, "Max: " . sprintf("%.2f", $maxSample));
    $ave = $ave / $sampleCount if $sampleCount > 0;
    if ($ave) {
# Draw the average line and bar
        $dcDev->MoveTo(0, $chartHeight - ($ave * $hStep)); # Start of the
average line
        $dcDev->LineTo($winPlot->grCanvas->Width, $chartHeight - ($ave *
$hStep)); # End of the average line
        $dcDev->Rectangle(50, $chartHeight, 75, $chartHeight - ($ave *
$hStep)); # The average bar
        $dcDev->TextOut(20, $chartHeight - 20, "Ave: " . sprintf("%.2f",
$ave)); #The average text
    }
    #### Tell Windows we're satisfied with the way the area looks like
    $dcDev->Validate();
    
    return(1);
}

sub timTimer_Timer {
    my $win = $winPlot;

    $xBar = rand 2; # Create a random value


    push @graphData, $xBar; # Add the new value
    shift @graphData; # Remove the first value

    #### Tell Windows the window needs to be repainted,
    #### will call the _Paint event handler
    #### The 1 wipes the area before repainting it,
    #### but since we may know exactly what needs to be repainted
    #### when $xBar changed, that may not be necessary
    $win->InvalidateRect(1);

    return(1);
}

Win32::GUI::Dialog();


Brian Millham
This message traveled at least 44,000 miles to reach you!
Creator of the DW6000 Monitor
http://www.millham.net/dw6000
[EMAIL PROTECTED] 

-----Original Message-----
From: Brian Millham [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, January 18, 2006 3:42 PM
To: 'Brian Millham'; 'Johan Lindstrvm';
perl-win32-gui-users@lists.sourceforge.net
Subject: RE: [perl-win32-gui-users] Win32::GUI and a "rolling line graph"

Here is the code (based on Johan's sample) that creates a rolling line
graph.  The data being charted is saved in @graphData, and the graph data is
automatically adjusted to fit in the chart.  I also added
Win32::GUI::Resizer to handle resizing the window.

I do have another question:
How do you change the color and the style of the line.  It appears that you
have to use a Win32::GUI::Brush object, but I can't figure out how to tie it
to the Graphic object.

Brian Millham
This message traveled at least 44,000 miles to reach you!
Creator of the DW6000 Monitor
http://www.millham.net/dw6000
[EMAIL PROTECTED] 

-----Original Message-----
From: Brian Millham [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, January 17, 2006 6:33 PM
To: 'Johan Lindstrvm'; 'perl-win32-gui-users@lists.sourceforge.net'
Subject: RE: [perl-win32-gui-users] Win32::GUI and a "rolling line graph"

Great!  This was exactly what I needed!  I now have a very simple rolling
line graph working based on this code.  I'll send an example to the list in
a bit (there are a few glitches that I need to work out)

Brian Millham
This message traveled at least 44,000 miles to reach you!
Creator of the DW6000 Monitor
http://www.millham.net/dw6000
[EMAIL PROTECTED] 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Johan
Lindstrvm
Sent: Tuesday, January 17, 2006 4:37 PM
To: perl-win32-gui-users@lists.sourceforge.net
Subject: Re: [perl-win32-gui-users] Win32::GUI and a "rolling line graph"

At 21:40 2006-01-17, Brian Millham wrote:
>         The VB portion shows a "Rolling Line Graph" of data.  Is there an
>easy way to do this in Perl and Win32::GUI?  I'm currently using
MSCHART.OCX
>to make the graph, but I'd like something that I have more control over.

You can paint a graph yourself on a Graphic control.

I posted this a few years back as an example of how to paint stuff on a 
Graphic:

----------


This is one way. Consider it a proof-of-concept rather than best-practice 
given the load of globals.

Basically, the _Paint event handler is used to always plot first the mouse 
lines and then the actual graph. This happens when the the Graphic control 
is invalidated, which can be triggered by three things; either Windows 
itself notices the need for it, or the graph changes, or the mouse moves.

(This method decouples the cause of the event from the action to be 
performed when it happens, which is a Good Thing)


The #### comments explain the important concepts.


#!/usr/local/bin/perl -w
use strict;
use Win32::GUI;



my $wWinPlot = 400;
my $hWinPlot = 250;
my ($xMouse, $yMouse) = (100, 100);
my $xBar = 100;
my $winPlot = winCreate();


sub winCreate {
         my $winPlot = Win32::GUI::Window->new(
               -left   => 100,
               -top    => 100,
               -width  => $wWinPlot,
               -height => $hWinPlot,
               -name   => "winPlot",
               -text   => "Plot test",
               -minheight        => 10,
               -minwidth         => 10,
               );

         my $grCanvas = Win32::GUI::Graphic->new($winPlot,
                         -left   => 0,
                         -top    => 0,
                 -width  => $wWinPlot,
                 -height => $hWinPlot,
                         -name   => "grCanvas",
                         -interactive => 1,
                         );
         my $tim = $winPlot->AddTimer("timTimer", 100);

         $winPlot->Show();

         return($winPlot);
}





#### Will be called when the window needs to be painted,
#### caused either by Windows, or the InvalidateRect of the entire window
sub grCanvas_Paint { my $win = $winPlot;
         my($dcDev) = @_;
         return(0) if(!$dcDev);

         $dcDev->TextColor([0, 0, 0]);           #Black

         #The cross-hair
         $dcDev->Rectangle($yMouse, 0, $yMouse + 1, $hWinPlot);
         $dcDev->Rectangle(0, $xMouse, $wWinPlot, $xMouse + 1);

         #The bar
         $dcDev->Rectangle(50, 0, 120, $xBar);

         #### Tell Windows we're satisfied with the way the area looks like
         $dcDev->Validate();

         return(1);
}



#### Trigger on the mouse move
sub grCanvas_MouseMove { my $win = $winPlot;
         my ($dummy, @aPos) = @_;

         ($yMouse, $xMouse) = @aPos;

         #### Tell Windows the window needs to be repainted,
         #### will call the _Paint event handler
         #### The 1 wipes the area before repainting it,
         #### changing it to 0 will leave trails
         $win->InvalidateRect(1);

         return(1);
}



my $timerCount = 0;
sub timTimer_Timer { my $win = $winPlot;
         $timerCount += .1;

         $xBar = (int(cos($timerCount) * 50)) + 70;

         #### Tell Windows the window needs to be repainted,
         #### will call the _Paint event handler
         #### The 1 wipes the area before repainting it,
         #### but since we may know exactly what needs to be repainted
         #### when $xBar changed, that may not be necessary
         $win->InvalidateRect(1);

         return(1);
}



Win32::GUI::Dialog();



__END__


/J



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/
---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0603-2, 01/17/2006
Tested on: 1/17/2006 6:32:45 PM
avast! is copyright (c) 2000-2003 ALWIL Software.
http://www.avast.com



---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0603-3, 01/18/2006
Tested on: 1/18/2006 3:42:16 PM
avast! is copyright (c) 2000-2003 ALWIL Software.
http://www.avast.com
---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0603-3, 01/18/2006
Tested on: 1/18/2006 3:55:20 PM
avast! is copyright (c) 2000-2003 ALWIL Software.
http://www.avast.com





Reply via email to