File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.55: download - view: text, annotated - select for diffs
Wed Mar 6 19:55:30 2002 UTC (22 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Minor change to help text for plot, at the request of Ed Kashy.

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

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