Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Integrating TK and Chart::Gnuplot.

by Ppeoc (Beadle)
on Mar 06, 2016 at 14:49 UTC ( #1156921=perlquestion: print w/replies, xml ) Need Help??

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

Hey Monks, Thank to the help of some monks below, I was able to scale the heatmaps using cbrange. My issue now is with using TK to make it interactive. I was able to use this post http://www.perlmonks.org/?node_id=685864 to a certain extent to get what I want. When I hit the plot button, I get the contour plot in a new window. The Plot button is still pressed. I can close the contour plot window, but the plot button in the TK window is still pressed and my perl interface starts to hang. I cannot even hit the exit button on the Tk window. How can I "unrelease" the plot button so that every time I click it, I get a new contour plot window (replacing the old one). Thanks monks!
my $chart = Chart::Gnuplot->new( terminal => 'windows', title => "3D plot from arrays of x, y and z coordinates", xlabel => 'x', ylabel => 'y', ); #my $x = 110; #my $y = 130; $chart->set(pm3d => 'map'); $chart->set(palette => 'defined (0 0 0 1, 1 1 1 0, 2 1 0 0)'); $chart->set(dgrid3d => '385,385'); $chart->set(cbrange => "[$x:$y]"); my $dataSet = Chart::Gnuplot::DataSet->new( points => \@array, ); my $mw = new MainWindow; my $frame = $mw->Frame->pack( -anchor => 'w' ); $frame->Button(-text => "Exit", -command => sub{&quit, $mw->destroy; })->pack(-side => "bo +ttom"); my $plotbutton = $frame->Button( -text => 'Plot', -command => \&plot, )->pack( -side => "bottom"); $mw->protocol( 'WM_DELETE_WINDOW', \&quit ); $mw->bind( "<Return>", sub { $plotbutton->invoke } ); MainLoop; sub plot { $chart->plot3d($dataSet); } sub quit { $chart->close; $mw->destroy; }

Replies are listed 'Best First'.
Re: Integrating TK and Chart::Gnuplot.
by Corion (Pope) on Mar 06, 2016 at 15:31 UTC

    From reading Chart::Gnuplot, I guess that the terminal => 'windows' option makes Gnuplot itself display a window. Your Tk program will halt until that Gnuplot window is closed. Either make Chart::Gnuplot output a PNG (or whatever) image and display that in Tk, or find a way to make Chart::Gnuplot return control to your main program immediately.

      Thanks Corion. Any idea how I can make Chart::Gnuplot return control to the main program?

        Looking at the source code of Chart::Gnuplot, I see that it calls the program as:

        my $err = `$cmd 2>&1`;

        So basically, it will always wait until $cmd finishes. You can try to reconfigure/replace $cmd by a command that launches Gnuplot in the background and then immediately returns. This would return control to your program, but will always launch a new instance of the gnuplot program for each diagram you want to display I imagine.

        I fear the only way to integrate things more into your program is to make gnuplot return image data and to render that yourself. But maybe by reading the documentation or the source code you find a way that I missed.

        My suggestion would be to go with the first option that Corion mentioned above: have gnuplot save its image to a file instead of opening its own display window. I gather you've had success already with using something like this:
        my $chart = Chart::Gnuplot->new( output => 'test.png', # not "terminal => 'windows'" ...
        That way, gnuplot will exit as soon as the file is written, and then you just have to load the file into a new Tk::Toplevel window. I'd be inclined to make the file name different on each "plot" call - e.g. rename "test.png" to some new unique name after calling plot3d, and use the new name to label and load the new Toplevel window, like this:
        my $output_id = 0; # put this before the MainLoop call ... sub plot { $chart->plot3d($dataSet); my $newname = sprintf( "test_%03d.png", ++$output_id ); rename( "test.png", $newname ); my $top = $mw->Toplevel( -title => $newname ); my $img = $top->Photo( -file => $newname ); my $lbl => $top->Label( -image => $img )->pack; }
        (updated to adjust file name in second paragraph)
Re: Integrating TK and Chart::Gnuplot.
by Anonymous Monk on Mar 07, 2016 at 01:45 UTC
Re: Integrating TK and Chart::Gnuplot.
by vkon (Curate) on Mar 06, 2016 at 18:02 UTC
    I would suggest to seek a solution on tcl/tk world - like this one - http://wiki.tcl.tk/5735 (but you could find another even better one) - and then utilise it using Tcl or Tcl::Tk cpan modules.

    This way you have more choices to select from.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1156921]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2021-01-24 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?