Diff for /loncom/xml/lonplot.pm between versions 1.87 and 1.95

version 1.87, 2003/08/28 20:38:25 version 1.95, 2004/03/08 17:31:37
Line 34 Line 34
 package Apache::lonplot;  package Apache::lonplot;
   
 use strict;  use strict;
   use warnings FATAL=>'all';
   no warnings 'uninitialized';
 use Apache::File;  use Apache::File;
 use Apache::response;  use Apache::response;
 use Apache::lonxml;  use Apache::lonxml;
Line 113  my $words_test     = sub {$_[0]=~s/\s+/ Line 115  my $words_test     = sub {$_[0]=~s/\s+/
 ###################################################################  ###################################################################
 my @gnuplot_edit_order =   my @gnuplot_edit_order = 
     qw/alttag bgcolor fgcolor height width font transparent grid samples       qw/alttag bgcolor fgcolor height width font transparent grid samples 
     border align texwidth plottype/;      border align texwidth texfont plottype/;
   
 my $gnuplot_help_text = <<"ENDPLOTHELP";  my $gnuplot_help_text = <<"ENDPLOTHELP";
 <p>  <p>
Line 230  my %gnuplot_defaults = Line 232  my %gnuplot_defaults =
          edit_type   => 'entry',           edit_type   => 'entry',
          size        => '5'           size        => '5'
          },           },
        texfont     => {
            default     => '22',
            test        => $int_test,
            description => 'Font size to use in TeX output (pts):',
            edit_type   => 'choice',
            choices     => [qw/10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
            },
      plottype  => {       plottype  => {
  default     => 'Cartesian',   default     => 'Cartesian',
  test        => sub {$_[0]=~/^(Polar|Cartesian)$/},   test        => sub {$_[0]=~/^(Polar|Cartesian)$/},
  description => 'Plot type:',   description => 'Plot type:',
  edit_type   => 'choice',   edit_type   => 'choice',
          choices     => ['Polar','Cartesian']           choices     => ['Cartesian','Polar']
          },           },
      );       );
   
Line 289  my %label_defaults = Line 298  my %label_defaults =
      }       }
      );       );
   
 my @tic_edit_order = ('location','mirror','start','increment','end');  my @tic_edit_order = ('location','mirror','start','increment','end',
                         'minorfreq');
 my %tic_defaults =  my %tic_defaults =
     (      (
      location => {       location => {
  default => 'border',    default => 'border', 
  test => sub {$_[0]=~/^(border|axis)$/},   test => sub {$_[0]=~/^(border|axis)$/},
  description => 'Location of tick marks',   description => 'Location of major tic marks',
  edit_type   => 'choice',   edit_type   => 'choice',
  choices     => ['border','axis']   choices     => ['border','axis']
  },   },
      mirror => {       mirror => {
  default => 'on',    default => 'on', 
  test => $onoff_test,   test => $onoff_test,
  description => 'mirror ticks on opposite axis?',   description => 'mirror tics on opposite axis?',
  edit_type   => 'onoff'   edit_type   => 'onoff'
  },   },
      start => {       start => {
  default => '-10.0',   default => '-10.0',
  test => $real_test,   test => $real_test,
  description => 'Start ticks at',   description => 'Start major tics at',
  edit_type   => 'entry',   edit_type   => 'entry',
  size        => '10'   size        => '10'
  },   },
      increment => {       increment => {
  default => '1.0',   default => '1.0',
  test => $real_test,   test => $real_test,
  description => 'Place a tick every',   description => 'Place a major tic every',
  edit_type   => 'entry',   edit_type   => 'entry',
  size        => '10'   size        => '10'
  },   },
      end => {       end => {
  default => ' 10.0',   default => ' 10.0',
  test => $real_test,   test => $real_test,
  description => 'Stop ticks at ',   description => 'Stop major tics at ',
  edit_type   => 'entry',   edit_type   => 'entry',
  size        => '10'   size        => '10'
  },   },
        minorfreq => {
    default => '0',
    test => $int_test,
    description => 'Number of minor tics between major tic marks',
    edit_type   => 'entry',
    size        => '10'
    },         
      );       );
   
 my @axis_edit_order = ('color','xmin','xmax','ymin','ymax');  my @axis_edit_order = ('color','xmin','xmax','ymin','ymax');
