File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.66: download - view: text, annotated - select for diffs
Wed Apr 24 16:38:40 2002 UTC (22 years ago) by matthew
Branches: MAIN
CVS tags: HEAD
Minor change.
specify location of output file.

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

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