File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.56: download - view: text, annotated - select for diffs
Thu Mar 14 19:02:30 2002 UTC (22 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- the rand call was screwing up grade and answer mode parses BUG #190

    1: # The LearningOnline Network with CAPA
    2: # Dynamic plot
    3: #
    4: # $Id: lonplot.pm,v 1.56 2002/03/14 19:02:30 albertel 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.  Be careful using this
  366: tag - 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: 	my $inside = &Apache::lonxml::get_all_text("/gnuplot",$$parser[-1]);
  420: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  421: 	&Apache::lonxml::newparser($parser,\$inside);
  422: 	&get_attributes(\%plot,\%gnuplot_defaults,$parstack,$safeeval,
  423: 			$tagstack->[-1]);
  424:     } elsif ($target eq 'edit') {
  425: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
  426: 	$result .= &make_javascript();
  427: 	$result .= &help_win($gnuplot_help_text);
  428: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
  429: 				    \@gnuplot_edit_order);
  430:     } elsif ($target eq 'modified') {
  431: 	my $constructtag=&Apache::edit::get_new_args
  432: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
  433: 	if ($constructtag) {
  434: 	    $result = &Apache::edit::rebuild_tag($token);
  435: 	}
  436:     }
  437:     return $result;
  438: }
  439: 
  440: sub end_gnuplot {
  441:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  442:     pop @Apache::lonxml::namespace;
  443:     &Apache::lonxml::deregister('Apache::lonplot',
  444: 	('title','xlabel','ylabel','key','axis','label','curve'));
  445:     my $result = '';
  446:     my $randnumber;
  447:     # need to call rand everytime start_script would evaluate, as the
  448:     # safe space rand number generator and the global rand generator 
  449:     # are not seperate
  450:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  451: 	$target eq 'answer') {
  452:       $randnumber=int(rand(1000));
  453:     }
  454:     if ($target eq 'web' || $target eq 'tex') {
  455: 	&check_inputs(); # Make sure we have all the data we need
  456: 	##
  457: 	## Determine filename
  458: 	my $tmpdir = '/home/httpd/perl/tmp/';
  459: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
  460: 	    '_'.time.'_'.$$.$randnumber.'_plot.data';
  461: 	## Write the plot description to the file
  462: 	&write_gnuplot_file($tmpdir,$filename,$target);
  463: 	$filename = &Apache::lonnet::escape($filename);
  464: 	## return image tag for the plot
  465: 	if ($target eq 'web') {
  466: 	    $result .= <<"ENDIMAGE";
  467: <img src    = "/cgi-bin/plot.gif?file=$filename&output=gif" 
  468:      width  = "$plot{'width'}" 
  469:      height = "$plot{'height'}"
  470:      align  = "$plot{'align'}"
  471:      alt    = "image should be /cgi-bin/plot.gif?$filename" />
  472: ENDIMAGE
  473:         } elsif ($target eq 'tex') {
  474: 	    &Apache::lonnet::ssi('cgi-bin/plot.gif?file=$filename'.
  475: 				 '&output=eps');
  476: 	    $result = "$filename.eps";
  477: 	}
  478:     } elsif ($target eq 'edit') {
  479: 	$result.=&Apache::edit::tag_end($target,$token);
  480:     }
  481:     return $result;
  482: }
  483: 
  484: 
  485: ##--------------------------------------------------------------- xtics
  486: sub start_xtics {
  487:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  488:     my $result='';
  489:     if ($target eq 'web' || $target eq 'tex') {
  490: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
  491: 		    $tagstack->[-1]);
  492:     } elsif ($target eq 'edit') {
  493: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
  494: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  495: 				    \@tic_edit_order);
  496:     } elsif ($target eq 'modified') {
  497: 	my $constructtag=&Apache::edit::get_new_args
  498: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  499: 	if ($constructtag) {
  500: 	    $result = &Apache::edit::rebuild_tag($token);
  501: 	}
  502:     }
  503:     return $result;
  504: }
  505: 
  506: sub end_xtics {
  507:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  508:     my $result = '';
  509:     if ($target eq 'web' || $target eq 'tex') {
  510:     } elsif ($target eq 'edit') {
  511: 	$result.=&Apache::edit::tag_end($target,$token);
  512:     }
  513:     return $result;
  514: }
  515: 
  516: ##--------------------------------------------------------------- ytics
  517: sub start_ytics {
  518:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  519:     my $result='';
  520:     if ($target eq 'web' || $target eq 'tex') {
  521: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
  522: 		    $tagstack->[-1]);
  523:     } elsif ($target eq 'edit') {
  524: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
  525: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  526: 				    \@tic_edit_order);
  527:     } elsif ($target eq 'modified') {
  528: 	my $constructtag=&Apache::edit::get_new_args
  529: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  530: 	if ($constructtag) {
  531: 	    $result = &Apache::edit::rebuild_tag($token);
  532: 	}
  533:     }
  534:     return $result;
  535: }
  536: 
  537: sub end_ytics {
  538:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  539:     my $result = '';
  540:     if ($target eq 'web' || $target eq 'tex') {
  541:     } elsif ($target eq 'edit') {
  542: 	$result.=&Apache::edit::tag_end($target,$token);
  543:     }
  544:     return $result;
  545: }
  546: 
  547: 
  548: ##----------------------------------------------------------------- key
  549: sub start_key {
  550:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  551:     my $result='';
  552:     if ($target eq 'web' || $target eq 'tex') {
  553: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
  554: 		    $tagstack->[-1]);
  555:     } elsif ($target eq 'edit') {
  556: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
  557: 	$result .= &edit_attributes($target,$token,\%key_defaults);
  558:     } elsif ($target eq 'modified') {
  559: 	my $constructtag=&Apache::edit::get_new_args
  560: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
  561: 	if ($constructtag) {
  562: 	    $result = &Apache::edit::rebuild_tag($token);
  563: 	}
  564:     }
  565:     return $result;
  566: }
  567: 
  568: sub end_key {
  569:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  570:     my $result = '';
  571:     if ($target eq 'web' || $target eq 'tex') {
  572:     } elsif ($target eq 'edit') {
  573: 	$result.=&Apache::edit::tag_end($target,$token);
  574:     }
  575:     return $result;
  576: }
  577: 
  578: ##------------------------------------------------------------------- title
  579: sub start_title {
  580:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  581:     my $result='';
  582:     if ($target eq 'web' || $target eq 'tex') {
  583: 	$title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
  584: 	$title =~ s/\n/ /g;
  585: 	if (length($title) > $max_str_len) {
  586: 	    $title = substr($title,0,$max_str_len);
  587: 	}
  588:     } elsif ($target eq 'edit') {
  589: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
  590: 	my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
  591: 	$result.=&Apache::edit::end_row().
  592: 	    &Apache::edit::start_spanning_row().
  593: 	    &Apache::edit::editfield('',$text,'',60,1);
  594:     } elsif ($target eq 'modified') {
  595: 	my $text=$$parser[-1]->get_text("/title");
  596: 	$result.=&Apache::edit::rebuild_tag($token);
  597: 	$result.=&Apache::edit::modifiedfield($token);
  598:     }
  599:     return $result;
  600: }
  601: 
  602: sub end_title {
  603:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  604:     my $result = '';
  605:     if ($target eq 'web' || $target eq 'tex') {
  606:     } elsif ($target eq 'edit') {
  607: 	$result.=&Apache::edit::tag_end($target,$token);
  608:     }
  609:     return $result;
  610: }
  611: ##------------------------------------------------------------------- xlabel
  612: sub start_xlabel {
  613:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  614:     my $result='';
  615:     if ($target eq 'web' || $target eq 'tex') {
  616: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
  617: 	$xlabel =~ s/\n/ /g;
  618: 	if (length($xlabel) > $max_str_len) {
  619: 	    $xlabel = substr($xlabel,0,$max_str_len);
  620: 	}
  621:     } elsif ($target eq 'edit') {
  622: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
  623: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
  624: 	$result.=&Apache::edit::end_row().
  625: 	    &Apache::edit::start_spanning_row().
  626: 	    &Apache::edit::editfield('',$text,'',60,1);
  627:     } elsif ($target eq 'modified') {
  628: 	my $text=$$parser[-1]->get_text("/xlabel");
  629: 	$result.=&Apache::edit::rebuild_tag($token);	
  630: 	$result.=&Apache::edit::modifiedfield($token);
  631:     }
  632:     return $result;
  633: }
  634: 
  635: sub end_xlabel {
  636:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  637:     my $result = '';
  638:     if ($target eq 'web' || $target eq 'tex') {
  639:     } elsif ($target eq 'edit') {
  640: 	$result.=&Apache::edit::tag_end($target,$token);
  641:     }
  642:     return $result;
  643: }
  644: 
  645: ##------------------------------------------------------------------- ylabel
  646: sub start_ylabel {
  647:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  648:     my $result='';
  649:     if ($target eq 'web' || $target eq 'tex') {
  650: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-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 =~ s/\n/ /g;
  689: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
  690: 	$label{'text'} = $text;
  691: 	push(@labels,\%label);
  692:     } elsif ($target eq 'edit') {
  693: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
  694: 	$result .= &edit_attributes($target,$token,\%label_defaults);
  695: 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
  696: 	$result .= &Apache::edit::end_row().
  697: 	    &Apache::edit::start_spanning_row().
  698: 	    &Apache::edit::editfield('',$text,'',60,1);
  699:     } elsif ($target eq 'modified') {
  700: 	&Apache::edit::get_new_args
  701: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
  702: 	$result.=&Apache::edit::rebuild_tag($token);
  703: 	my $text=$$parser[-1]->get_text("/label");
  704: 	$result.=&Apache::edit::modifiedfield($token);
  705:     }
  706:     return $result;
  707: }
  708: 
  709: sub end_label {
  710:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  711:     my $result = '';
  712:     if ($target eq 'web' || $target eq 'tex') {
  713:     } elsif ($target eq 'edit') {
  714: 	$result.=&Apache::edit::tag_end($target,$token);
  715:     }
  716:     return $result;
  717: }
  718: 
  719: ##------------------------------------------------------------------- curve
  720: sub start_curve {
  721:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  722:     my $result='';
  723:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
  724:     push (@Apache::lonxml::namespace,'curve');
  725:     if ($target eq 'web' || $target eq 'tex') {
  726: 	my %curve;
  727: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
  728: 		    $tagstack->[-1]);
  729: 	push (@curves,\%curve);
  730:     } elsif ($target eq 'edit') {
  731: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
  732: 	$result .= &help_win($curve_help_text);
  733: 	$result .= &edit_attributes($target,$token,\%curve_defaults);
  734:     } elsif ($target eq 'modified') {
  735: 	my $constructtag=&Apache::edit::get_new_args
  736: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
  737: 	if ($constructtag) {
  738: 	    $result = &Apache::edit::rebuild_tag($token);
  739: 	    $result.= &Apache::edit::handle_insert();
  740: 	}
  741:     }
  742:     return $result;
  743: }
  744: 
  745: sub end_curve {
  746:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  747:     my $result = '';
  748:     pop @Apache::lonxml::namespace;
  749:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
  750:     if ($target eq 'web' || $target eq 'tex') {
  751:     } elsif ($target eq 'edit') {
  752: 	$result.=&Apache::edit::tag_end($target,$token);
  753:     }
  754:     return $result;
  755: }
  756: 
  757: ##------------------------------------------------------------ curve function
  758: sub start_function {
  759:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  760:     my $result='';
  761:     if ($target eq 'web' || $target eq 'tex') {
  762: 	if (exists($curves[-1]->{'data'})) {
  763: 	    &Apache::lonxml::warning('Use of <function> precludes use of <data>.  The <data> will be omitted in favor of the <function> declaration.');
  764: 	    delete $curves[-1]->{'data'} ;
  765: 	}
  766: 	$curves[-1]->{'function'} = 
  767: 	    &Apache::lonxml::get_all_text("/function",$$parser[-1]);
  768:     } elsif ($target eq 'edit') {
  769: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
  770: 	my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
  771: 	$result .= &Apache::edit::end_row().
  772: 	    &Apache::edit::start_spanning_row().
  773: 	    &Apache::edit::editfield('',$text,'',60,1);
  774:     } elsif ($target eq 'modified') {
  775: 	$result.=&Apache::edit::rebuild_tag($token);
  776: 	my $text=$$parser[-1]->get_text("/function");
  777: 	$result.=&Apache::edit::modifiedfield($token);
  778:     }
  779:     return $result;
  780: }
  781: 
  782: sub end_function {
  783:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  784:     my $result = '';
  785:     if ($target eq 'web' || $target eq 'tex') {
  786:     } elsif ($target eq 'edit') {
  787: 	$result .= &Apache::edit::end_table();
  788:     }
  789:     return $result;
  790: }
  791: 
  792: ##------------------------------------------------------------ curve  data
  793: sub start_data {
  794:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  795:     my $result='';
  796:     if ($target eq 'web' || $target eq 'tex') {
  797: 	if (exists($curves[-1]->{'function'})) {
  798: 	    &Apache::lonxml::warning('Use of <data> precludes use of .'.
  799: 	    '<function>.  The <function> will be omitted in favor of '.
  800:             'the <data> declaration.');
  801: 	    delete($curves[-1]->{'function'});
  802: 	}
  803: 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
  804: 	# Deal with cases where we're given an array...
  805: 	if ($datatext =~ /^\@/) {
  806: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
  807: 					  $safeeval,1);
  808: 	}
  809: 	$datatext =~ s/\s+/ /g;
  810: 	# Need to do some error checking on the @data array - 
  811: 	# make sure it's all numbers and make sure each array 
  812: 	# is of the same length.
  813: 	my @data;
  814: 	if ($datatext =~ /,/) { # comma deliminated
  815: 	    @data = split /,/,$datatext;
  816: 	} else { # Assume it's space seperated.
  817: 	    @data = split / /,$datatext;
  818: 	}
  819: 	for (my $i=0;$i<=$#data;$i++) {
  820: 	    # Check that it's non-empty
  821: 	    if (! defined($data[$i])) {
  822: 		&Apache::lonxml::warning(
  823: 		    'undefined <data> value.  Replacing with '.
  824: 		    ' pi/e = 1.15572734979092');
  825: 		$data[$i] = 1.15572734979092;
  826: 	    }
  827: 	    # Check that it's a number
  828: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
  829: 		&Apache::lonxml::warning(
  830: 		    'Bad <data> value of '.$data[$i].'  Replacing with '.
  831: 		    ' pi/e = 1.15572734979092');
  832: 		$data[$i] = 1.15572734979092;
  833: 	    }
  834: 	}
  835: 	# complain if the number of data points is not the same as
  836: 	# in previous sets of data.
  837: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
  838: 	    &Apache::lonxml::warning
  839: 		('Number of data points is not consistent with previous '.
  840: 		 'number of data points');
  841: 	}
  842: 	push  @{$curves[-1]->{'data'}},\@data;
  843:     } elsif ($target eq 'edit') {
  844: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
  845: 	my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
  846: 	$result .= &Apache::edit::end_row().
  847: 	    &Apache::edit::start_spanning_row().
  848: 	    &Apache::edit::editfield('',$text,'',60,1);
  849:     } elsif ($target eq 'modified') {
  850: 	$result.=&Apache::edit::rebuild_tag($token);
  851: 	my $text=$$parser[-1]->get_text("/data");
  852: 	$result.=&Apache::edit::modifiedfield($token);
  853:     }
  854:     return $result;
  855: }
  856: 
  857: sub end_data {
  858:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  859:     my $result = '';
  860:     if ($target eq 'web' || $target eq 'tex') {
  861:     } elsif ($target eq 'edit') {
  862: 	$result .= &Apache::edit::end_table();
  863:     }
  864:     return $result;
  865: }
  866: 
  867: ##------------------------------------------------------------------- axis
  868: sub start_axis {
  869:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  870:     my $result='';
  871:     if ($target eq 'web' || $target eq 'tex') {
  872: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
  873: 			$tagstack->[-1]);
  874:     } elsif ($target eq 'edit') {
  875: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
  876: 	$result .= &edit_attributes($target,$token,\%axis_defaults);
  877:     } elsif ($target eq 'modified') {
  878: 	my $constructtag=&Apache::edit::get_new_args
  879: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
  880: 	if ($constructtag) {
  881: 	    $result = &Apache::edit::rebuild_tag($token);
  882: 	}
  883:     }
  884:     return $result;
  885: }
  886: 
  887: sub end_axis {
  888:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  889:     my $result = '';
  890:     if ($target eq 'web' || $target eq 'tex') {
  891:     } elsif ($target eq 'edit') {
  892: 	$result.=&Apache::edit::tag_end($target,$token);
  893:     } elsif ($target eq 'modified') {
  894:     }
  895:     return $result;
  896: }
  897: 
  898: ###################################################################
  899: ##                                                               ##
  900: ##        Utility Functions                                      ##
  901: ##                                                               ##
  902: ###################################################################
  903: 
  904: ##----------------------------------------------------------- set_defaults
  905: sub set_defaults {
  906:     my ($var,$defaults) = @_;
  907:     my $key;
  908:     foreach $key (keys(%$defaults)) {
  909: 	$var->{$key} = $defaults->{$key}->{'default'};
  910:     }
  911: }
  912: 
  913: ##------------------------------------------------------------------- misc
  914: sub get_attributes{
  915:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
  916:     foreach my $attr (keys(%{$defaults})) {
  917: 	$values->{$attr} = 
  918: 	    &Apache::lonxml::get_param($attr,$parstack,$safeeval);
  919: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
  920: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
  921: 	    next;
  922: 	}
  923: 	my $test = $defaults->{$attr}->{'test'};
  924: 	if (! &$test($values->{$attr})) {
  925: 	    &Apache::lonxml::warning
  926: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
  927: 		 .$defaults->{$attr}->{'default'} );
  928: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
  929: 	}
  930:     }
  931:     return ;
  932: }
  933: 
  934: ##------------------------------------------------------- write_gnuplot_file
  935: sub write_gnuplot_file {
  936:     my ($tmpdir,$filename,$target)= @_;
  937:     my $gnuplot_input = '';
  938:     my $curve;
  939:     # Collect all the colors
  940:     my @Colors;
  941:     push @Colors, $plot{'bgcolor'};
  942:     push @Colors, $plot{'fgcolor'}; 
  943:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
  944:     foreach $curve (@curves) {
  945: 	push @Colors, ($curve->{'color'} ne '' ? 
  946: 		       $curve->{'color'}       : 
  947: 		       $plot{'fgcolor'}        );
  948:     }
  949:     # set term
  950:     if ($target eq 'web') {
  951: 	$gnuplot_input .= 'set term gif ';
  952: 	$gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
  953: 	$gnuplot_input .= $plot{'font'} . ' ';
  954: 	$gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
  955: 	$gnuplot_input .= "@Colors\n";
  956: 	# set output
  957: 	$gnuplot_input .= "set output\n";
  958:     } elsif ($target eq 'tex') {
  959: 	$gnuplot_input .= "set term postscript eps monochrome\n";
  960: 	$gnuplot_input .= "set output \"$filename.eps\"\n";
  961:     }
  962:     # grid
  963:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
  964:     # border
  965:     $gnuplot_input .= ($plot{'border'} eq 'on'?
  966: 		       'set border'.$/           :
  967: 		       'set noborder'.$/         );    # title, xlabel, ylabel
  968:     # titles
  969:     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;
  970:     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
  971:     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
  972:     # tics
  973:     if (%xtics) {    
  974: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
  975: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
  976: 	$gnuplot_input .= "$xtics{'start'}, ";
  977: 	$gnuplot_input .= "$xtics{'increment'}, ";
  978: 	$gnuplot_input .= "$xtics{'end'}\n";
  979:     }
  980:     if (%ytics) {    
  981: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
  982: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
  983: 	$gnuplot_input .= "$ytics{'start'}, ";
  984: 	$gnuplot_input .= "$ytics{'increment'}, ";
  985:         $gnuplot_input .= "$ytics{'end'}\n";
  986:     }
  987:     # axis
  988:     if (%axis) {
  989: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
  990: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
  991:     }
  992:     # Key
  993:     if (%key) {
  994: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
  995: 	if ($key{'title'} ne '') {
  996: 	    $gnuplot_input .= 'title " '.$key{'title'}.'" ';
  997: 	} 
  998: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
  999:     } else {
 1000: 	$gnuplot_input .= 'set nokey'.$/;
 1001:     }
 1002:     # labels
 1003:     my $label;
 1004:     foreach $label (@labels) {
 1005: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
 1006: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
 1007:     }
 1008:     # curves
 1009:     $gnuplot_input .= 'plot ';
 1010:     for (my $i = 0;$i<=$#curves;$i++) {
 1011: 	$curve = $curves[$i];
 1012: 	$gnuplot_input.= ', ' if ($i > 0);
 1013: 	if (exists($curve->{'function'})) {
 1014: 	    $gnuplot_input.= 
 1015: 		$curve->{'function'}.' title "'.
 1016: 		$curve->{'name'}.'" with '.
 1017: 		$curve->{'linestyle'};
 1018: 	} elsif (exists($curve->{'data'})) {
 1019: 	    # Store data values in $datatext
 1020: 	    my $datatext = '';
 1021: 	    #   get new filename
 1022: 	    my $datafilename = "$tmpdir/$filename.$i";
 1023: 	    my $fh=Apache::File->new(">$datafilename");
 1024: 	    # Compile data
 1025: 	    my @Data = @{$curve->{'data'}};
 1026: 	    my @Data0 = @{$Data[0]};
 1027: 	    for (my $i =0; $i<=$#Data0; $i++) {
 1028: 		my $dataset;
 1029: 		foreach $dataset (@Data) {
 1030: 		    $datatext .= $dataset->[$i] . ' ';
 1031: 		}
 1032: 		$datatext .= $/;
 1033: 	    }
 1034: 	    #   write file
 1035: 	    print $fh $datatext;
 1036: 	    close ($fh);
 1037: 	    #   generate gnuplot text
 1038: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
 1039: 		$curve->{'name'}.'" with '.
 1040: 		$curve->{'linestyle'};
 1041: 	}
 1042:     }
 1043:     # Write the output to a file.
 1044:     my $fh=Apache::File->new(">$tmpdir$filename");
 1045:     print $fh $gnuplot_input;
 1046:     close($fh);
 1047:     # That's all folks.
 1048:     return ;
 1049: }
 1050: 
 1051: #---------------------------------------------- check_inputs
 1052: sub check_inputs {
 1053:     ## Note: no inputs, no outputs - this acts only on global variables.
 1054:     ## Make sure we have all the input we need:
 1055:     if (! %plot) { &set_defaults(\%plot,\%gnuplot_defaults); }
 1056:     if (! %key ) {} # No key for this plot, thats okay
 1057: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
 1058:     if (! defined($title )) {} # No title for this plot, thats okay
 1059:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
 1060:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
 1061:     if ($#labels < 0) { }      # No labels for this plot, thats okay
 1062:     if ($#curves < 0) { 
 1063: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
 1064: 	return '';
 1065:     }
 1066:     my $curve;
 1067:     foreach $curve (@curves) {
 1068: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
 1069: 	    &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
 1070: 	    return '';
 1071: 	}
 1072:     }
 1073: }
 1074: 
 1075: #------------------------------------------------ make_edit
 1076: sub edit_attributes {
 1077:     my ($target,$token,$defaults,$keys) = @_;
 1078:     my ($result,@keys);
 1079:     if ($keys && ref($keys) eq 'ARRAY') {
 1080:         @keys = @$keys;
 1081:     } else {
 1082: 	@keys = sort(keys(%$defaults));
 1083:     }
 1084:     foreach my $attr (@keys) {
 1085: 	# append a ' ' to the description if it doesn't have one already.
 1086: 	my $description = $defaults->{$attr}->{'description'};
 1087: 	$description .= ' ' if ($description !~ / $/);
 1088: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
 1089: 	    $result .= &Apache::edit::text_arg
 1090: 		($description,$attr,$token,
 1091: 		 $defaults->{$attr}->{'size'});
 1092: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
 1093: 	    $result .= &Apache::edit::select_arg
 1094: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
 1095: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
 1096: 	    $result .= &Apache::edit::select_arg
 1097: 		($description,$attr,['on','off'],$token);
 1098: 	}
 1099: 	$result .= '<br />';
 1100:     }
 1101:     return $result;
 1102: }
 1103: 
 1104: 
 1105: ###################################################################
 1106: ##                                                               ##
 1107: ##           Insertion functions for editing plots               ##
 1108: ##                                                               ##
 1109: ###################################################################
 1110: 
 1111: sub insert_gnuplot {
 1112:     my $result = '';
 1113:     #  plot attributes
 1114:     $result .= "<gnuplot \n";
 1115:     foreach my $attr (keys(%gnuplot_defaults)) {
 1116: 	$result .= "     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"\n";
 1117:     }
 1118:     $result .= ">\n";
 1119:     # Add the components (most are commented out for simplicity)
 1120:     # $result .= &insert_key();
 1121:     # $result .= &insert_axis();
 1122:     # $result .= &insert_title();    
 1123:     # $result .= &insert_xlabel();    
 1124:     # $result .= &insert_ylabel();    
 1125:     $result .= &insert_curve();
 1126:     # close up the <gnuplot>
 1127:     $result .= "</gnuplot>\n";
 1128:     return $result;
 1129: }
 1130: 
 1131: sub insert_tics {
 1132:     my $result;
 1133:     $result .= &insert_xtics() . &insert_ytics;
 1134:     return $result;
 1135: }
 1136: 
 1137: sub insert_xtics {
 1138:     my $result;
 1139:     $result .= "\n    <xtics ";
 1140:     foreach my $attr (keys(%tic_defaults)) {
 1141: 	$result .= "$attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1142:     }
 1143:     $result .= "/>\n";
 1144:     return $result;
 1145: }
 1146: 
 1147: sub insert_ytics {
 1148:     my $result;
 1149:     $result .= "\n    <ytics ";
 1150:     foreach my $attr (keys(%tic_defaults)) {
 1151: 	$result .= "$attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1152:     }
 1153:     $result .= "/>\n";
 1154:     return $result;
 1155: }
 1156: 
 1157: sub insert_key {
 1158:     my $result;
 1159:     $result .= "\n    <key \n";
 1160:     foreach my $attr (keys(%key_defaults)) {
 1161: 	$result .= "         $attr=\"$key_defaults{$attr}->{'default'}\"\n";
 1162:     }
 1163:     $result .= "   />\n";
 1164:     return $result;
 1165: }
 1166: 
 1167: sub insert_axis{
 1168:     my $result;
 1169:     $result .= "\n    <axis ";
 1170:    foreach my $attr (keys(%axis_defaults)) {
 1171: 	$result .= "         $attr=\"$axis_defaults{$attr}->{'default'}\"\n";
 1172:     }
 1173:     $result .= "   />\n";
 1174:     return $result;
 1175: }
 1176: 
 1177: sub insert_title { return "\n    <title></title>\n"; }
 1178: sub insert_xlabel { return "\n    <xlabel></xlabel>\n"; }
 1179: sub insert_ylabel { return "\n    <ylabel></ylabel>\n"; }
 1180: 
 1181: sub insert_label {
 1182:     my $result;
 1183:     $result .= "\n    <label ";
 1184:     foreach my $attr (keys(%label_defaults)) {
 1185: 	$result .= '         '.$attr.'="'.
 1186: 	    $label_defaults{$attr}->{'default'}."\"\n";
 1187:     }
 1188:     $result .= "   ></label>\n";
 1189:     return $result;
 1190: }
 1191: 
 1192: sub insert_curve {
 1193:     my $result;
 1194:     $result .= "\n    <curve ";
 1195:     foreach my $attr (keys(%curve_defaults)) {
 1196: 	$result .= '         '.$attr.'="'.
 1197: 	    $curve_defaults{$attr}->{'default'}."\"\n";
 1198:     }
 1199:     $result .= "    >\n";
 1200:     $result .= &insert_data().&insert_data()."</curve>\n";
 1201: }
 1202: 
 1203: sub insert_function {
 1204:     my $result;
 1205:     $result .= "<function></function>\n";
 1206:     return $result;
 1207: }
 1208: 
 1209: sub insert_data {
 1210:     my $result;
 1211:     $result .= "     <data></data>\n";
 1212:     return $result;
 1213: }
 1214: 
 1215: ##----------------------------------------------------------------------
 1216: # Javascript functions to display help for tags
 1217: 
 1218: sub make_javascript {
 1219:     my $helpwindowwidth  = 400;
 1220:     my $helpwindowheight = 400;
 1221:     my $result = '';
 1222:     $result.=<<"ENDFUNCTION";
 1223: <script language="JavaScript">
 1224: function openWin(text)
 1225: {
 1226:   newWin = open("", "new_W", "width=$helpwindowwidth,height=$helpwindowheight,resizable=1,scrollbars=1");
 1227:   newWin.document.open("text/html", "replace");
 1228:   newWin.document.writeln(text);
 1229:   newWin.document.writeln('<center><a href=\"javascript:window.close()\">close this window</a></center>');
 1230:   newWin.document.close();
 1231: }
 1232: </script>
 1233: ENDFUNCTION
 1234:     return $result;
 1235: }
 1236: 
 1237: sub help_win {
 1238:     my ($helptext)=@_;
 1239:     $helptext =~ s/\n/ /g;
 1240:     $helptext =~ s/\'/\\\'/g;
 1241:     my $result = '';
 1242:     $result.=<<"ENDWIN";
 1243: <table width="100%"><tr><td align="right">
 1244: <a href="javascript:openWin('$helptext')">help</a>
 1245: </td></tr></table><hr />
 1246: ENDWIN
 1247:     return $result;
 1248: }
 1249: ##----------------------------------------------------------------------
 1250: 1;
 1251: __END__
 1252: 
 1253: 

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