Cool. You might look at Tk::WorldCanvas. It does some scaling (and looks like scrolling work for you). I also changed it to use an actual line tool instead of points, and smooth the line. Makes for some nice graphs.

#!/usr/bin/perl -w use strict; use constant pi => 3.14159265358979; use Tk; use Tk::WorldCanvas; my $top = new MainWindow( -width => 800, -height => 600 ); my $cvs = $top->WorldCanvas( -width => 800, -height => 550 ); $cvs->center( 0, 0 ); my $frm = $top->Canvas( -width => 800, -height => 20 ); my $txt = $frm->Entry( -width => 40 ); my $min = $frm->Entry( -width => 5 ); my $max = $frm->Entry( -width => 5 ); my $stp = $frm->Entry( -width => 5 ); my $clr = $frm->Button( -text => 'Clear' ); $clr->bind( '<Button-1>' => [ sub { $cvs->delete($_) for $cvs->find('all') } ] + ); $txt->bind( '<KeyPress-Return>', [ \&evaluate ] ); $frm->pack( -side => 'bottom' ); $min->pack( -side => 'left' ); $max->pack( -side => 'left' ); $stp->pack( -side => 'left' ); $clr->pack( -side => 'right' ); $txt->pack( -side => 'right' ); $cvs->pack( -side => 'top' ); $min->insert( 0, "0" ); $max->insert( 0, "100" ); $stp->insert( 0, "1" ); MainLoop; sub evaluate { my $text = $txt->get(); my $func = lc $text; $func =~ s/(\d+)(pi|x)/$1 * $2/g; $func =~ s/([xy])/\$$1/g; $func =~ s/\^/**/g; my $mini = $min->get(); my $maxi = $max->get(); my $step = $stp->get(); #my $size = 1; my @points = (); for ( my $i = $mini ; $i <= $maxi ; $i += $step ) { my $y = 0; my $x = $i; eval "$func"; # warn "$func: $x, $y\n" if !($x % 50); push @points, $x, $y; # $cvs->createRectangle( # 800 * $x/$maxx, 300 - $y, # 800 * $x/$maxx + $size, 300 - $y + $size, # -width => 1); } $cvs->createLine( @points, -smooth => 1 ); $cvs->viewFit('all'); } __END__


___________
Eric Hodges

In reply to Re: Cheap'n'cheerful Graph Drawer by eric256
in thread Cheap'n'cheerful Graph Drawer by moot

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.