Line 384  required.  Unfortunately, you must make Line 401  required.  Unfortunately, you must make
 in the order gnuplot expects the data.  in the order gnuplot expects the data.
 </p><p>  </p><p>
 Specifying the data should usually be done with a perl variable or array,   Specifying the data should usually be done with a perl variable or array, 
 such as \@Xdata and \@Ydata.  You may also specify numerical data seperated   such as \@Xdata and \@Ydata.  You may also specify numerical data separated 
 by commas.  Again, the order of the <b>data</b> tags is important.  The  by commas.  Again, the order of the <b>data</b> tags is important.  The
 first tag will be the X data and the second will be the Y data.  first tag will be the X data and the second will be the Y data.
 </p>  </p>
Line 456  my %curve_defaults = Line 473  my %curve_defaults =
 my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);  my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
   
 sub start_gnuplot {  sub start_gnuplot {
     %plot    = ();      %key     = ();      %axis   = ();       undef(%plot);   undef(%key);    undef(%axis);
     $title   = undef;   $xlabel  = undef;   $ylabel = undef;      undef($title);  undef($xlabel); undef($ylabel);
     $#labels = -1;      $#curves = -1;      undef(@labels); undef(@curves);
     %xtics    = ();      %ytics    = ();      undef(%xtics);  undef(%ytics);
     #      #
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;      my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result='';      my $result='';
Line 495  sub end_gnuplot { Line 512  sub end_gnuplot {
     my $randnumber;      my $randnumber;
     # need to call rand everytime start_script would evaluate, as the      # need to call rand everytime start_script would evaluate, as the
     # safe space rand number generator and the global rand generator       # safe space rand number generator and the global rand generator 
     # are not seperate      # are not separate
     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||      if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  $target eq 'answer') {   $target eq 'answer') {
       $randnumber=int(rand(1000));        $randnumber=int(rand(1000));
Line 520  sub end_gnuplot { Line 537  sub end_gnuplot {
      alt    = "$plot{'alttag'}" />       alt    = "$plot{'alttag'}" />
 ENDIMAGE  ENDIMAGE
         } elsif ($target eq 'tex') {          } elsif ($target eq 'tex') {
     &Apache::lonnet::ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");      #might be inside the safe space, register the URL for later
       &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
     $result = '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$plot{'texwidth'}.' mm]{'.&Apache::lonnet::unescape($filename).'.eps}';      $result = '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$plot{'texwidth'}.' mm]{'.&Apache::lonnet::unescape($filename).'.eps}';
  }   }
     } elsif ($target eq 'edit') {      } elsif ($target eq 'edit') {
Line 642  sub start_title { Line 659  sub start_title {
     &Apache::edit::start_spanning_row().      &Apache::edit::start_spanning_row().
     &Apache::edit::editline('',$text,'',60);      &Apache::edit::editline('',$text,'',60);
     } elsif ($target eq 'modified') {      } elsif ($target eq 'modified') {
  my $text=$$parser[-1]->get_text("/title");  
  $result.=&Apache::edit::rebuild_tag($token);   $result.=&Apache::edit::rebuild_tag($token);
  $result.=&Apache::edit::modifiedfield($token);   $result.=&Apache::edit::modifiedfield("/title",$parser);
     }      }
     return $result;      return $result;
 }  }
Line 676  sub start_xlabel { Line 692  sub start_xlabel {
     &Apache::edit::start_spanning_row().      &Apache::edit::start_spanning_row().
     &Apache::edit::editline('',$text,'',60);      &Apache::edit::editline('',$text,'',60);
     } elsif ($target eq 'modified') {      } elsif ($target eq 'modified') {
  my $text=$$parser[-1]->get_text("/xlabel");  
  $result.=&Apache::edit::rebuild_tag($token);   $result.=&Apache::edit::rebuild_tag($token);
  $result.=&Apache::edit::modifiedfield($token);   $result.=&Apache::edit::modifiedfield("/xlabel",$parser);
     }      }
     return $result;      return $result;
 }  }
