File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.61: download - view: text, annotated - select for diffs
Fri Mar 22 16:04:09 2002 UTC (22 years, 2 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Minor cleanup of insert functions to make the xml a little neater.

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

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