File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.58: download - view: text, annotated - select for diffs
Fri Mar 22 13:45:59 2002 UTC (22 years, 1 month ago) by matthew
Branches: MAIN
CVS tags: HEAD
Changes to avoid double-interpolation of perl variables.

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

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