Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi. I'm building a data aquisition system in Perl that colects data at the rate of 10 cicles per second, and I'd like to show that in a graph on a Tk window.

If I use Tk::Canvas and create boxes or dots for each aquired data point, soon I'll fill all the memory because, if I understood correctly, each of those points is an entity on Tk.

Using Imager with Tk::photo was tried too, but the convertion from Imager format to PNG, as stated on Tk::Photo example, using base64 takes too long.

GD was also tried, but we need more functionality for the generated graphics.

In fact, what I'm looking for is a suggestion of a way to draw primitives on a canvas that are not items or objects, as was done in the past on VGA screens. The Tk::Photo + Imager solution would be fine if the conversion did not take so long.

Can you suggest me a way of solving this problem?


The following piece of code examples the problem. In a athlon64 notebook it takes 70% of the CPU to run.
#!/usr/bin/perl use strict; use Tk; use Tk::Photo; use MIME::Base64; use Tk::PNG; use Imager; my $xsize = 200; my $ysize = 200; my $cicle; my ($x, $y) = (int($xsize/2), int($ysize/2)); my $image = Imager->new(xsize=>$xsize, ysize=>$ysize); my $blue = Imager::Color->new( 255, 255, 255 ); my $image_data; $image->write(data =>\$image_data, type=>'png') or die "Cannot save image: ", $image->errstr; $image_data = encode_base64($image_data); my $main = MainWindow->new; my $tk_image = $main->Photo(-data => $image_data); my $screen = $main->Label(-image=>$tk_image)->pack; my $id = $main->repeat(100,\&move); MainLoop; sub move { my ($dx, $dy); my $i = int(rand(100)); if (($i/5) == int($i/5)) { $dx = 0; } elsif (($i/2) == int($i/2)) { $dx = 1; } else { $dx = -1; } my $j = int(rand(100)); if (($j/5) == int($j/5)) { $dy = 0; } elsif (($j/2) == int($j/2)) { $dy = 1; } else { $dy = -1; } if (($x + $dx) > $xsize) { $x = $xsize; } elsif (($x + $dx) < 0) { $x = 0; } else { $x += $dx; } if (($y + $dy) > $ysize) { $y = $ysize; } elsif (($y + $dy) < 0) { $y = 0; } else { $y += $dy; } $image->setpixel(x=>$x, y=>$y, color=>$blue); $cicle++; print "Cicle $cicle. Coords: $x, $y. Diference $dx, $dy.\n"; $image->write(data =>\$image_data, type=>'png') or die "Cannot save image: ", $image->errstr; $image_data = encode_base64($image_data); $tk_image->configure(-data => $image_data); $screen->configure(-image=>$tk_image); }

In reply to Real time data graphs. by enhering

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (10)
    As of 2014-07-25 02:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (167 votes), past polls