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

Hi Monks, I'm having more problems with the Tk::Graph module. The problem is, I need a menu to change the layout of my graph, eg from bar chart to pie chart, e.t.c I've managed to create the menu and when I choose the options the correct value seems to be getting stored in the variable because I'm printing it out everytime I run that function, but the graph doesn't change. It just remains a pie chart, when I choose bar chart, or any other type of graph. I've tried to tidy my code, but I'm quite new to this, so I appologise if the code is hard to read. The code is as follows:
sub reportg{ my( $userjobcount_ref, $userjobcpu_ref, $queuejobsystem_ref, $userjobt +ime_ref, $queue_cpu_summary_ref, $total_ref, $count) = @_; my( $total, $avg, $month_or_day,$user, $queue, $system_cpu); $MW = MainWindow->new; foreach $month_or_day (sort keys %{$queue_cpu_summary_ref}) { $total_user_job_count=0; foreach $queue (sort keys %{$queue_cpu_summary_ref->{$ +month_or_day}}) { $total += $$queue_cpu_summary_ref{$month_or_da +y}{$queue}; } } foreach $month_or_day (sort keys %{$queue_cpu_summary_ref}) { foreach $queue (sort keys %{$queue_cpu_summary_ref->{$ +month_or_day}}) { printf ("%-2s",$month_or_day); printf("%-10s","\t$queue"); $usage=sprintf ("%.3f", $$queue_cpu_su +mmary_ref{$month_or_day}{$queue} / $total * 100); print "\t$usage%"; print "\n"; $$data{$queue} = $usage; } } print $total; $ca = $MW->Graph( -type => $typ, -borderwidth => 2, -title => $field, -titlecolor => 'Brown', -yformat => '%2.2f', -ylabel => 'percentage', -xlabel => 'queue', -barwidth => 15, -padding => [20,20,20,100], # Padding [top, right, + buttom, left] -linewidth => 1, -shadow => 'gray50', -shadowdepth => 3, -maxmin => 1, -look => 50, -balloon => 1, -legend => 1, )->pack(-expand => 1, -fill => 'both'); $ca->configure(-variable => $data); # bind to data &menu($MW, $ca); MainLoop; } # Subs ---------------------------------- sub set3d { my $val = shift || 0; $ca->configure(-threed => $val); $ca->clear; } sub setdisplay { $typ = shift; $ca->clear; print "$typ\n"; $ca->configure(-type => $typ); } sub menu { my $top = shift || die; my $ca = shift || die; my $menuitems = [ [Cascade => "File", -menuitems => [ [Button => "Quit", -command => \&quitapp], ] ], [Cascade => "View", -menuitems => [ [Radiobutton => "~Bars", -variable => \$typ, -command +=> [\&setdisplay, 'Bars'] ], [Radiobutton => "~HBars", -variable => \$typ, -command +=> [\&setdisplay, 'Hbars']], [Radiobutton => "~Circle", -variable => \$typ, -command + => [\&setdisplay, 'Circle']], [Radiobutton => "~Line", -variable => \$typ, -command = +> [\&setdisplay, 'Line']], [Cascade => "~3d", -menuitems => [ [Button => "Off", -command => [\&set3d, 0] ], [Button => "3", -command => [\&set3d, 3] ], [Button => "5", -command => [\&set3d, 5] ], [Button => "7", -command => [\&set3d, 7] ], [Button => "10", -command => [\&set3d, 10] ], ] ], ] ], ]; if ($Tk::VERSION >= 800) { my $menubar = $top->Menu(-menuitems => $menuitems); $top->configure(-menu => $menubar); } else { $top->Menubutton(-text => "Pseudo menubar", -menuitems => $menuitems)->pack; } } sub quitapp { exit; }
Any help would be fantastic. I've hit a brick wall with it.

Replies are listed 'Best First'.
Re: Tk:: Graph menu not working to change the type of graph
by liverpole (Monsignor) on Sep 27, 2006 at 15:07 UTC
    Hi wishartz,

    A couple of suggestions:

    First, please include the entire program.  What you've supplied is apparently only subroutines; there is no use Tk, nor use Tk::Graph, and no program main from which any of the subroutines are called.

    Secondly, please provide a program which *works*.  When I make the assumption that you're trying to call reportg(), I get the following:

    #!/usr/bin/perl use Tk; use Tk::Graph; reportg(); # The rest of your code as you provided it... # # Output # Tk::Error: Can't set -variable to `undef' for Tk::Graph=HASH(0x84c9780 +): Tie::Watch::new(): -variable is required. at /usr/lib/perl5/site_p +erl/5.8.0/Tk/Graph.pm line 1088 at /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Tk/Derived. +pm line 294 Tk callback for . Tk::Derived::configure at /usr/lib/perl5/site_perl/5.8.0/i386-linux-t +hread-multi/Tk/Derived.pm line 306 main::reportg at x line 72 ERROR in Tk::Graph:Tk::Graph::set #947: Option '-type' is incorrect! ( +) at /usr/lib/perl5/site_perl/5.8.0/Tk/Graph.pm line 1802. Can't set -variable to `undef' for Tk::Graph=HASH(0x84c9780): Tie::Wat +ch::new(): -variable is required. at /usr/lib/perl5/site_perl/5.8.0/T +k/Graph.pm line 1088 at /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Tk/Derived. +pm line 294

    Thirdly, please put the following statements at or near the top of your code:

    use strict; use warnings;

    They will alert you to variables which have not been declared, which is a GOOD THING!

    For example, when I did that to your program, I needed to then define the following variables (under the section "Global data"):

    #!/usr/bin/perl # Use Strict! Use Warnings! #use strict; #use warnings; # Libraries use Tk; use Tk::Graph; # Global data #my $data; #my $MW; #my $total_user_job_count; #my $usage; #my $ca; #my $typ; #my $field; # Main program reportg();

    Once you provide a working example which anyone else can run, to get the errors you describe, it will be much easier to identify your problem!


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Unfortunatley, I can't really supply the entire program because you will need the data files to run it, which I'm not allowed to give out. Sorry about that, I know it makes it harder to find the problem. I have put in the statements use Tk; and use Tk::Graph at the start of the program and the program does display a graph with the correct values, so I know that part of the program works ok. It's just when I tried changing the type of the graph by using menus, it just doesn't change. It displays a pie chart to start off with, with the correct values, but when I chose a selection on the menu the graph doesn't change and I can see the error that i mentioned before in the console. I know the variable for the type of graph is correct because I'm printing it out when that function is called.
        We don't WANT to see the propritary stuff! WHat we need you do is make us up a compact test case that shows what doesn't work, then we can run it and figure out how to help you.

        In this case what you need to do is give us some dummy data built-into the code.

        jdtoronto

Re: Tk:: Graph menu not working to change the type of graph
by hgolden (Pilgrim) on Sep 27, 2006 at 14:59 UTC
    Hey,

    So I can't run your program because I'm missing the data, but the first thing to try with things like this is calling $MW->update; after you change something. Sometimes that's all it takes.

    Hays

    Update: Why don't you try:

    $ca->clear(); $setdisplay(whatever); $ca->redraw(); $mw->update;
    I don't know if that will work any better, but it does use Tk::Graph's methods, which you might need to do. Maybe you can't change the type while the graph is drawn...

    Also, the below posts are right, you should post your complete code, or at least a working version, so we can help you better.

      I tried adding that in the function setdisplay, underneath $ca->configure(-type => $typ); but it didnt' work. The console behind my graph, prints the message
      ERROR in Tk::Graph:Tk::Graph::set #947: Option '-type' is incorrect! ( +) at /home/others/it/itwi/perl/lib/perl5/site_perl/5.6.1/Tk/Graph.pm +line 1802, <IN> line 9318. Bars
      The last line which says'Bars' is the contents of the $typ variable being passed to the setdisplay function. Which whould be correct, I don't know why it is reporting -type is incorrect?
Re: Tk:: Graph menu not working to change the type of graph
by rcseege (Pilgrim) on Sep 27, 2006 at 15:26 UTC
    Here is one way, using the example that comes with the module:
    use Tk; use Tk::Graph; my $mw = MainWindow->new; my $data = { Sleep => 51, Work => 135, Access => 124, mySQL => 5 }; my $ca = $mw->Graph( -title => 'Example', -type => 'BARS' )->pack(qw/-expand 1 -fill both/); $ca->set($data); $mw->Button(-text => 'Change', -command => sub { $ca->configure(-type => 'Circle'); $ca->set($data); })->pack; MainLoop;

    Update: The suggestion to use redraw was a good one -- that should work, but there is a bug within Graph.pm. The redraw method calls set with no value - the implication is that if no data is provided then the existing data should be used, however that is not what happens, and ultimately results in the display not getting redrawn. The workaround I provided will work. I'll report the bug and send the patch to the Author.

    Within the set method, there is the following line:

       my $data = shift;.

    If you change this to:

       my $data = shift || $self->{data};

    Then redraw will work as you'd expect, and would support the following version of your setdisplay sub.

    sub setdisplay { $typ = shift; $ca->configure(-type => $typ); $ca->redraw; }
    Rob
      I managed to get it fixed by putting
      $ca->configure(-variable => $data);
      in my subdisplay function, below the line
      $ca->configure(-type => $typ);
      and it seems to work. Thanks for everybody's help.

        Yep, under the hood that will also call the set method. The advantage of using variable is that if you update your data, then the changes should be reflected automatically within your Graph.

        Rob
Re: Tk:: Graph menu not working to change the type of graph
by jdtoronto (Prior) on Sep 27, 2006 at 15:03 UTC
    Usually whever you make a change to something the next step should be to cal$MW->update which will cause the MainWindow to redraw itself. This might be all that is needed. If this doesn't work, please give us code that we can run and test - then we can look into things in more detail.

    jdtoronto