Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Updating Gtk2::Ex::Graph::GD

by deadpickle (Pilgrim)
on Mar 05, 2009 at 21:56 UTC ( [id://748684]=perlquestion: print w/replies, xml ) Need Help??

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

I'm using the module (described above) to create a "scrolling" line graph, useful tool if your looking at real-time data.
#!/usr/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use Gtk2::Ex::Graph::GD; use GD::Graph::Data; my $met_data1; my $met_image1; my $met_graph1; my @utc; my @temp; my $utc = 00; my $temp = 25.4; my $pres = 1019.3; my $rh1 = 20.0; my $rh2 = 20.4; my $timer_graph = Glib::Timeout->add(5000, \&graph); &data_build; Gtk2->main; sub data_build { my $data_window = Gtk2::Window->new('toplevel'); $data_window->signal_connect(delete_event=> sub{Gtk2->main_quit}); $data_window->set_title('Sonde Data'); $data_window->set_position('center'); $data_window->set_default_size(350,350); my $data_table = Gtk2::Table->new(6,2,FALSE); my $utc_label = Gtk2::Label->new("UTC"); my $utc_entry = Gtk2::Entry->new(); $utc_entry->set_editable(0); my $temp_label = Gtk2::Label->new("Temperature"); my $temp_entry = Gtk2::Entry->new(); $temp_entry->set_editable(0); my $pres_label = Gtk2::Label->new("Pressure"); my $pres_entry = Gtk2::Entry->new(); $pres_entry->set_editable(0); my $rh1_label = Gtk2::Label->new("Relative Humidity(1)"); my $rh1_entry = Gtk2::Entry->new(); $rh1_entry->set_editable(0); my $rh2_label = Gtk2::Label->new("Relative Humidity(2)"); my $rh2_entry = Gtk2::Entry->new(); $rh2_entry->set_editable(0); #$met_data1 = GD::Graph::Data->new([ # [ 201002,201004,201006,201008,201010,201012], # [ 0,10,20,30,40,50], # [ 1000,975,950,925,920,910] #]) or die GD::Graph::Data->error; $met_data1 = GD::Graph::Data->new([ [ 0], [ 0], # [ 1100] ]) or die GD::Graph::Data->error; $met_graph1 = Gtk2::Ex::Graph::GD->new(350, 200, 'lines'); $met_graph1->set( x_label => 'UTC', y1_label => 'Celcius', y2_label => 'Millibars', y1_max_value => 50, y1_min_value => 0, y2_max_value => 1100, y2_min_value => 0, y1_tick_number => 14, y2_tick_number => 14, two_axes => 2, line_width => 3, transparent => 0, dclrs => [ qw(red blue) ], x_label_position => 1/2, ); $met_image1 = $met_graph1->get_image($met_data1); $data_table->attach_defaults($utc_label,0,1,0,1); $data_table->attach_defaults($utc_entry,1,2,0,1); $data_table->attach_defaults($temp_label,0,1,1,2); $data_table->attach_defaults($temp_entry,1,2,1,2); $data_table->attach_defaults($pres_label,0,1,2,3); $data_table->attach_defaults($pres_entry,1,2,2,3); $data_table->attach_defaults($rh1_label,0,1,3,4); $data_table->attach_defaults($rh1_entry,1,2,3,4); $data_table->attach_defaults($rh2_label,0,1,4,5); $data_table->attach_defaults($rh2_entry,1,2,4,5); $data_table->attach_defaults($met_image1,0,2,5,6); $data_window->add($data_table); $data_window->show_all; return 1; } sub graph{ my $utc_data; my $temp_data; my $count; $utc = $utc + 5; push(@utc,$utc); my $length = @utc; shift(@utc) if $length > 10; #foreach my $kid(@utc) { # $utc_data .= "$kid,"; #} #chop $utc_data; $temp = $temp - 0.05; push(@temp,$temp); $length = @temp; shift(@temp) if $length > 10; foreach my $kid(@temp) { print "temp value $kid\n"; } #chop $temp_data; my $num = $met_data1->num_points(); if ($met_data1->num_points() == 10) { for($count=0;$count<=$num-1;$count++) { print "$count $temp[$count]\n"; $met_data1->set_x($count,$utc[$count]); $met_data1->set_y(1,$count,$temp[$count]); } }else{ $met_data1->add_point($utc,$temp); } $num = $met_data1->num_points(); my @values = $met_data1->y_values(1); foreach my $kid(@values) { print "$num $kid\n"; } #$pres = $pres - 3.2; #$rh1 = $rh1 - 0.01; #$rh2 = $rh2 - 0.012; $met_image1 = $met_graph1->get_image($met_data1); return 1; }
So far it works good but when it goes to update the image all it does is overlay the new image top of the prior image, at least thats what I think it is doing. Any ideas?

Replies are listed 'Best First'.
Re: Updating Gtk2::Ex::Graph::GD
by Anonymous Monk on Mar 06, 2009 at 08:20 UTC
      Dont know if this will help any. According to Gtk2::Ex:Graph::GD the image is contained within an eventbox. Is there a way to destroy this eventbox or refresh it?
        Heres what I got so far. I used the code linked to me (I hope I got it right). I still cant get rid of the prior image.
        #!/usr/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use GD::Graph::Data; use GD::Graph::lines; my $met_data1; my $met_image1; my $met_graph1; my @utc; my @temp; my $pixbuf_orig; my $pixbuf; my $vp = Gtk2::Viewport->new(undef,undef); my $utc = 00; my $temp = 25.4; my $pres = 1019.3; my $rh1 = 20.0; my $rh2 = 20.4; my $timer_graph = Glib::Timeout->add(5000, \&graph); &data_build; Gtk2->main; sub data_build { my $data_window = Gtk2::Window->new('toplevel'); $data_window->signal_connect(delete_event=> sub{Gtk2->main_quit}); $data_window->set_title('Sonde Data'); $data_window->set_position('center'); $data_window->set_default_size(350,350); my $data_table = Gtk2::Table->new(6,2,FALSE); my $utc_label = Gtk2::Label->new("UTC"); my $utc_entry = Gtk2::Entry->new(); $utc_entry->set_editable(0); my $temp_label = Gtk2::Label->new("Temperature"); my $temp_entry = Gtk2::Entry->new(); $temp_entry->set_editable(0); my $pres_label = Gtk2::Label->new("Pressure"); my $pres_entry = Gtk2::Entry->new(); $pres_entry->set_editable(0); my $rh1_label = Gtk2::Label->new("Relative Humidity(1)"); my $rh1_entry = Gtk2::Entry->new(); $rh1_entry->set_editable(0); my $rh2_label = Gtk2::Label->new("Relative Humidity(2)"); my $rh2_entry = Gtk2::Entry->new(); $rh2_entry->set_editable(0); $met_graph1 = GD::Graph::lines->new(350, 200); $met_graph1->set( x_label => 'UTC', y1_label => 'Celcius', y2_label => 'Millibars', y1_max_value => 32, y1_min_value => 0, y2_max_value => 1100, y2_min_value => 0, y1_tick_number => 14, y2_tick_number => 14, two_axes => 2, line_width => 3, transparent => 0, dclrs => [ qw(red blue) ], x_label_position => 1/2, ); # The Data to Start with $met_data1 = GD::Graph::Data->new([ [ 0], [ 0], # [ 1100] ]) or die GD::Graph::Data->error; $met_graph1->plot($met_data1); my $loader = Gtk2::Gdk::PixbufLoader->new; $loader->write ($met_graph1->gd->png); $loader->close; $pixbuf = $loader->get_pixbuf; $pixbuf_orig = $pixbuf->copy(); $met_image1 = Gtk2::Image->new_from_pixbuf($pixbuf); $vp->add($met_image1); $data_table->attach_defaults($utc_label,0,1,0,1); $data_table->attach_defaults($utc_entry,1,2,0,1); $data_table->attach_defaults($temp_label,0,1,1,2); $data_table->attach_defaults($temp_entry,1,2,1,2); $data_table->attach_defaults($pres_label,0,1,2,3); $data_table->attach_defaults($pres_entry,1,2,2,3); $data_table->attach_defaults($rh1_label,0,1,3,4); $data_table->attach_defaults($rh1_entry,1,2,3,4); $data_table->attach_defaults($rh2_label,0,1,4,5); $data_table->attach_defaults($rh2_entry,1,2,4,5); $data_table->attach_defaults($vp,0,2,5,6); $data_window->add($data_table); $data_window->show_all; return 1; } sub graph{ my $utc_data; my $temp_data; my $count; $utc = $utc + 5; push(@utc,$utc); my $length = @utc; shift(@utc) if $length > 10; $temp = $temp - 0.05; push(@temp,$temp); $length = @temp; shift(@temp) if $length > 10; my $num = $met_data1->num_points(); if ($met_data1->num_points() == 10) { for($count=0;$count<=$num-1;$count++) { print "$count $temp[$count]\n"; $met_data1->set_x($count,$utc[$count]); $met_data1->set_y(1,$count,$temp[$count]); } }else{ $met_data1->add_point($utc,$temp); } $num = $met_data1->num_points(); my @values = $met_data1->y_values(1); foreach my $kid(@values) { print "$num $kid\n"; } #$pres = $pres - 3.2; #$rh1 = $rh1 - 0.01; #$rh2 = $rh2 - 0.012; $met_graph1->plot($met_data1); my $loader = Gtk2::Gdk::PixbufLoader->new; $loader->write ($met_graph1->gd->png); $loader->close; my $pixbuf = $loader->get_pixbuf; $pixbuf_orig = $pixbuf->copy(); $met_image1->set_from_pixbuf ($pixbuf); Gtk2->main_iteration while Gtk2->events_pending; $pixbuf = $pixbuf_orig->copy(); return 1; }

      Hi

      Today , I also was looking in to Gtk2::Ex::Graph::GD module and I do experienced graph update problem. After debugging I have solved it

      Trick is you need create new Gtk2::Ex::Graph::GD object every time when you want to update GUI in Gtk

      refer following code which works for me in ActivePerl 5.12 in windows 7

      use strict; use warnings; use threads; use threads::shared; use Thread::Queue; use Glib qw/TRUE FALSE/; use Gtk2 -init; use Data::Dumper; use Gtk2::Ex::Graph::GD; use GD::Graph; use GD::Graph::Data; my @xlegend = (1..10); $|=1; my $gui; my $table; sub on_window1_destroy { my $widget = shift @_; #my $userData = shift @_; Gtk2->main_quit; } sub updateGraph { print "\n called"; my @data1 = map(rand(100),(1..10)); my @graph_data = (\@xlegend,\@data1,); my $data = GD::Graph::Data->new(\@graph_data) or die GD::Graph::Data-> +error; my $graph = Gtk2::Ex::Graph::GD->new(500, 300, 'lines'); my $image = $graph->get_image($data); $table->attach_defaults($image,1,2,1,2); return TRUE; } #Main Glib::Object->set_threadsafe (TRUE); $gui = Gtk2::Builder->new(); $gui->add_from_file('chart.glade'); $gui->connect_signals(undef); $table=$gui->get_object('table1'); &updateGraph(); Glib::Timeout->add (1000,\&updateGraph); Gtk2->main();

      Note: Not a very good code. ONly shared for reference

      Enjoy :)
        Trick is you need create new Gtk2::Ex::Graph::GD object every time when you want to update GUI in Gtk

        That makes sense to me, since all Gtk2 is doing is displaying a graphic generated by GD. If you make changes to your graph data, and want the Gtk2 gui updated, you must have GD rebuild the new graph.

        There may be a more efficient method than creating a new Gtk2::Ex::Graph::GD object every time, like reusing the existing object, clearing out it's data, and making a new graph.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://748684]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found