Line 711  sub start_ylabel { Line 726  sub start_ylabel {
     &Apache::edit::start_spanning_row().      &Apache::edit::start_spanning_row().
     &Apache::edit::editline('',$text,'',60);      &Apache::edit::editline('',$text,'',60);
     } elsif ($target eq 'modified') {      } elsif ($target eq 'modified') {
  my $text=$$parser[-1]->get_text("/ylabel");  
  $result.=&Apache::edit::rebuild_tag($token);   $result.=&Apache::edit::rebuild_tag($token);
  $result.=&Apache::edit::modifiedfield($token);   $result.=&Apache::edit::modifiedfield("/ylabel",$parser);
     }      }
     return $result;      return $result;
 }  }
Line 753  sub start_label { Line 767  sub start_label {
  &Apache::edit::get_new_args   &Apache::edit::get_new_args
     ($token,$parstack,$safeeval,keys(%label_defaults));      ($token,$parstack,$safeeval,keys(%label_defaults));
  $result.=&Apache::edit::rebuild_tag($token);   $result.=&Apache::edit::rebuild_tag($token);
  my $text=$$parser[-1]->get_text("/label");   $result.=&Apache::edit::modifiedfield("/label",$parser);
  $result.=&Apache::edit::modifiedfield($token);  
     }      }
     return $result;      return $result;
 }  }
Line 832  sub start_function { Line 845  sub start_function {
     &Apache::edit::editline('',$text,'',60);      &Apache::edit::editline('',$text,'',60);
     } elsif ($target eq 'modified') {      } elsif ($target eq 'modified') {
  $result.=&Apache::edit::rebuild_tag($token);   $result.=&Apache::edit::rebuild_tag($token);
  my $text=$$parser[-1]->get_text("/function");   $result.=&Apache::edit::modifiedfield("/function",$parser);
  $result.=&Apache::edit::modifiedfield($token);  
     }      }
     return $result;      return $result;
 }  }
Line 875  sub start_data { Line 887  sub start_data {
  my @data;   my @data;
  if ($datatext =~ /,/) { # comma deliminated   if ($datatext =~ /,/) { # comma deliminated
     @data = split /,/,$datatext;      @data = split /,/,$datatext;
  } else { # Assume it's space seperated.   } else { # Assume it's space separated.
     @data = split / /,$datatext;      @data = split / /,$datatext;
  }   }
  for (my $i=0;$i<=$#data;$i++) {   for (my $i=0;$i<=$#data;$i++) {
Line 910  sub start_data { Line 922  sub start_data {
     &Apache::edit::editline('',$text,'',60);      &Apache::edit::editline('',$text,'',60);
     } elsif ($target eq 'modified') {      } elsif ($target eq 'modified') {
  $result.=&Apache::edit::rebuild_tag($token);   $result.=&Apache::edit::rebuild_tag($token);
  my $text=$$parser[-1]->get_text("/data");   $result.=&Apache::edit::modifiedfield("/data",$parser);
  $result.=&Apache::edit::modifiedfield($token);  
     }      }
     return $result;      return $result;
 }  }
