File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.81: download - view: text, annotated - select for diffs
Fri Feb 7 22:03:21 2003 UTC (21 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- get_all_text() really wants to see the entire stack of parsers now, that way style redefinitons of tags can do a get_all_text across styl def boundaries.

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

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