File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.104: download - view: text, annotated - select for diffs
Thu Dec 2 15:38:11 2004 UTC (19 years, 5 months ago) by www
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_99_1, HEAD
Typo

    1: # The LearningOnline Network with CAPA
    2: # Dynamic plot
    3: #
    4: # $Id: lonplot.pm,v 1.104 2004/12/02 15:38:11 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonplot;
   30: 
   31: use strict;
   32: use warnings FATAL=>'all';
   33: no warnings 'uninitialized';
   34: use Apache::File;
   35: use Apache::response;
   36: use Apache::lonxml;
   37: use Apache::edit;
   38: 
   39: use vars qw/$weboutputformat $versionstring/;
   40: 
   41: BEGIN {
   42:     &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
   43:     #
   44:     # Determine the version of GNUPLOT
   45:     $weboutputformat = 'gif';
   46:     $versionstring = `gnuplot --version 2>/dev/null`;
   47:     if ($versionstring =~ /^gnuplot 4/) {
   48:         $weboutputformat = 'png';
   49:     }
   50: }
   51: 
   52: ## 
   53: ## Description of data structures:
   54: ##
   55: ##  %plot       %key    %axis
   56: ## --------------------------
   57: ##  height      title   color
   58: ##  width       box     xmin
   59: ##  bgcolor     pos     xmax
   60: ##  fgcolor             ymin
   61: ##  transparent         ymax
   62: ##  grid
   63: ##  border
   64: ##  font
   65: ##  align
   66: ##
   67: ##  @labels: $labels[$i] = \%label
   68: ##           %label: text, xpos, ypos, justify
   69: ##
   70: ##  @curves: $curves[$i] = \%curve
   71: ##           %curve: name, linestyle, ( function | data )
   72: ##
   73: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
   74: ##                            [y1,y2,y3,y4] ]
   75: ##
   76: 
   77: ###################################################################
   78: ##                                                               ##
   79: ##        Tests used in checking the validitity of input         ##
   80: ##                                                               ##
   81: ###################################################################
   82: 
   83: my $max_str_len = 50;    # if a label, title, xlabel, or ylabel text
   84:                          # is longer than this, it will be truncated.
   85: 
   86: my %linestyles = 
   87:     (
   88:      lines          => 2,     # Maybe this will be used in the future
   89:      linespoints    => 2,     # to check on whether or not they have 
   90:      dots	    => 2,     # supplied enough <data></data> fields
   91:      points         => 2,     # to use the given line style.  But for
   92:      steps	    => 2,     # now there are more important things 
   93:      fsteps	    => 2,     # for me to deal with.
   94:      histeps        => 2,
   95:      errorbars	    => 3,
   96:      xerrorbars	    => [3,4],
   97:      yerrorbars	    => [3,4],
   98:      xyerrorbars    => [4,6],
   99:      boxes          => 3,
  100:      vector	    => 4
  101:     );		    
  102: 
  103: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
  104: my $real_test      = 
  105:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
  106: my $pos_real_test  =
  107:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
  108: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-fA-F]{6}$/};
  109: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
  110: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
  111: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
  112: my $linestyle_test = sub {exists($linestyles{$_[0]})};
  113: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w~!\@\#\$\%^&\*\(\)-=_\+\[\]\{\}:\;\'<>,\.\/\?\\]+ ?)+$/};
  114: 
  115: ###################################################################
  116: ##                                                               ##
  117: ##                      Attribute metadata                       ##
  118: ##                                                               ##
  119: ###################################################################
  120: my @gnuplot_edit_order = 
  121:     qw/alttag bgcolor fgcolor height width font transparent grid samples 
  122:     border align texwidth texfont plottype lmargin rmargin tmargin bmargin 
  123:     major_ticscale minor_ticscale/;
  124: 
  125: my $margin_choices = ['default',
  126:                       qw{0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}];
  127: 
  128: my %gnuplot_defaults = 
  129:     (
  130:      alttag       => {
  131: 	 default     => 'dynamically generated plot',
  132: 	 test        => $words_test,
  133: 	 description => 'brief description of the plot',
  134:       	 edit_type   => 'entry',
  135: 	 size        => '40'
  136: 	 },
  137:      height       => {
  138: 	 default     => 300,
  139: 	 test        => $int_test,
  140: 	 description => 'height of image (pixels)',
  141:       	 edit_type   => 'entry',
  142: 	 size        => '10'
  143: 	 },
  144:      width        => {
  145: 	 default     => 400,
  146: 	 test        => $int_test,
  147: 	 description => 'width of image (pixels)',
  148: 	 edit_type   => 'entry',
  149: 	 size        => '10'
  150: 	 },
  151:      bgcolor      => {
  152: 	 default     => 'xffffff',
  153: 	 test        => $color_test, 
  154: 	 description => 'background color of image (xffffff)',
  155: 	 edit_type   => 'entry',
  156: 	 size        => '10'
  157: 	 },
  158:      fgcolor      => {
  159: 	 default     => 'x000000',
  160: 	 test        => $color_test,
  161: 	 description => 'foreground color of image (x000000)',
  162: 	 edit_type   => 'entry',
  163: 	 size        => '10'
  164: 	 },
  165:      transparent  => {
  166: 	 default     => 'off',
  167: 	 test        => $onoff_test, 
  168: 	 description => 'Transparent image',
  169: 	 edit_type   => 'onoff'
  170: 	 },
  171:      grid         => {
  172: 	 default     => 'on',
  173: 	 test        => $onoff_test, 
  174: 	 description => 'Display grid',
  175: 	 edit_type   => 'onoff'
  176: 	 },
  177:      border       => {
  178: 	 default     => 'on',
  179: 	 test        => $onoff_test, 
  180: 	 description => 'Draw border around plot',
  181: 	 edit_type   => 'onoff'
  182: 	 },
  183:      font         => {
  184: 	 default     => 'medium',
  185: 	 test        => $sml_test,
  186: 	 description => 'Size of font to use',
  187: 	 edit_type   => 'choice',
  188: 	 choices     => ['small','medium','large']
  189: 	 },
  190:      samples         => {
  191: 	 default     => '100',
  192: 	 test        => $int_test,
  193: 	 description => 'Number of samples for non-data plots',
  194: 	 edit_type   => 'choice',
  195: 	 choices     => ['100','200','500','1000','2000','5000']
  196: 	 },
  197:      align        => {
  198: 	 default     => 'center',
  199: 	 test        => sub {$_[0]=~/^(left|right|center)$/},
  200: 	 description => 'alignment for image in html',
  201: 	 edit_type   => 'choice',
  202: 	 choices     => ['left','right','center']
  203: 	 },
  204:      texwidth     => {
  205:          default     => '93',
  206:          test        => $int_test,
  207:          description => 'Width of plot when printed (mm)',
  208:          edit_type   => 'entry',
  209:          size        => '5'
  210:          },
  211:      texfont     => {
  212:          default     => '22',
  213:          test        => $int_test,
  214:          description => 'Font size to use in TeX output (pts):',
  215:          edit_type   => 'choice',
  216:          choices     => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
  217:          },
  218:      plottype  => {
  219: 	 default     => 'Cartesian',
  220: 	 test        => sub {$_[0]=~/^(Polar|Cartesian)$/},
  221: 	 description => 'Plot type:',
  222: 	 edit_type   => 'choice',
  223:          choices     => ['Cartesian','Polar']
  224:          },
  225:      lmargin   => {
  226: 	 default     => 'default',
  227: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  228: 	 description => 'Left margin width (pts):',
  229: 	 edit_type   => 'choice',
  230:          choices     => $margin_choices,
  231:          },
  232:      rmargin   => {
  233: 	 default     => 'default',
  234: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  235: 	 description => 'Right margin width (pts):',
  236: 	 edit_type   => 'choice',
  237:          choices     => $margin_choices,
  238:          },
  239:      tmargin   => {
  240: 	 default     => 'default',
  241: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  242: 	 description => 'Top margin width (pts):',
  243: 	 edit_type   => 'choice',
  244:          choices     => $margin_choices,
  245:          },
  246:      bmargin   => {
  247: 	 default     => 'default',
  248: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  249: 	 description => 'Bottom margin width (pts):',
  250: 	 edit_type   => 'choice',
  251:          choices     => $margin_choices,
  252:          },
  253:      major_ticscale  => {
  254:          default     => '1',
  255:          test        => $real_test,
  256:          description => 'Size of major tic marks (plot coordinates)',
  257:          edit_type   => 'entry',
  258:          size        => '5'
  259:          },
  260:      minor_ticscale  => {
  261:          default     => '0.5',
  262:          test        => $real_test,
  263:          description => 'Size of minor tic mark (plot coordinates)',
  264:          edit_type   => 'entry',
  265:          size        => '5'
  266:          },
  267:      );
  268: 
  269: my %key_defaults = 
  270:     (
  271:      title => { 
  272: 	 default => '',
  273: 	 test => $words_test,
  274: 	 description => 'Title of key',
  275: 	 edit_type   => 'entry',
  276: 	 size        => '40'
  277: 	 },
  278:      box   => { 
  279: 	 default => 'off',
  280: 	 test => $onoff_test,
  281: 	 description => 'Draw a box around the key?',
  282: 	 edit_type   => 'onoff'
  283: 	 },
  284:      pos   => { 
  285: 	 default => 'top right', 
  286: 	 test => $key_pos_test, 
  287: 	 description => 'position of the key on the plot',
  288: 	 edit_type   => 'choice',
  289: 	 choices     => ['top left','top right','bottom left','bottom right',
  290: 			 'outside','below']
  291: 	 }
  292:      );
  293: 
  294: my %label_defaults = 
  295:     (
  296:      xpos    => {
  297: 	 default => 0,
  298: 	 test => $real_test,
  299: 	 description => 'x position of label (graph coordinates)',
  300: 	 edit_type   => 'entry',
  301: 	 size        => '10'
  302: 	 },
  303:      ypos    => {
  304: 	 default => 0, 
  305: 	 test => $real_test,
  306: 	 description => 'y position of label (graph coordinates)',
  307: 	 edit_type   => 'entry',
  308: 	 size        => '10'
  309: 	 },
  310:      justify => {
  311: 	 default => 'left',    
  312: 	 test => sub {$_[0]=~/^(left|right|center)$/},
  313: 	 description => 'justification of the label text on the plot',
  314: 	 edit_type   => 'choice',
  315: 	 choices     => ['left','right','center']
  316:      }
  317:      );
  318: 
  319: my @tic_edit_order = ('location','mirror','start','increment','end',
  320:                       'minorfreq');
  321: my %tic_defaults =
  322:     (
  323:      location => {
  324: 	 default => 'border', 
  325: 	 test => sub {$_[0]=~/^(border|axis)$/},
  326: 	 description => 'Location of major tic marks',
  327: 	 edit_type   => 'choice',
  328: 	 choices     => ['border','axis']
  329: 	 },
  330:      mirror => {
  331: 	 default => 'on', 
  332: 	 test => $onoff_test,
  333: 	 description => 'mirror tics on opposite axis?',
  334: 	 edit_type   => 'onoff'
  335: 	 },
  336:      start => {
  337: 	 default => '-10.0',
  338: 	 test => $real_test,
  339: 	 description => 'Start major tics at',
  340: 	 edit_type   => 'entry',
  341: 	 size        => '10'
  342: 	 },
  343:      increment => {
  344: 	 default => '1.0',
  345: 	 test => $real_test,
  346: 	 description => 'Place a major tic every',
  347: 	 edit_type   => 'entry',
  348: 	 size        => '10'
  349: 	 },
  350:      end => {
  351: 	 default => ' 10.0',
  352: 	 test => $real_test,
  353: 	 description => 'Stop major tics at ',
  354: 	 edit_type   => 'entry',
  355: 	 size        => '10'
  356: 	 },
  357:      minorfreq => {
  358: 	 default => '0',
  359: 	 test => $int_test,
  360: 	 description => 'Number of minor tics per major tic mark',
  361: 	 edit_type   => 'entry',
  362: 	 size        => '10'
  363: 	 },         
  364:      );
  365: 
  366: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax');
  367: my %axis_defaults = 
  368:     (
  369:      color   => {
  370: 	 default => 'x000000', 
  371: 	 test => $color_test,
  372: 	 description => 'color of grid lines (x000000)',
  373: 	 edit_type   => 'entry',
  374: 	 size        => '10'
  375: 	 },
  376:      xmin      => {
  377: 	 default => '-10.0',
  378: 	 test => $real_test,
  379: 	 description => 'minimum x-value shown in plot',
  380: 	 edit_type   => 'entry',
  381: 	 size        => '10'
  382: 	 },
  383:      xmax      => {
  384: 	 default => ' 10.0',
  385: 	 test => $real_test,
  386: 	 description => 'maximum x-value shown in plot',	 
  387: 	 edit_type   => 'entry',
  388: 	 size        => '10'
  389: 	 },
  390:      ymin      => {
  391: 	 default => '-10.0',
  392: 	 test => $real_test,
  393: 	 description => 'minimum y-value shown in plot',	 
  394: 	 edit_type   => 'entry',
  395: 	 size        => '10'
  396: 	 },
  397:      ymax      => {
  398: 	 default => ' 10.0',
  399: 	 test => $real_test,
  400: 	 description => 'maximum y-value shown in plot',	 
  401: 	 edit_type   => 'entry',
  402: 	 size        => '10'
  403: 	 }
  404:      );
  405: 
  406: my @curve_edit_order = ('color','name','linestyle','pointtype','pointsize');
  407: 
  408: my %curve_defaults = 
  409:     (
  410:      color     => {
  411: 	 default => 'x000000',
  412: 	 test => $color_test,
  413: 	 description => 'color of curve (x000000)',
  414: 	 edit_type   => 'entry',
  415: 	 size        => '10'
  416: 	 },
  417:      name      => {
  418: 	 default => '',
  419: 	 test => $words_test,
  420: 	 description => 'name of curve to appear in key',
  421: 	 edit_type   => 'entry',
  422: 	 size        => '20'
  423: 	 },
  424:      linestyle => {
  425: 	 default => 'lines',
  426: 	 test => $linestyle_test,
  427: 	 description => 'Line style',
  428: 	 edit_type   => 'choice',
  429: 	 choices     => [keys(%linestyles)]
  430: 	 },
  431: # gnuplots term=gif driver does not handle linewidth :(
  432: #     linewidth => {
  433: #         default     => 1,
  434: #         test        => $int_test,
  435: #         description => 'Line width (may not apply to all line styles)',
  436: #         edit_type   => 'choice',
  437: #         choices     => [1,2,3,4,5,6,7,8,9,10]
  438: #         },
  439:      pointsize => {
  440:          default     => 1,
  441:          test        => $pos_real_test,
  442:          description => 'point size (may not apply to all line styles)',
  443:          edit_type   => 'entry',
  444:          size        => '5'
  445:          },
  446:      pointtype => {
  447:          default     => 1,
  448:          test        => $int_test,
  449:          description => 'point type (may not apply to all line styles)',
  450:          edit_type   => 'choice',
  451:          choices     => [0,1,2,3,4,5,6]
  452:          }
  453:      );
  454: 
  455: ###################################################################
  456: ##                                                               ##
  457: ##                    parsing and edit rendering                 ##
  458: ##                                                               ##
  459: ###################################################################
  460: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
  461: 
  462: sub start_gnuplot {
  463:     undef(%plot);   undef(%key);    undef(%axis);
  464:     undef($title);  undef($xlabel); undef($ylabel);
  465:     undef(@labels); undef(@curves);
  466:     undef(%xtics);  undef(%ytics);
  467:     #
  468:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  469:     my $result='';
  470:     &Apache::lonxml::register('Apache::lonplot',
  471: 	     ('title','xlabel','ylabel','key','axis','label','curve',
  472: 	      'xtics','ytics'));
  473:     push (@Apache::lonxml::namespace,'lonplot');
  474:     if ($target eq 'web' || $target eq 'tex') {
  475: 	&get_attributes(\%plot,\%gnuplot_defaults,$parstack,$safeeval,
  476: 			$tagstack->[-1]);
  477:     } elsif ($target eq 'edit') {
  478: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
  479: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
  480: 				    \@gnuplot_edit_order);
  481:     } elsif ($target eq 'modified') {
  482: 	my $constructtag=&Apache::edit::get_new_args
  483: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
  484: 	if ($constructtag) {
  485: 	    $result = &Apache::edit::rebuild_tag($token);
  486: 	}
  487:     }
  488:     return $result;
  489: }
  490: 
  491: sub end_gnuplot {
  492:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  493:     pop @Apache::lonxml::namespace;
  494:     &Apache::lonxml::deregister('Apache::lonplot',
  495: 	('title','xlabel','ylabel','key','axis','label','curve'));
  496:     my $result = '';
  497:     my $randnumber;
  498:     # need to call rand everytime start_script would evaluate, as the
  499:     # safe space rand number generator and the global rand generator 
  500:     # are not separate
  501:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  502: 	$target eq 'answer') {
  503:       $randnumber=int(rand(1000));
  504:     }
  505:     if ($target eq 'web' || $target eq 'tex') {
  506: 	&check_inputs(); # Make sure we have all the data we need
  507: 	##
  508: 	## Determine filename
  509: 	my $tmpdir = '/home/httpd/perl/tmp/';
  510: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
  511: 	    '_'.time.'_'.$$.$randnumber.'_plot';
  512: 	## Write the plot description to the file
  513: 	&write_gnuplot_file($tmpdir,$filename,$target);
  514: 	$filename = &Apache::lonnet::escape($filename);
  515: 	## return image tag for the plot
  516: 	if ($target eq 'web') {
  517: 	    $result .= <<"ENDIMAGE";
  518: <img src    = "/cgi-bin/plot.gif?file=$filename.data&output=$weboutputformat" 
  519:      width  = "$plot{'width'}"
  520:      height = "$plot{'height'}"
  521:      align  = "$plot{'align'}"
  522:      alt    = "$plot{'alttag'}" />
  523: ENDIMAGE
  524:         } elsif ($target eq 'tex') {
  525: 	    #might be inside the safe space, register the URL for later
  526: 	    &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
  527: 	    $result = '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$plot{'texwidth'}.' mm]{'.&Apache::lonnet::unescape($filename).'.eps}';
  528: 	}
  529:     } elsif ($target eq 'edit') {
  530: 	$result.=&Apache::edit::tag_end($target,$token);
  531:     }
  532:     return $result;
  533: }
  534: 
  535: 
  536: ##--------------------------------------------------------------- xtics
  537: sub start_xtics {
  538:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  539:     my $result='';
  540:     if ($target eq 'web' || $target eq 'tex') {
  541: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
  542: 		    $tagstack->[-1]);
  543:     } elsif ($target eq 'edit') {
  544: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
  545: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  546: 				    \@tic_edit_order);
  547:     } elsif ($target eq 'modified') {
  548: 	my $constructtag=&Apache::edit::get_new_args
  549: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  550: 	if ($constructtag) {
  551: 	    $result = &Apache::edit::rebuild_tag($token);
  552: 	}
  553:     }
  554:     return $result;
  555: }
  556: 
  557: sub end_xtics {
  558:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  559:     my $result = '';
  560:     if ($target eq 'web' || $target eq 'tex') {
  561:     } elsif ($target eq 'edit') {
  562: 	$result.=&Apache::edit::tag_end($target,$token);
  563:     }
  564:     return $result;
  565: }
  566: 
  567: ##--------------------------------------------------------------- ytics
  568: sub start_ytics {
  569:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  570:     my $result='';
  571:     if ($target eq 'web' || $target eq 'tex') {
  572: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
  573: 		    $tagstack->[-1]);
  574:     } elsif ($target eq 'edit') {
  575: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
  576: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  577: 				    \@tic_edit_order);
  578:     } elsif ($target eq 'modified') {
  579: 	my $constructtag=&Apache::edit::get_new_args
  580: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  581: 	if ($constructtag) {
  582: 	    $result = &Apache::edit::rebuild_tag($token);
  583: 	}
  584:     }
  585:     return $result;
  586: }
  587: 
  588: sub end_ytics {
  589:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  590:     my $result = '';
  591:     if ($target eq 'web' || $target eq 'tex') {
  592:     } elsif ($target eq 'edit') {
  593: 	$result.=&Apache::edit::tag_end($target,$token);
  594:     }
  595:     return $result;
  596: }
  597: 
  598: 
  599: ##----------------------------------------------------------------- key
  600: sub start_key {
  601:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  602:     my $result='';
  603:     if ($target eq 'web' || $target eq 'tex') {
  604: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
  605: 		    $tagstack->[-1]);
  606:     } elsif ($target eq 'edit') {
  607: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
  608: 	$result .= &edit_attributes($target,$token,\%key_defaults);
  609:     } elsif ($target eq 'modified') {
  610: 	my $constructtag=&Apache::edit::get_new_args
  611: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
  612: 	if ($constructtag) {
  613: 	    $result = &Apache::edit::rebuild_tag($token);
  614: 	}
  615:     }
  616:     return $result;
  617: }
  618: 
  619: sub end_key {
  620:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  621:     my $result = '';
  622:     if ($target eq 'web' || $target eq 'tex') {
  623:     } elsif ($target eq 'edit') {
  624: 	$result.=&Apache::edit::tag_end($target,$token);
  625:     }
  626:     return $result;
  627: }
  628: 
  629: ##------------------------------------------------------------------- title
  630: sub start_title {
  631:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  632:     my $result='';
  633:     if ($target eq 'web' || $target eq 'tex') {
  634: 	$title = &Apache::lonxml::get_all_text("/title",$parser);
  635: 	$title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
  636: 	$title =~ s/\n/ /g;
  637: 	if (length($title) > $max_str_len) {
  638: 	    $title = substr($title,0,$max_str_len);
  639: 	}
  640:     } elsif ($target eq 'edit') {
  641: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
  642: 	my $text=&Apache::lonxml::get_all_text("/title",$parser);
  643: 	$result.=&Apache::edit::end_row().
  644: 	    &Apache::edit::start_spanning_row().
  645: 	    &Apache::edit::editline('',$text,'',60);
  646:     } elsif ($target eq 'modified') {
  647: 	$result.=&Apache::edit::rebuild_tag($token);
  648: 	$result.=&Apache::edit::modifiedfield("/title",$parser);
  649:     }
  650:     return $result;
  651: }
  652: 
  653: sub end_title {
  654:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  655:     my $result = '';
  656:     if ($target eq 'web' || $target eq 'tex') {
  657:     } elsif ($target eq 'edit') {
  658: 	$result.=&Apache::edit::tag_end($target,$token);
  659:     }
  660:     return $result;
  661: }
  662: ##------------------------------------------------------------------- xlabel
  663: sub start_xlabel {
  664:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  665:     my $result='';
  666:     if ($target eq 'web' || $target eq 'tex') {
  667: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser);
  668: 	$xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
  669: 	$xlabel =~ s/\n/ /g;
  670: 	if (length($xlabel) > $max_str_len) {
  671: 	    $xlabel = substr($xlabel,0,$max_str_len);
  672: 	}
  673:     } elsif ($target eq 'edit') {
  674: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
  675: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$parser);
  676: 	$result.=&Apache::edit::end_row().
  677: 	    &Apache::edit::start_spanning_row().
  678: 	    &Apache::edit::editline('',$text,'',60);
  679:     } elsif ($target eq 'modified') {
  680: 	$result.=&Apache::edit::rebuild_tag($token);	
  681: 	$result.=&Apache::edit::modifiedfield("/xlabel",$parser);
  682:     }
  683:     return $result;
  684: }
  685: 
  686: sub end_xlabel {
  687:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  688:     my $result = '';
  689:     if ($target eq 'web' || $target eq 'tex') {
  690:     } elsif ($target eq 'edit') {
  691: 	$result.=&Apache::edit::tag_end($target,$token);
  692:     }
  693:     return $result;
  694: }
  695: 
  696: ##------------------------------------------------------------------- ylabel
  697: sub start_ylabel {
  698:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  699:     my $result='';
  700:     if ($target eq 'web' || $target eq 'tex') {
  701: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser);
  702: 	$ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
  703: 	$ylabel =~ s/\n/ /g;
  704: 	if (length($ylabel) > $max_str_len) {
  705: 	    $ylabel = substr($ylabel,0,$max_str_len);
  706: 	}
  707:     } elsif ($target eq 'edit') {
  708: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
  709: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$parser);
  710: 	$result .= &Apache::edit::end_row().
  711: 	    &Apache::edit::start_spanning_row().
  712: 	    &Apache::edit::editline('',$text,'',60);
  713:     } elsif ($target eq 'modified') {
  714: 	$result.=&Apache::edit::rebuild_tag($token);
  715: 	$result.=&Apache::edit::modifiedfield("/ylabel",$parser);
  716:     }
  717:     return $result;
  718: }
  719: 
  720: sub end_ylabel {
  721:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  722:     my $result = '';
  723:     if ($target eq 'web' || $target eq 'tex') {
  724:     } elsif ($target eq 'edit') {
  725: 	$result.=&Apache::edit::tag_end($target,$token);
  726:     }
  727:     return $result;
  728: }
  729: 
  730: ##------------------------------------------------------------------- label
  731: sub start_label {
  732:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  733:     my $result='';
  734:     if ($target eq 'web' || $target eq 'tex') {
  735: 	my %label;
  736: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
  737: 		    $tagstack->[-1]);
  738: 	my $text = &Apache::lonxml::get_all_text("/label",$parser);
  739: 	$text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
  740: 	$text =~ s/\n/ /g;
  741: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
  742: 	$label{'text'} = $text;
  743: 	push(@labels,\%label);
  744:     } elsif ($target eq 'edit') {
  745: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
  746: 	$result .= &edit_attributes($target,$token,\%label_defaults);
  747: 	my $text = &Apache::lonxml::get_all_text("/label",$parser);
  748: 	$result .= &Apache::edit::end_row().
  749: 	    &Apache::edit::start_spanning_row().
  750: 	    &Apache::edit::editline('',$text,'',60);
  751:     } elsif ($target eq 'modified') {
  752: 	&Apache::edit::get_new_args
  753: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
  754: 	$result.=&Apache::edit::rebuild_tag($token);
  755: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
  756:     }
  757:     return $result;
  758: }
  759: 
  760: sub end_label {
  761:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  762:     my $result = '';
  763:     if ($target eq 'web' || $target eq 'tex') {
  764:     } elsif ($target eq 'edit') {
  765: 	$result.=&Apache::edit::tag_end($target,$token);
  766:     }
  767:     return $result;
  768: }
  769: 
  770: ##------------------------------------------------------------------- curve
  771: sub start_curve {
  772:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  773:     my $result='';
  774:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
  775:     push (@Apache::lonxml::namespace,'curve');
  776:     if ($target eq 'web' || $target eq 'tex') {
  777: 	my %curve;
  778: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
  779: 		    $tagstack->[-1]);
  780: 	push (@curves,\%curve);
  781:     } elsif ($target eq 'edit') {
  782: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
  783: 	$result .= &edit_attributes($target,$token,\%curve_defaults,
  784:                                     \@curve_edit_order);
  785:     } elsif ($target eq 'modified') {
  786: 	my $constructtag=&Apache::edit::get_new_args
  787: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
  788: 	if ($constructtag) {
  789: 	    $result = &Apache::edit::rebuild_tag($token);
  790: 	    $result.= &Apache::edit::handle_insert();
  791: 	}
  792:     }
  793:     return $result;
  794: }
  795: 
  796: sub end_curve {
  797:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  798:     my $result = '';
  799:     pop @Apache::lonxml::namespace;
  800:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
  801:     if ($target eq 'web' || $target eq 'tex') {
  802:     } elsif ($target eq 'edit') {
  803: 	$result.=&Apache::edit::tag_end($target,$token);
  804:     }
  805:     return $result;
  806: }
  807: 
  808: ##------------------------------------------------------------ curve function
  809: sub start_function {
  810:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  811:     my $result='';
  812:     if ($target eq 'web' || $target eq 'tex') {
  813: 	if (exists($curves[-1]->{'data'})) {
  814: 	    &Apache::lonxml::warning
  815:                 ('Use of the <b>curve function</b> tag precludes use of '.
  816:                  ' the <b>curve data</b> tag.  '.
  817:                  'The curve data tag will be omitted in favor of the '.
  818:                  'curve function declaration.');
  819: 	    delete $curves[-1]->{'data'} ;
  820: 	}
  821:         my $function = &Apache::lonxml::get_all_text("/function",$parser);
  822: 	$function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
  823: 	$curves[-1]->{'function'} = $function; 
  824:     } elsif ($target eq 'edit') {
  825: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
  826: 	my $text = &Apache::lonxml::get_all_text("/function",$parser);
  827: 	$result .= &Apache::edit::end_row().
  828: 	    &Apache::edit::start_spanning_row().
  829: 	    &Apache::edit::editline('',$text,'',60);
  830:     } elsif ($target eq 'modified') {
  831: 	$result.=&Apache::edit::rebuild_tag($token);
  832: 	$result.=&Apache::edit::modifiedfield("/function",$parser);
  833:     }
  834:     return $result;
  835: }
  836: 
  837: sub end_function {
  838:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  839:     my $result = '';
  840:     if ($target eq 'web' || $target eq 'tex') {
  841:     } elsif ($target eq 'edit') {
  842: 	$result .= &Apache::edit::end_table();
  843:     }
  844:     return $result;
  845: }
  846: 
  847: ##------------------------------------------------------------ curve  data
  848: sub start_data {
  849:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  850:     my $result='';
  851:     if ($target eq 'web' || $target eq 'tex') {
  852: 	if (exists($curves[-1]->{'function'})) {
  853: 	    &Apache::lonxml::warning
  854:                 ('Use of the <b>curve function</b> tag precludes use of '.
  855:                  ' the <b>curve data</b> tag.  '.
  856:                  'The curve function tag will be omitted in favor of the '.
  857:                  'curve data declaration.');
  858: 	    delete($curves[-1]->{'function'});
  859: 	}
  860: 	my $datatext = &Apache::lonxml::get_all_text("/data",$parser);
  861: 	$datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
  862: 	# Deal with cases where we're given an array...
  863: 	if ($datatext =~ /^\@/) {
  864: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
  865: 					  $safeeval,1);
  866: 	}
  867: 	$datatext =~ s/\s+/ /g;
  868: 	# Need to do some error checking on the @data array - 
  869: 	# make sure it's all numbers and make sure each array 
  870: 	# is of the same length.
  871: 	my @data;
  872: 	if ($datatext =~ /,/) { # comma deliminated
  873: 	    @data = split /,/,$datatext;
  874: 	} else { # Assume it's space separated.
  875: 	    @data = split / /,$datatext;
  876: 	}
  877: 	for (my $i=0;$i<=$#data;$i++) {
  878: 	    # Check that it's non-empty
  879: 	    if (! defined($data[$i])) {
  880: 		&Apache::lonxml::warning(
  881: 		    'undefined curve data value.  Replacing with '.
  882: 		    ' pi/e = 1.15572734979092');
  883: 		$data[$i] = 1.15572734979092;
  884: 	    }
  885: 	    # Check that it's a number
  886: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
  887: 		&Apache::lonxml::warning(
  888: 		    'Bad curve data value of '.$data[$i].'  Replacing with '.
  889: 		    ' pi/e = 1.15572734979092');
  890: 		$data[$i] = 1.15572734979092;
  891: 	    }
  892: 	}
  893: 	# complain if the number of data points is not the same as
  894: 	# in previous sets of data.
  895: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
  896: 	    &Apache::lonxml::warning
  897: 		('Number of data points is not consistent with previous '.
  898: 		 'number of data points');
  899: 	}
  900: 	push  @{$curves[-1]->{'data'}},\@data;
  901:     } elsif ($target eq 'edit') {
  902: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
  903: 	my $text = &Apache::lonxml::get_all_text("/data",$parser);
  904: 	$result .= &Apache::edit::end_row().
  905: 	    &Apache::edit::start_spanning_row().
  906: 	    &Apache::edit::editline('',$text,'',60);
  907:     } elsif ($target eq 'modified') {
  908: 	$result.=&Apache::edit::rebuild_tag($token);
  909: 	$result.=&Apache::edit::modifiedfield("/data",$parser);
  910:     }
  911:     return $result;
  912: }
  913: 
  914: sub end_data {
  915:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  916:     my $result = '';
  917:     if ($target eq 'web' || $target eq 'tex') {
  918:     } elsif ($target eq 'edit') {
  919: 	$result .= &Apache::edit::end_table();
  920:     }
  921:     return $result;
  922: }
  923: 
  924: ##------------------------------------------------------------------- axis
  925: sub start_axis {
  926:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  927:     my $result='';
  928:     if ($target eq 'web' || $target eq 'tex') {
  929: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
  930: 			$tagstack->[-1]);
  931:     } elsif ($target eq 'edit') {
  932: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
  933: 	$result .= &edit_attributes($target,$token,\%axis_defaults,
  934: 				    \@axis_edit_order);
  935:     } elsif ($target eq 'modified') {
  936: 	my $constructtag=&Apache::edit::get_new_args
  937: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
  938: 	if ($constructtag) {
  939: 	    $result = &Apache::edit::rebuild_tag($token);
  940: 	}
  941:     }
  942:     return $result;
  943: }
  944: 
  945: sub end_axis {
  946:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  947:     my $result = '';
  948:     if ($target eq 'web' || $target eq 'tex') {
  949:     } elsif ($target eq 'edit') {
  950: 	$result.=&Apache::edit::tag_end($target,$token);
  951:     } elsif ($target eq 'modified') {
  952:     }
  953:     return $result;
  954: }
  955: 
  956: ###################################################################
  957: ##                                                               ##
  958: ##        Utility Functions                                      ##
  959: ##                                                               ##
  960: ###################################################################
  961: 
  962: ##----------------------------------------------------------- set_defaults
  963: sub set_defaults {
  964:     my ($var,$defaults) = @_;
  965:     my $key;
  966:     foreach $key (keys(%$defaults)) {
  967: 	$var->{$key} = $defaults->{$key}->{'default'};
  968:     }
  969: }
  970: 
  971: ##------------------------------------------------------------------- misc
  972: sub get_attributes{
  973:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
  974:     foreach my $attr (keys(%{$defaults})) {
  975: 	if ($attr eq 'texwidth' || $attr eq 'texfont') {
  976: 	    $values->{$attr} = 
  977: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
  978: 	} else {
  979: 	    $values->{$attr} = 
  980: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval);
  981: 	}
  982: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
  983: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
  984: 	    next;
  985: 	}
  986: 	my $test = $defaults->{$attr}->{'test'};
  987: 	if (! &$test($values->{$attr})) {
  988: 	    &Apache::lonxml::warning
  989: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
  990: 		 .$defaults->{$attr}->{'default'} );
  991: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
  992: 	}
  993:     }
  994:     return ;
  995: }
  996: 
  997: ##------------------------------------------------------- write_gnuplot_file
  998: sub write_gnuplot_file {
  999:     my ($tmpdir,$filename,$target)= @_;
 1000:     my $gnuplot_input = '';
 1001:     my $curve;
 1002:     my $pt = $plot{'texfont'};
 1003:     #
 1004:     # Check to be sure we do not have any empty curves
 1005:     my @curvescopy;
 1006:     foreach my $curve (@curves) {
 1007:         if (exists($curve->{'function'})) {
 1008:             if ($curve->{'function'} !~ /^\s*$/) {
 1009:                 push(@curvescopy,$curve);
 1010:             }
 1011:         } elsif (exists($curve->{'data'})) {
 1012:             foreach my $data (@{$curve->{'data'}}) {
 1013:                 if (scalar(@$data) > 0) {
 1014:                     push(@curvescopy,$curve);
 1015:                     last;
 1016:                 }
 1017:             }
 1018:         }
 1019:     }
 1020:     @curves = @curvescopy;
 1021:     # Collect all the colors
 1022:     my @Colors;
 1023:     push @Colors, $plot{'bgcolor'};
 1024:     push @Colors, $plot{'fgcolor'}; 
 1025:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
 1026:     foreach $curve (@curves) {
 1027: 	push @Colors, ($curve->{'color'} ne '' ? 
 1028: 		       $curve->{'color'}       : 
 1029: 		       $plot{'fgcolor'}        );
 1030:     }
 1031:     # set term
 1032:     if ($target eq 'web') {
 1033: 	$gnuplot_input .= 'set term '.$weboutputformat .' ';
 1034: 	$gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
 1035: 	$gnuplot_input .= $plot{'font'} . ' ';
 1036: 	$gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
 1037: 	$gnuplot_input .= "@Colors\n";
 1038: 	# set output
 1039: 	$gnuplot_input .= "set output\n";
 1040:     } elsif ($target eq 'tex') {
 1041: 	$gnuplot_input .= "set term postscript eps monochrome solid \"Helvetica\" $pt \n";
 1042: 	$gnuplot_input .= "set output \"/home/httpd/perl/tmp/".
 1043: 	    &Apache::lonnet::unescape($filename).".eps\"\n";
 1044:     }
 1045:     # cartesian or polar?
 1046:     if (lc($plot{'plottype'}) eq 'polar') {
 1047:         $gnuplot_input .= 'set polar'.$/;
 1048:     } else {
 1049:         # Assume Cartesian
 1050:     }
 1051:     # margin
 1052:     if (lc($plot{'lmargin'}) ne 'default') {
 1053:         $gnuplot_input .= 'set lmargin '.$plot{'lmargin'}.$/;
 1054:     }
 1055:     if (lc($plot{'rmargin'}) ne 'default') {
 1056:         $gnuplot_input .= 'set rmargin '.$plot{'rmargin'}.$/;
 1057:     }
 1058:     if (lc($plot{'tmargin'}) ne 'default') {
 1059:         $gnuplot_input .= 'set tmargin '.$plot{'tmargin'}.$/;
 1060:     }
 1061:     if (lc($plot{'bmargin'}) ne 'default') {
 1062:         $gnuplot_input .= 'set bmargin '.$plot{'bmargin'}.$/;
 1063:     }
 1064:     # tic scales
 1065:     $gnuplot_input .= 'set ticscale '.
 1066:         $plot{'major_ticscale'}.' '.$plot{'minor_ticscale'}.$/;
 1067:     # grid
 1068:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
 1069:     # border
 1070:     $gnuplot_input .= ($plot{'border'} eq 'on'?
 1071: 		       'set border'.$/           :
 1072: 		       'set noborder'.$/         );
 1073:     # sampling rate for non-data curves
 1074:     $gnuplot_input .= "set samples $plot{'samples'}\n";
 1075:     # title, xlabel, ylabel
 1076:     # titles
 1077:     if ($target eq 'tex') {
 1078:         $gnuplot_input .= "set title  \"$title\" font \"Helvetica,".$pt."pt\"\n"  if (defined($title)) ;
 1079:         $gnuplot_input .= "set xlabel \"$xlabel\" font \"Helvetica,".$pt."pt\" \n" if (defined($xlabel));
 1080:         $gnuplot_input .= "set ylabel \"$ylabel\" font \"Helvetica,".$pt."pt\"\n" if (defined($ylabel));
 1081:     } else {
 1082:         $gnuplot_input .= "set title  \"$title\"  \n"  if (defined($title)) ;
 1083:         $gnuplot_input .= "set xlabel \"$xlabel\" \n" if (defined($xlabel));
 1084:         $gnuplot_input .= "set ylabel \"$ylabel\" \n" if (defined($ylabel));
 1085:     }
 1086:     # tics
 1087:     if (%xtics) {    
 1088: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
 1089: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
 1090: 	$gnuplot_input .= "$xtics{'start'}, ";
 1091: 	$gnuplot_input .= "$xtics{'increment'}, ";
 1092: 	$gnuplot_input .= "$xtics{'end'}\n";
 1093:         if ($xtics{'minorfreq'} != 0) {
 1094:             $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
 1095:         } 
 1096:     }
 1097:     if (%ytics) {    
 1098: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
 1099: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
 1100: 	$gnuplot_input .= "$ytics{'start'}, ";
 1101: 	$gnuplot_input .= "$ytics{'increment'}, ";
 1102:         $gnuplot_input .= "$ytics{'end'}\n";
 1103:         if ($ytics{'minorfreq'} != 0) {
 1104:             $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
 1105:         } 
 1106:     }
 1107:     # axis
 1108:     if (%axis) {
 1109: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
 1110: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
 1111:     }
 1112:     # Key
 1113:     if (%key) {
 1114: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
 1115: 	if ($key{'title'} ne '') {
 1116: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
 1117: 	} 
 1118: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
 1119:     } else {
 1120: 	$gnuplot_input .= 'set nokey'.$/;
 1121:     }
 1122:     # labels
 1123:     my $label;
 1124:     foreach $label (@labels) {
 1125: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
 1126: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'};
 1127:         if ($target eq 'tex') {
 1128:             $gnuplot_input .=' font "Helvetica,'.$pt.'pt"' ;
 1129:         }
 1130:         $gnuplot_input .= $/;
 1131:     }
 1132:     if ($target eq 'tex') {
 1133:         $gnuplot_input .="set size 1,".$plot{'height'}/$plot{'width'}*1.38;
 1134:         $gnuplot_input .="\n";
 1135:         }
 1136:     # curves
 1137:     $gnuplot_input .= 'plot ';
 1138:     for (my $i = 0;$i<=$#curves;$i++) {
 1139: 	$curve = $curves[$i];
 1140: 	$gnuplot_input.= ', ' if ($i > 0);
 1141: 	if (exists($curve->{'function'})) {
 1142: 	    $gnuplot_input.= 
 1143: 		$curve->{'function'}.' title "'.
 1144: 		$curve->{'name'}.'" with '.
 1145:                 $curve->{'linestyle'};
 1146:             $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
 1147:             if (($curve->{'linestyle'} eq 'points')      ||
 1148:                 ($curve->{'linestyle'} eq 'linespoints') ||
 1149:                 ($curve->{'linestyle'} eq 'errorbars')   ||
 1150:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
 1151:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
 1152:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
 1153:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
 1154:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
 1155:             }
 1156: 	} elsif (exists($curve->{'data'})) {
 1157: 	    # Store data values in $datatext
 1158: 	    my $datatext = '';
 1159: 	    #   get new filename
 1160: 	    my $datafilename = "$tmpdir/$filename.data.$i";
 1161: 	    my $fh=Apache::File->new(">$datafilename");
 1162: 	    # Compile data
 1163: 	    my @Data = @{$curve->{'data'}};
 1164: 	    my @Data0 = @{$Data[0]};
 1165: 	    for (my $i =0; $i<=$#Data0; $i++) {
 1166: 		my $dataset;
 1167: 		foreach $dataset (@Data) {
 1168: 		    $datatext .= $dataset->[$i] . ' ';
 1169: 		}
 1170: 		$datatext .= $/;
 1171: 	    }
 1172: 	    #   write file
 1173: 	    print $fh $datatext;
 1174: 	    close ($fh);
 1175: 	    #   generate gnuplot text
 1176: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
 1177: 		$curve->{'name'}.'" with '.
 1178: 		$curve->{'linestyle'};
 1179:             $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
 1180:             if (($curve->{'linestyle'} eq 'points')      ||
 1181:                 ($curve->{'linestyle'} eq 'linespoints') ||
 1182:                 ($curve->{'linestyle'} eq 'errorbars')   ||
 1183:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
 1184:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
 1185:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
 1186:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
 1187:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
 1188:             }
 1189: 	}
 1190:     }
 1191:     # Write the output to a file.
 1192:     my $fh=Apache::File->new(">$tmpdir$filename.data");
 1193:     print $fh $gnuplot_input;
 1194:     close($fh);
 1195:     # That's all folks.
 1196:     return ;
 1197: }
 1198: 
 1199: #---------------------------------------------- check_inputs
 1200: sub check_inputs {
 1201:     ## Note: no inputs, no outputs - this acts only on global variables.
 1202:     ## Make sure we have all the input we need:
 1203:     if (! %plot) { &set_defaults(\%plot,\%gnuplot_defaults); }
 1204:     if (! %key ) {} # No key for this plot, thats okay
 1205: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
 1206:     if (! defined($title )) {} # No title for this plot, thats okay
 1207:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
 1208:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
 1209:     if ($#labels < 0) { }      # No labels for this plot, thats okay
 1210:     if ($#curves < 0) { 
 1211: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
 1212: 	return '';
 1213:     }
 1214:     my $curve;
 1215:     foreach $curve (@curves) {
 1216: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
 1217: 	    &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
 1218: 	    return '';
 1219: 	}
 1220:     }
 1221: }
 1222: 
 1223: #------------------------------------------------ make_edit
 1224: sub edit_attributes {
 1225:     my ($target,$token,$defaults,$keys) = @_;
 1226:     my ($result,@keys);
 1227:     if ($keys && ref($keys) eq 'ARRAY') {
 1228:         @keys = @$keys;
 1229:     } else {
 1230: 	@keys = sort(keys(%$defaults));
 1231:     }
 1232:     foreach my $attr (@keys) {
 1233: 	# append a ' ' to the description if it doesn't have one already.
 1234: 	my $description = $defaults->{$attr}->{'description'};
 1235: 	$description .= ' ' if ($description !~ / $/);
 1236: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
 1237: 	    $result .= &Apache::edit::text_arg
 1238: 		($description,$attr,$token,
 1239: 		 $defaults->{$attr}->{'size'});
 1240: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
 1241: 	    $result .= &Apache::edit::select_or_text_arg
 1242: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
 1243: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
 1244: 	    $result .= &Apache::edit::select_or_text_arg
 1245: 		($description,$attr,['on','off'],$token);
 1246: 	}
 1247: 	$result .= '<br />';
 1248:     }
 1249:     return $result;
 1250: }
 1251: 
 1252: 
 1253: ###################################################################
 1254: ##                                                               ##
 1255: ##           Insertion functions for editing plots               ##
 1256: ##                                                               ##
 1257: ###################################################################
 1258: 
 1259: sub insert_gnuplot {
 1260:     my $result = '';
 1261:     #  plot attributes
 1262:     $result .= "\n<gnuplot ";
 1263:     foreach my $attr (keys(%gnuplot_defaults)) {
 1264: 	$result .= "\n     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
 1265:     }
 1266:     $result .= ">";
 1267:     # Add the components (most are commented out for simplicity)
 1268:     # $result .= &insert_key();
 1269:     # $result .= &insert_axis();
 1270:     # $result .= &insert_title();    
 1271:     # $result .= &insert_xlabel();    
 1272:     # $result .= &insert_ylabel();    
 1273:     $result .= &insert_curve();
 1274:     # close up the <gnuplot>
 1275:     $result .= "\n</gnuplot>";
 1276:     return $result;
 1277: }
 1278: 
 1279: sub insert_tics {
 1280:     my $result;
 1281:     $result .= &insert_xtics() . &insert_ytics;
 1282:     return $result;
 1283: }
 1284: 
 1285: sub insert_xtics {
 1286:     my $result;
 1287:     $result .= "\n    <xtics ";
 1288:     foreach my $attr (keys(%tic_defaults)) {
 1289: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1290:     }
 1291:     $result .= "/>";
 1292:     return $result;
 1293: }
 1294: 
 1295: sub insert_ytics {
 1296:     my $result;
 1297:     $result .= "\n    <ytics ";
 1298:     foreach my $attr (keys(%tic_defaults)) {
 1299: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1300:     }
 1301:     $result .= "/>";
 1302:     return $result;
 1303: }
 1304: 
 1305: sub insert_key {
 1306:     my $result;
 1307:     $result .= "\n    <key ";
 1308:     foreach my $attr (keys(%key_defaults)) {
 1309: 	$result .= "\n         $attr=\"$key_defaults{$attr}->{'default'}\"";
 1310:     }
 1311:     $result .= " />";
 1312:     return $result;
 1313: }
 1314: 
 1315: sub insert_axis{
 1316:     my $result;
 1317:     $result .= "\n    <axis ";
 1318:    foreach my $attr (keys(%axis_defaults)) {
 1319: 	$result .= "\n         $attr=\"$axis_defaults{$attr}->{'default'}\"";
 1320:     }
 1321:     $result .= " />";
 1322:     return $result;
 1323: }
 1324: 
 1325: sub insert_title  { return "\n    <title></title>"; }
 1326: sub insert_xlabel { return "\n    <xlabel></xlabel>"; }
 1327: sub insert_ylabel { return "\n    <ylabel></ylabel>"; }
 1328: 
 1329: sub insert_label {
 1330:     my $result;
 1331:     $result .= "\n    <label ";
 1332:     foreach my $attr (keys(%label_defaults)) {
 1333: 	$result .= "\n         $attr=\"".
 1334:             $label_defaults{$attr}->{'default'}."\"";
 1335:     }
 1336:     $result .= "></label>";
 1337:     return $result;
 1338: }
 1339: 
 1340: sub insert_curve {
 1341:     my $result;
 1342:     $result .= "\n    <curve ";
 1343:     foreach my $attr (keys(%curve_defaults)) {
 1344: 	$result .= "\n         $attr=\"".
 1345: 	    $curve_defaults{$attr}->{'default'}."\"";
 1346:     }
 1347:     $result .= " >";
 1348:     $result .= &insert_data().&insert_data()."\n    </curve>";
 1349: }
 1350: 
 1351: sub insert_function {
 1352:     my $result;
 1353:     $result .= "\n        <function></function>";
 1354:     return $result;
 1355: }
 1356: 
 1357: sub insert_data {
 1358:     my $result;
 1359:     $result .= "\n        <data></data>";
 1360:     return $result;
 1361: }
 1362: 
 1363: ##----------------------------------------------------------------------
 1364: 1;
 1365: __END__
 1366: 
 1367: 

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