Line 977  sub set_defaults { Line 988  sub set_defaults {
 sub get_attributes{  sub get_attributes{
     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;      my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
     foreach my $attr (keys(%{$defaults})) {      foreach my $attr (keys(%{$defaults})) {
  if ($attr eq 'texwidth') {   if ($attr eq 'texwidth' || $attr eq 'texfont') {
     $values->{$attr} =       $values->{$attr} = 
  &Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);   &Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
  } else {   } else {
Line 1004  sub write_gnuplot_file { Line 1015  sub write_gnuplot_file {
     my ($tmpdir,$filename,$target)= @_;      my ($tmpdir,$filename,$target)= @_;
     my $gnuplot_input = '';      my $gnuplot_input = '';
     my $curve;      my $curve;
       my $pt = $plot{'texfont'};
     # Collect all the colors      # Collect all the colors
     my @Colors;      my @Colors;
     push @Colors, $plot{'bgcolor'};      push @Colors, $plot{'bgcolor'};
Line 1024  sub write_gnuplot_file { Line 1036  sub write_gnuplot_file {
  # set output   # set output
  $gnuplot_input .= "set output\n";   $gnuplot_input .= "set output\n";
     } elsif ($target eq 'tex') {      } elsif ($target eq 'tex') {
  $gnuplot_input .= "set term postscript eps monochrome solid\n";   $gnuplot_input .= "set term postscript eps monochrome solid \"Helvetica\" $pt \n";
  $gnuplot_input .= "set output \"/home/httpd/perl/tmp/".   $gnuplot_input .= "set output \"/home/httpd/perl/tmp/".
     &Apache::lonnet::unescape($filename).".eps\"\n";      &Apache::lonnet::unescape($filename).".eps\"\n";
     }      }
Line 1044  sub write_gnuplot_file { Line 1056  sub write_gnuplot_file {
     $gnuplot_input .= "set samples $plot{'samples'}\n";      $gnuplot_input .= "set samples $plot{'samples'}\n";
     # title, xlabel, ylabel      # title, xlabel, ylabel
     # titles      # titles
     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;      if ($target eq 'tex') {
     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));          $gnuplot_input .= "set title  \"$title\" font \"Helvetica,".$pt."pt\"\n"  if (defined($title)) ;
     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));          $gnuplot_input .= "set xlabel \"$xlabel\" font \"Helvetica,".$pt."pt\" \n" if (defined($xlabel));
           $gnuplot_input .= "set ylabel \"$ylabel\" font \"Helvetica,".$pt."pt\"\n" if (defined($ylabel));
       } else {
           $gnuplot_input .= "set title  \"$title\"  \n"  if (defined($title)) ;
           $gnuplot_input .= "set xlabel \"$xlabel\" \n" if (defined($xlabel));
           $gnuplot_input .= "set ylabel \"$ylabel\" \n" if (defined($ylabel));
       }
     # tics      # tics
     if (%xtics) {          if (%xtics) {    
  $gnuplot_input .= "set xtics $xtics{'location'} ";   $gnuplot_input .= "set xtics $xtics{'location'} ";
Line 1054  sub write_gnuplot_file { Line 1072  sub write_gnuplot_file {
  $gnuplot_input .= "$xtics{'start'}, ";   $gnuplot_input .= "$xtics{'start'}, ";
  $gnuplot_input .= "$xtics{'increment'}, ";   $gnuplot_input .= "$xtics{'increment'}, ";
  $gnuplot_input .= "$xtics{'end'}\n";   $gnuplot_input .= "$xtics{'end'}\n";
           if ($xtics{'minorfreq'} != 0) {
               $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
           } 
     }      }
     if (%ytics) {          if (%ytics) {    
  $gnuplot_input .= "set ytics $ytics{'location'} ";   $gnuplot_input .= "set ytics $ytics{'location'} ";
Line 1061  sub write_gnuplot_file { Line 1082  sub write_gnuplot_file {
  $gnuplot_input .= "$ytics{'start'}, ";   $gnuplot_input .= "$ytics{'start'}, ";
  $gnuplot_input .= "$ytics{'increment'}, ";   $gnuplot_input .= "$ytics{'increment'}, ";
         $gnuplot_input .= "$ytics{'end'}\n";          $gnuplot_input .= "$ytics{'end'}\n";
           if ($ytics{'minorfreq'} != 0) {
               $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
           } 
     }      }
     # axis      # axis
     if (%axis) {      if (%axis) {
Line 1081  sub write_gnuplot_file { Line 1105  sub write_gnuplot_file {
     my $label;      my $label;
     foreach $label (@labels) {      foreach $label (@labels) {
  $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.   $gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
     $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;      $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.' font "Helvetica,'.$pt.'pt"'.$/ ;
     }      }
     if ($target eq 'tex') {      if ($target eq 'tex') {
         $gnuplot_input .="set size 1,".$plot{'height'}/$plot{'width'}*1.38;          $gnuplot_input .="set size 1,".$plot{'height'}/$plot{'width'}*1.38;

Removed from v.1.87  
changed lines
  Added in v.1.95


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>