File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.118: download - view: text, annotated - select for diffs
Thu Jun 7 23:42:14 2007 UTC (16 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: version_2_5_X, version_2_5_0, version_2_4_99_0, HEAD
- BUG#5288

    1: # The LearningOnline Network with CAPA
    2: # Dynamic plot
    3: #
    4: # $Id: lonplot.pm,v 1.118 2007/06/07 23:42:14 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: 
   29: package Apache::lonplot;
   30: 
   31: use strict;
   32: use warnings FATAL=>'all';
   33: no warnings 'uninitialized';
   34: use Apache::File;
   35: use Apache::response;
   36: use Apache::lonxml;
   37: use Apache::edit;
   38: use Apache::lonnet;
   39: use lib '/home/httpd/lib/perl/';
   40: use LONCAPA;
   41:  
   42: 
   43: use vars qw/$weboutputformat $versionstring/;
   44: 
   45: 
   46: 
   47: BEGIN {
   48:     &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
   49:     #
   50:     # Determine the version of GNUPLOT
   51:     $weboutputformat = 'gif';
   52:     $versionstring = `gnuplot --version 2>/dev/null`;
   53:     if ($versionstring =~ /^gnuplot 4/) {
   54:         $weboutputformat = 'png';
   55:     }
   56:     
   57: }
   58: 
   59: 
   60: ## 
   61: ## Description of data structures:
   62: ##
   63: ##  %plot       %key    %axis
   64: ## --------------------------
   65: ##  height      title   color
   66: ##  width       box     xmin
   67: ##  bgcolor     pos     xmax
   68: ##  fgcolor             ymin
   69: ##  transparent         ymax
   70: ##  grid
   71: ##  border
   72: ##  font
   73: ##  align
   74: ##
   75: ##  @labels: $labels[$i] = \%label
   76: ##           %label: text, xpos, ypos, justify
   77: ##
   78: ##  @curves: $curves[$i] = \%curve
   79: ##           %curve: name, linestyle, ( function | data )
   80: ##
   81: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
   82: ##                            [y1,y2,y3,y4] ]
   83: ##
   84: 
   85: ###################################################################
   86: ##                                                               ##
   87: ##        Tests used in checking the validitity of input         ##
   88: ##                                                               ##
   89: ###################################################################
   90: 
   91: my $max_str_len = 50;    # if a label, title, xlabel, or ylabel text
   92:                          # is longer than this, it will be truncated.
   93: 
   94: my %linestyles = 
   95:     (
   96:      lines          => 2,     # Maybe this will be used in the future
   97:      linespoints    => 2,     # to check on whether or not they have 
   98:      dots	    => 2,     # supplied enough <data></data> fields
   99:      points         => 2,     # to use the given line style.  But for
  100:      steps	    => 2,     # now there are more important things 
  101:      fsteps	    => 2,     # for me to deal with.
  102:      histeps        => 2,
  103:      errorbars	    => 3,
  104:      xerrorbars	    => [3,4],
  105:      yerrorbars	    => [3,4],
  106:      xyerrorbars    => [4,6],
  107:      boxes          => 3,
  108:      filledcurves   => 2,
  109:      vector	    => 4
  110:     );		    
  111: 
  112: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
  113: my $real_test      = 
  114:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
  115: my $pos_real_test  =
  116:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
  117: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-fA-F]{6}$/};
  118: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
  119: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
  120: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
  121: my $linestyle_test = sub {exists($linestyles{$_[0]})};
  122: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w~!\@\#\$\%^&\*\(\)-=_\+\[\]\{\}:\;\'<>,\.\/\?\\]+ ?)+$/};
  123: 
  124: ###################################################################
  125: ##                                                               ##
  126: ##                      Attribute metadata                       ##
  127: ##                                                               ##
  128: ###################################################################
  129: my @gnuplot_edit_order = 
  130:     qw/alttag bgcolor fgcolor height width font transparent grid samples 
  131:     border align texwidth texfont plotcolor plottype gridtype lmargin rmargin
  132:     tmargin bmargin major_ticscale minor_ticscale boxwidth gridlayer fillstyle
  133:     pattern solid/;
  134: 
  135: my $margin_choices = ['default',0..20];
  136: 
  137: my %gnuplot_defaults = 
  138:     (
  139:      alttag       => {
  140: 	 default     => 'dynamically generated plot',
  141: 	 test        => $words_test,
  142: 	 description => 'brief description of the plot',
  143:       	 edit_type   => 'entry',
  144: 	 size        => '40'
  145: 	 },
  146:      height       => {
  147: 	 default     => 300,
  148: 	 test        => $int_test,
  149: 	 description => 'height of image (pixels)',
  150:       	 edit_type   => 'entry',
  151: 	 size        => '10'
  152: 	 },
  153:      width        => {
  154: 	 default     => 400,
  155: 	 test        => $int_test,
  156: 	 description => 'width of image (pixels)',
  157: 	 edit_type   => 'entry',
  158: 	 size        => '10'
  159: 	 },
  160:      bgcolor      => {
  161: 	 default     => 'xffffff',
  162: 	 test        => $color_test, 
  163: 	 description => 'background color of image (xffffff)',
  164: 	 edit_type   => 'entry',
  165: 	 size        => '10'
  166: 	 },
  167:      fgcolor      => {
  168: 	 default     => 'x000000',
  169: 	 test        => $color_test,
  170: 	 description => 'foreground color of image (x000000)',
  171: 	 edit_type   => 'entry',
  172: 	 size        => '10'
  173: 	 },
  174:      transparent  => {
  175: 	 default     => 'off',
  176: 	 test        => $onoff_test, 
  177: 	 description => 'Transparent image',
  178: 	 edit_type   => 'onoff'
  179: 	 },
  180:      grid         => {
  181: 	 default     => 'on',
  182: 	 test        => $onoff_test, 
  183: 	 description => 'Display grid',
  184: 	 edit_type   => 'onoff'
  185: 	 },
  186:      gridlayer    => {
  187: 	 default     => 'off',
  188: 	 test        => $onoff_test, 
  189: 	 description => 'Display grid front layer over filled boxes or filled curves',
  190: 	 edit_type   => 'onoff'
  191: 	 },
  192:      box_border   => {
  193: 	 default     => 'noborder',
  194: 	 test        => sub {$_[0]=~/^(noborder|border)$/},
  195: 	 description => 'Draw border for boxes',
  196: 	 edit_type   => 'choice',
  197: 	 choices     => ['border','noborder']
  198: 	 },
  199:      border       => {
  200: 	 default     => 'on',
  201: 	 test        => $onoff_test, 
  202: 	 description => 'Draw border around plot',
  203: 	 edit_type   => 'onoff'
  204: 	 },
  205:      font         => {
  206: 	 default     => 'medium',
  207: 	 test        => $sml_test,
  208: 	 description => 'Size of font to use',
  209: 	 edit_type   => 'choice',
  210: 	 choices     => ['small','medium','large']
  211: 	 },
  212:      samples      => {
  213: 	 default     => '100',
  214: 	 test        => $int_test,
  215: 	 description => 'Number of samples for non-data plots',
  216: 	 edit_type   => 'choice',
  217: 	 choices     => ['100','200','500','1000','2000','5000']
  218: 	 },
  219:      align        => {
  220: 	 default     => 'middle',
  221: 	 test        => sub {$_[0]=~/^(left|right|middle|center)$/},
  222: 	 description => 'alignment for image in html',
  223: 	 edit_type   => 'choice',
  224: 	 choices     => ['left','right','middle']
  225: 	 },
  226:      texwidth     => {
  227:          default     => '93',
  228:          test        => $int_test,
  229:          description => 'Width of plot when printed (mm)',
  230:          edit_type   => 'entry',
  231:          size        => '5'
  232:          },
  233:      texfont      => {
  234:          default     => '22',
  235:          test        => $int_test,
  236:          description => 'Font size to use in TeX output (pts):',
  237:          edit_type   => 'choice',
  238:          choices     => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
  239:          },
  240:      plotcolor    => {
  241:          default     => 'monochrome',
  242:          test        => sub {$_[0]=~/^(monochrome|color|colour)$/},
  243:          description => 'Color setting for printing:',
  244:          edit_type   => 'choice',
  245:          choices     => [qw/monochrome color colour/],
  246:          },
  247:      pattern      => {
  248: 	 default     => '',
  249: 	 test        => $int_test,
  250: 	 description => 'pattern value for boxes:',
  251: 	 edit_type   => 'choice',
  252:          choices     => [0,1,2,3,4,5,6]
  253:          },
  254:      solid        => {
  255:          default     => 0,
  256:          test        => $real_test,
  257:          description => 'The density of fill style for boxes',
  258:          edit_type   => 'entry',
  259:          size        => '5'
  260:          },
  261:      fillstyle    => {
  262: 	 default     => 'empty',
  263: 	 test        => sub {$_[0]=~/^(empty|solid|pattern)$/},
  264: 	 description => 'Filled style for boxes:',
  265: 	 edit_type   => 'choice',
  266:          choices     => ['empty','solid','pattern']
  267:          },
  268:      plottype     => {
  269: 	 default     => 'Cartesian',
  270: 	 test        => sub {$_[0]=~/^(Polar|Cartesian)$/},
  271: 	 description => 'Plot type:',
  272: 	 edit_type   => 'choice',
  273:          choices     => ['Cartesian','Polar']
  274:          },
  275:      gridtype     => {
  276: 	 default     => 'Cartesian',
  277: 	 test        => sub {$_[0]=~/^(Polar|Cartesian|Linear-Log|Log-Linear|Log-Log)$/},
  278: 	 description => 'Grid type:',
  279: 	 edit_type   => 'choice',
  280:          choices     => ['Cartesian','Polar','Linear-Log','Log-Linear','Log-Log']
  281:          },
  282:      lmargin      => {
  283: 	 default     => 'default',
  284: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  285: 	 description => 'Left margin width (pts):',
  286: 	 edit_type   => 'choice',
  287:          choices     => $margin_choices,
  288:          },
  289:      rmargin      => {
  290: 	 default     => 'default',
  291: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  292: 	 description => 'Right margin width (pts):',
  293: 	 edit_type   => 'choice',
  294:          choices     => $margin_choices,
  295:          },
  296:      tmargin      => {
  297: 	 default     => 'default',
  298: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  299: 	 description => 'Top margin width (pts):',
  300: 	 edit_type   => 'choice',
  301:          choices     => $margin_choices,
  302:          },
  303:      bmargin      => {
  304: 	 default     => 'default',
  305: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  306: 	 description => 'Bottom margin width (pts):',
  307: 	 edit_type   => 'choice',
  308:          choices     => $margin_choices,
  309:          },
  310:      boxwidth     => {
  311: 	 default     => '',
  312: 	 test        => $real_test, 
  313: 	 description => 'width of boxes default auto',
  314: 	 edit_type   => 'entry',
  315:          size        => '5'
  316:          },
  317:      major_ticscale  => {
  318:          default     => '1',
  319:          test        => $real_test,
  320:          description => 'Size of major tic marks (plot coordinates)',
  321:          edit_type   => 'entry',
  322:          size        => '5'
  323:          },
  324:      minor_ticscale  => {
  325:          default     => '0.5',
  326:          test        => $real_test,
  327:          description => 'Size of minor tic mark (plot coordinates)',
  328:          edit_type   => 'entry',
  329:          size        => '5'
  330:          },
  331:      );
  332: 
  333: my %key_defaults = 
  334:     (
  335:      title => { 
  336: 	 default => '',
  337: 	 test => $words_test,
  338: 	 description => 'Title of key',
  339: 	 edit_type   => 'entry',
  340: 	 size        => '40'
  341: 	 },
  342:      box   => { 
  343: 	 default => 'off',
  344: 	 test => $onoff_test,
  345: 	 description => 'Draw a box around the key?',
  346: 	 edit_type   => 'onoff'
  347: 	 },
  348:      pos   => { 
  349: 	 default => 'top right', 
  350: 	 test => $key_pos_test, 
  351: 	 description => 'position of the key on the plot',
  352: 	 edit_type   => 'choice',
  353: 	 choices     => ['top left','top right','bottom left','bottom right',
  354: 			 'outside','below']
  355: 	 }
  356:      );
  357: 
  358: my %label_defaults = 
  359:     (
  360:      xpos    => {
  361: 	 default => 0,
  362: 	 test => $real_test,
  363: 	 description => 'x position of label (graph coordinates)',
  364: 	 edit_type   => 'entry',
  365: 	 size        => '10'
  366: 	 },
  367:      ypos    => {
  368: 	 default => 0, 
  369: 	 test => $real_test,
  370: 	 description => 'y position of label (graph coordinates)',
  371: 	 edit_type   => 'entry',
  372: 	 size        => '10'
  373: 	 },
  374:      justify => {
  375: 	 default => 'left',    
  376: 	 test => sub {$_[0]=~/^(left|right|center)$/},
  377: 	 description => 'justification of the label text on the plot',
  378: 	 edit_type   => 'choice',
  379: 	 choices     => ['left','right','center']
  380:      }
  381:      );
  382: 
  383: my @tic_edit_order = ('location','mirror','start','increment','end',
  384:                       'minorfreq');
  385: my %tic_defaults =
  386:     (
  387:      location => {
  388: 	 default => 'border', 
  389: 	 test => sub {$_[0]=~/^(border|axis)$/},
  390: 	 description => 'Location of major tic marks',
  391: 	 edit_type   => 'choice',
  392: 	 choices     => ['border','axis']
  393: 	 },
  394:      mirror => {
  395: 	 default => 'on', 
  396: 	 test => $onoff_test,
  397: 	 description => 'mirror tics on opposite axis?',
  398: 	 edit_type   => 'onoff'
  399: 	 },
  400:      start => {
  401: 	 default => '-10.0',
  402: 	 test => $real_test,
  403: 	 description => 'Start major tics at',
  404: 	 edit_type   => 'entry',
  405: 	 size        => '10'
  406: 	 },
  407:      increment => {
  408: 	 default => '1.0',
  409: 	 test => $real_test,
  410: 	 description => 'Place a major tic every',
  411: 	 edit_type   => 'entry',
  412: 	 size        => '10'
  413: 	 },
  414:      end => {
  415: 	 default => ' 10.0',
  416: 	 test => $real_test,
  417: 	 description => 'Stop major tics at ',
  418: 	 edit_type   => 'entry',
  419: 	 size        => '10'
  420: 	 },
  421:      minorfreq => {
  422: 	 default => '0',
  423: 	 test => $int_test,
  424: 	 description => 'Number of minor tics per major tic mark',
  425: 	 edit_type   => 'entry',
  426: 	 size        => '10'
  427: 	 },         
  428:      );
  429: 
  430: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax');
  431: my %axis_defaults = 
  432:     (
  433:      color   => {
  434: 	 default => 'x000000', 
  435: 	 test => $color_test,
  436: 	 description => 'color of grid lines (x000000)',
  437: 	 edit_type   => 'entry',
  438: 	 size        => '10'
  439: 	 },
  440:      xmin      => {
  441: 	 default => '-10.0',
  442: 	 test => $real_test,
  443: 	 description => 'minimum x-value shown in plot',
  444: 	 edit_type   => 'entry',
  445: 	 size        => '10'
  446: 	 },
  447:      xmax      => {
  448: 	 default => ' 10.0',
  449: 	 test => $real_test,
  450: 	 description => 'maximum x-value shown in plot',	 
  451: 	 edit_type   => 'entry',
  452: 	 size        => '10'
  453: 	 },
  454:      ymin      => {
  455: 	 default => '-10.0',
  456: 	 test => $real_test,
  457: 	 description => 'minimum y-value shown in plot',	 
  458: 	 edit_type   => 'entry',
  459: 	 size        => '10'
  460: 	 },
  461:      ymax      => {
  462: 	 default => ' 10.0',
  463: 	 test => $real_test,
  464: 	 description => 'maximum y-value shown in plot',	 
  465: 	 edit_type   => 'entry',
  466: 	 size        => '10'
  467: 	 }
  468:      );
  469: 
  470: my @curve_edit_order = ('color','name','linestyle','pointtype','pointsize','limit');
  471: 
  472: my %curve_defaults = 
  473:     (
  474:      color     => {
  475: 	 default => 'x000000',
  476: 	 test => $color_test,
  477: 	 description => 'color of curve (x000000)',
  478: 	 edit_type   => 'entry',
  479: 	 size        => '10'
  480: 	 },
  481:      name      => {
  482: 	 default => '',
  483: 	 test => $words_test,
  484: 	 description => 'name of curve to appear in key',
  485: 	 edit_type   => 'entry',
  486: 	 size        => '20'
  487: 	 },
  488:      linestyle => {
  489: 	 default => 'lines',
  490: 	 test => $linestyle_test,
  491: 	 description => 'Line style',
  492: 	 edit_type   => 'choice',
  493: 	 choices     => [keys(%linestyles)]
  494: 	 },
  495: # gnuplots term=gif driver does not handle linewidth :(
  496: #     linewidth => {
  497: #         default     => 1,
  498: #         test        => $int_test,
  499: #         description => 'Line width (may not apply to all line styles)',
  500: #         edit_type   => 'choice',
  501: #         choices     => [1,2,3,4,5,6,7,8,9,10]
  502: #         },
  503:      pointsize => {
  504:          default     => 1,
  505:          test        => $pos_real_test,
  506:          description => 'point size (may not apply to all line styles)',
  507:          edit_type   => 'entry',
  508:          size        => '5'
  509:          },
  510:      pointtype => {
  511:          default     => 1,
  512:          test        => $int_test,
  513:          description => 'point type (may not apply to all line styles)',
  514:          edit_type   => 'choice',
  515:          choices     => [0,1,2,3,4,5,6]
  516:          },
  517:      limit     => {
  518:          default     => 'closed',
  519: 	 test        => sub {$_[0]=~/^(closed|x1|x2|y1|y2)$/},
  520:          description => 'point to fill -- for filledcurves',
  521:          edit_type   => 'choice',
  522:          choices     => ['closed','x1','x2','y1','y2']
  523:          },
  524:      );
  525: 
  526: ###################################################################
  527: ##                                                               ##
  528: ##                    parsing and edit rendering                 ##
  529: ##                                                               ##
  530: ###################################################################
  531: 
  532: undef %Apache::lonplot::plot;
  533: my (%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
  534: 
  535: sub start_gnuplot {
  536:     undef(%Apache::lonplot::plot);   undef(%key);    undef(%axis);
  537:     undef($title);  undef($xlabel); undef($ylabel);
  538:     undef(@labels); undef(@curves);
  539:     undef(%xtics);  undef(%ytics);
  540:     #
  541:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  542:     my $result='';
  543:     &Apache::lonxml::register('Apache::lonplot',
  544: 	     ('title','xlabel','ylabel','key','axis','label','curve',
  545: 	      'xtics','ytics'));
  546:     push (@Apache::lonxml::namespace,'lonplot');
  547:     if ($target eq 'web' || $target eq 'tex') {
  548: 	&get_attributes(\%Apache::lonplot::plot,\%gnuplot_defaults,$parstack,$safeeval,
  549: 			$tagstack->[-1]);
  550:     } elsif ($target eq 'edit') {
  551: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
  552: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
  553: 				    \@gnuplot_edit_order)
  554: 	    .&Apache::edit::end_row()
  555: 	    .&Apache::edit::start_spanning_row();
  556:     } elsif ($target eq 'modified') {
  557: 	my $constructtag=&Apache::edit::get_new_args
  558: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
  559: 	if ($constructtag) {
  560: 	    $result = &Apache::edit::rebuild_tag($token);
  561: 	}
  562:     }
  563:     return $result;
  564: }
  565: 
  566: sub end_gnuplot {
  567:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  568:     pop @Apache::lonxml::namespace;
  569:     &Apache::lonxml::deregister('Apache::lonplot',
  570: 	('title','xlabel','ylabel','key','axis','label','curve'));
  571:     my $result = '';
  572:     my $randnumber;
  573:     # need to call rand everytime start_script would evaluate, as the
  574:     # safe space rand number generator and the global rand generator 
  575:     # are not separate
  576:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  577: 	$target eq 'answer') {
  578:       $randnumber=int(rand(1000));
  579:     }
  580:     if ($target eq 'web' || $target eq 'tex') {
  581: 	&check_inputs(); # Make sure we have all the data we need
  582: 	##
  583: 	## Determine filename
  584: 	my $tmpdir = '/home/httpd/perl/tmp/';
  585: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
  586: 	    '_'.time.'_'.$$.$randnumber.'_plot';
  587: 	## Write the plot description to the file
  588: 	&write_gnuplot_file($tmpdir,$filename,$target);
  589: 	$filename = &escape($filename);
  590: 	## return image tag for the plot
  591: 	if ($target eq 'web') {
  592: 	    $result .= <<"ENDIMAGE";
  593: <img src    = "/cgi-bin/plot.$weboutputformat?file=$filename.data" 
  594:      width  = "$Apache::lonplot::plot{'width'}"
  595:      height = "$Apache::lonplot::plot{'height'}"
  596:      align  = "$Apache::lonplot::plot{'align'}"
  597:      alt    = "$Apache::lonplot::plot{'alttag'}" />
  598: ENDIMAGE
  599:         } elsif ($target eq 'tex') {
  600: 	    &Apache::lonxml::debug(" gnuplot wid = $Apache::lonplot::plot{'width'}");
  601: 	    &Apache::lonxml::debug(" gnuplot ht  = $Apache::lonplot::plot{'height'}");
  602: 	    #might be inside the safe space, register the URL for later
  603: 	    &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
  604: 	    $result  = "%DYNAMICIMAGE:$Apache::lonplot::plot{'width'}:$Apache::lonplot::plot{'height'}:$Apache::lonplot::plot{'texwidth'}\n";
  605: 	    $result .= '\graphicspath{{/home/httpd/perl/tmp/}}'."\n";
  606: 	    $result .= '\includegraphics[width='.$Apache::lonplot::plot{'texwidth'}.' mm]{'.&unescape($filename).'.eps}';
  607: 	}
  608:     } elsif ($target eq 'edit') {
  609: 	$result.=&Apache::edit::tag_end($target,$token);
  610:     }
  611:     return $result;
  612: }
  613: 
  614: 
  615: ##--------------------------------------------------------------- xtics
  616: sub start_xtics {
  617:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  618:     my $result='';
  619:     if ($target eq 'web' || $target eq 'tex') {
  620: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
  621: 		    $tagstack->[-1]);
  622:     } elsif ($target eq 'edit') {
  623: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
  624: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  625: 				    \@tic_edit_order);
  626:     } elsif ($target eq 'modified') {
  627: 	my $constructtag=&Apache::edit::get_new_args
  628: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  629: 	if ($constructtag) {
  630: 	    $result = &Apache::edit::rebuild_tag($token);
  631: 	}
  632:     }
  633:     return $result;
  634: }
  635: 
  636: sub end_xtics {
  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: 
  646: ##--------------------------------------------------------------- ytics
  647: sub start_ytics {
  648:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  649:     my $result='';
  650:     if ($target eq 'web' || $target eq 'tex') {
  651: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
  652: 		    $tagstack->[-1]);
  653:     } elsif ($target eq 'edit') {
  654: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
  655: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  656: 				    \@tic_edit_order);
  657:     } elsif ($target eq 'modified') {
  658: 	my $constructtag=&Apache::edit::get_new_args
  659: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  660: 	if ($constructtag) {
  661: 	    $result = &Apache::edit::rebuild_tag($token);
  662: 	}
  663:     }
  664:     return $result;
  665: }
  666: 
  667: sub end_ytics {
  668:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  669:     my $result = '';
  670:     if ($target eq 'web' || $target eq 'tex') {
  671:     } elsif ($target eq 'edit') {
  672: 	$result.=&Apache::edit::tag_end($target,$token);
  673:     }
  674:     return $result;
  675: }
  676: 
  677: 
  678: ##----------------------------------------------------------------- key
  679: sub start_key {
  680:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  681:     my $result='';
  682:     if ($target eq 'web' || $target eq 'tex') {
  683: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
  684: 		    $tagstack->[-1]);
  685:     } elsif ($target eq 'edit') {
  686: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
  687: 	$result .= &edit_attributes($target,$token,\%key_defaults);
  688:     } elsif ($target eq 'modified') {
  689: 	my $constructtag=&Apache::edit::get_new_args
  690: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
  691: 	if ($constructtag) {
  692: 	    $result = &Apache::edit::rebuild_tag($token);
  693: 	}
  694:     }
  695:     return $result;
  696: }
  697: 
  698: sub end_key {
  699:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  700:     my $result = '';
  701:     if ($target eq 'web' || $target eq 'tex') {
  702:     } elsif ($target eq 'edit') {
  703: 	$result.=&Apache::edit::tag_end($target,$token);
  704:     }
  705:     return $result;
  706: }
  707: 
  708: ##------------------------------------------------------------------- title
  709: sub start_title {
  710:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  711:     my $result='';
  712:     if ($target eq 'web' || $target eq 'tex') {
  713: 	$title = &Apache::lonxml::get_all_text("/title",$parser,$style);
  714: 	$title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
  715: 	$title =~ s/\n/ /g;
  716: 	if (length($title) > $max_str_len) {
  717: 	    $title = substr($title,0,$max_str_len);
  718: 	}
  719:     } elsif ($target eq 'edit') {
  720: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
  721: 	my $text=&Apache::lonxml::get_all_text("/title",$parser,$style);
  722: 	$result.=&Apache::edit::editline('',$text,'',60);
  723:     } elsif ($target eq 'modified') {
  724: 	$result.=&Apache::edit::rebuild_tag($token);
  725: 	$result.=&Apache::edit::modifiedfield("/title",$parser);
  726:     }
  727:     return $result;
  728: }
  729: 
  730: sub end_title {
  731:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  732:     my $result = '';
  733:     if ($target eq 'web' || $target eq 'tex') {
  734:     } elsif ($target eq 'edit') {
  735: 	$result.=&Apache::edit::tag_end($target,$token);
  736:     }
  737:     return $result;
  738: }
  739: ##------------------------------------------------------------------- xlabel
  740: sub start_xlabel {
  741:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  742:     my $result='';
  743:     if ($target eq 'web' || $target eq 'tex') {
  744: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser,$style);
  745: 	$xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
  746: 	$xlabel =~ s/\n/ /g;
  747: 	if (length($xlabel) > $max_str_len) {
  748: 	    $xlabel = substr($xlabel,0,$max_str_len);
  749: 	}
  750:     } elsif ($target eq 'edit') {
  751: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
  752: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$parser,$style);
  753: 	$result.=&Apache::edit::editline('',$text,'',60);
  754:     } elsif ($target eq 'modified') {
  755: 	$result.=&Apache::edit::rebuild_tag($token);	
  756: 	$result.=&Apache::edit::modifiedfield("/xlabel",$parser);
  757:     }
  758:     return $result;
  759: }
  760: 
  761: sub end_xlabel {
  762:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  763:     my $result = '';
  764:     if ($target eq 'web' || $target eq 'tex') {
  765:     } elsif ($target eq 'edit') {
  766: 	$result.=&Apache::edit::tag_end($target,$token);
  767:     }
  768:     return $result;
  769: }
  770: 
  771: ##------------------------------------------------------------------- ylabel
  772: sub start_ylabel {
  773:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  774:     my $result='';
  775:     if ($target eq 'web' || $target eq 'tex') {
  776: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
  777: 	$ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
  778: 	$ylabel =~ s/\n/ /g;
  779: 	if (length($ylabel) > $max_str_len) {
  780: 	    $ylabel = substr($ylabel,0,$max_str_len);
  781: 	}
  782:     } elsif ($target eq 'edit') {
  783: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
  784: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
  785: 	$result .= &Apache::edit::editline('',$text,'',60);
  786:     } elsif ($target eq 'modified') {
  787: 	$result.=&Apache::edit::rebuild_tag($token);
  788: 	$result.=&Apache::edit::modifiedfield("/ylabel",$parser);
  789:     }
  790:     return $result;
  791: }
  792: 
  793: sub end_ylabel {
  794:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  795:     my $result = '';
  796:     if ($target eq 'web' || $target eq 'tex') {
  797:     } elsif ($target eq 'edit') {
  798: 	$result.=&Apache::edit::tag_end($target,$token);
  799:     }
  800:     return $result;
  801: }
  802: 
  803: ##------------------------------------------------------------------- label
  804: sub start_label {
  805:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  806:     my $result='';
  807:     if ($target eq 'web' || $target eq 'tex') {
  808: 	my %label;
  809: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
  810: 		    $tagstack->[-1]);
  811: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
  812: 	$text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
  813: 	$text =~ s/\n/ /g;
  814: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
  815: 	$label{'text'} = $text;
  816: 	push(@labels,\%label);
  817:     } elsif ($target eq 'edit') {
  818: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
  819: 	$result .= &edit_attributes($target,$token,\%label_defaults);
  820: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
  821: 	$result .= &Apache::edit::end_row().
  822: 	    &Apache::edit::start_spanning_row().
  823: 	    &Apache::edit::editline('',$text,'',60);
  824:     } elsif ($target eq 'modified') {
  825: 	&Apache::edit::get_new_args
  826: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
  827: 	$result.=&Apache::edit::rebuild_tag($token);
  828: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
  829:     }
  830:     return $result;
  831: }
  832: 
  833: sub end_label {
  834:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  835:     my $result = '';
  836:     if ($target eq 'web' || $target eq 'tex') {
  837:     } elsif ($target eq 'edit') {
  838: 	$result.=&Apache::edit::tag_end($target,$token);
  839:     }
  840:     return $result;
  841: }
  842: 
  843: ##------------------------------------------------------------------- curve
  844: sub start_curve {
  845:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  846:     my $result='';
  847:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
  848:     push (@Apache::lonxml::namespace,'curve');
  849:     if ($target eq 'web' || $target eq 'tex') {
  850: 	my %curve;
  851: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
  852: 		    $tagstack->[-1]);
  853: 	push (@curves,\%curve);
  854:     } elsif ($target eq 'edit') {
  855: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
  856: 	$result .= &edit_attributes($target,$token,\%curve_defaults,
  857:                                     \@curve_edit_order)
  858: 	    .&Apache::edit::end_row()
  859: 	    .&Apache::edit::start_spanning_row();
  860: 
  861:     } elsif ($target eq 'modified') {
  862: 	my $constructtag=&Apache::edit::get_new_args
  863: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
  864: 	if ($constructtag) {
  865: 	    $result = &Apache::edit::rebuild_tag($token);
  866: 	}
  867:     }
  868:     return $result;
  869: }
  870: 
  871: sub end_curve {
  872:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  873:     my $result = '';
  874:     pop @Apache::lonxml::namespace;
  875:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
  876:     if ($target eq 'web' || $target eq 'tex') {
  877:     } elsif ($target eq 'edit') {
  878: 	$result.=&Apache::edit::tag_end($target,$token);
  879:     }
  880:     return $result;
  881: }
  882: 
  883: ##------------------------------------------------------------ curve function
  884: sub start_function {
  885:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  886:     my $result='';
  887:     if ($target eq 'web' || $target eq 'tex') {
  888: 	if (exists($curves[-1]->{'data'})) {
  889: 	    &Apache::lonxml::warning
  890:                 ('Use of the <b>curve function</b> tag precludes use of '.
  891:                  ' the <b>curve data</b> tag.  '.
  892:                  'The curve data tag will be omitted in favor of the '.
  893:                  'curve function declaration.');
  894: 	    delete $curves[-1]->{'data'} ;
  895: 	}
  896:         my $function = &Apache::lonxml::get_all_text("/function",$parser,
  897: 						     $style);
  898: 	$function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
  899: 	$curves[-1]->{'function'} = $function; 
  900:     } elsif ($target eq 'edit') {
  901: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
  902: 	my $text = &Apache::lonxml::get_all_text("/function",$parser,$style);
  903: 	$result .= &Apache::edit::editline('',$text,'',60);
  904:     } elsif ($target eq 'modified') {
  905: 	$result.=&Apache::edit::rebuild_tag($token);
  906: 	$result.=&Apache::edit::modifiedfield("/function",$parser);
  907:     }
  908:     return $result;
  909: }
  910: 
  911: sub end_function {
  912:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  913:     my $result = '';
  914:     if ($target eq 'web' || $target eq 'tex') {
  915:     } elsif ($target eq 'edit') {
  916: 	$result .= &Apache::edit::end_table();
  917:     }
  918:     return $result;
  919: }
  920: 
  921: ##------------------------------------------------------------ curve  data
  922: sub start_data {
  923:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  924:     my $result='';
  925:     if ($target eq 'web' || $target eq 'tex') {
  926: 	if (exists($curves[-1]->{'function'})) {
  927: 	    &Apache::lonxml::warning
  928:                 ('Use of the <b>curve function</b> tag precludes use of '.
  929:                  ' the <b>curve data</b> tag.  '.
  930:                  'The curve function tag will be omitted in favor of the '.
  931:                  'curve data declaration.');
  932: 	    delete($curves[-1]->{'function'});
  933: 	}
  934: 	my $datatext = &Apache::lonxml::get_all_text("/data",$parser,$style);
  935: 	$datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
  936: 	# Deal with cases where we're given an array...
  937: 	if ($datatext =~ /^\@/) {
  938: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
  939: 					  $safeeval,1);
  940: 	}
  941: 	$datatext =~ s/\s+/ /g;
  942: 	# Need to do some error checking on the @data array - 
  943: 	# make sure it's all numbers and make sure each array 
  944: 	# is of the same length.
  945: 	my @data;
  946: 	if ($datatext =~ /,/) { # comma deliminated
  947: 	    @data = split /,/,$datatext;
  948: 	} else { # Assume it's space separated.
  949: 	    @data = split / /,$datatext;
  950: 	}
  951: 	for (my $i=0;$i<=$#data;$i++) {
  952: 	    # Check that it's non-empty
  953: 	    if (! defined($data[$i])) {
  954: 		&Apache::lonxml::warning(
  955: 		    'undefined curve data value.  Replacing with '.
  956: 		    ' pi/e = 1.15572734979092');
  957: 		$data[$i] = 1.15572734979092;
  958: 	    }
  959: 	    # Check that it's a number
  960: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
  961: 		&Apache::lonxml::warning(
  962: 		    'Bad curve data value of '.$data[$i].'  Replacing with '.
  963: 		    ' pi/e = 1.15572734979092');
  964: 		$data[$i] = 1.15572734979092;
  965: 	    }
  966: 	}
  967: 	# complain if the number of data points is not the same as
  968: 	# in previous sets of data.
  969: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
  970: 	    &Apache::lonxml::warning
  971: 		('Number of data points is not consistent with previous '.
  972: 		 'number of data points');
  973: 	}
  974: 	push  @{$curves[-1]->{'data'}},\@data;
  975:     } elsif ($target eq 'edit') {
  976: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
  977: 	my $text = &Apache::lonxml::get_all_text("/data",$parser,$style);
  978: 	$result .= &Apache::edit::editline('',$text,'',60);
  979:     } elsif ($target eq 'modified') {
  980: 	$result.=&Apache::edit::rebuild_tag($token);
  981: 	$result.=&Apache::edit::modifiedfield("/data",$parser);
  982:     }
  983:     return $result;
  984: }
  985: 
  986: sub end_data {
  987:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  988:     my $result = '';
  989:     if ($target eq 'web' || $target eq 'tex') {
  990:     } elsif ($target eq 'edit') {
  991: 	$result .= &Apache::edit::end_table();
  992:     }
  993:     return $result;
  994: }
  995: 
  996: ##------------------------------------------------------------------- axis
  997: sub start_axis {
  998:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  999:     my $result='';
 1000:     if ($target eq 'web' || $target eq 'tex') {
 1001: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
 1002: 			$tagstack->[-1]);
 1003:     } elsif ($target eq 'edit') {
 1004: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
 1005: 	$result .= &edit_attributes($target,$token,\%axis_defaults,
 1006: 				    \@axis_edit_order);
 1007:     } elsif ($target eq 'modified') {
 1008: 	my $constructtag=&Apache::edit::get_new_args
 1009: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
 1010: 	if ($constructtag) {
 1011: 	    $result = &Apache::edit::rebuild_tag($token);
 1012: 	}
 1013:     }
 1014:     return $result;
 1015: }
 1016: 
 1017: sub end_axis {
 1018:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1019:     my $result = '';
 1020:     if ($target eq 'web' || $target eq 'tex') {
 1021:     } elsif ($target eq 'edit') {
 1022: 	$result.=&Apache::edit::tag_end($target,$token);
 1023:     } elsif ($target eq 'modified') {
 1024:     }
 1025:     return $result;
 1026: }
 1027: 
 1028: ###################################################################
 1029: ##                                                               ##
 1030: ##        Utility Functions                                      ##
 1031: ##                                                               ##
 1032: ###################################################################
 1033: 
 1034: ##----------------------------------------------------------- set_defaults
 1035: sub set_defaults {
 1036:     my ($var,$defaults) = @_;
 1037:     my $key;
 1038:     foreach $key (keys(%$defaults)) {
 1039: 	$var->{$key} = $defaults->{$key}->{'default'};
 1040:     }
 1041: }
 1042: 
 1043: ##------------------------------------------------------------------- misc
 1044: sub get_attributes{
 1045:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
 1046:     foreach my $attr (keys(%{$defaults})) {
 1047: 	if ($attr eq 'texwidth' || $attr eq 'texfont') {
 1048: 	    $values->{$attr} = 
 1049: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
 1050: 	} else {
 1051: 	    $values->{$attr} = 
 1052: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval);
 1053: 	}
 1054: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
 1055: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
 1056: 	    next;
 1057: 	}
 1058: 	my $test = $defaults->{$attr}->{'test'};
 1059: 	if (! &$test($values->{$attr})) {
 1060: 	    &Apache::lonxml::warning
 1061: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
 1062: 		 .$defaults->{$attr}->{'default'} );
 1063: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
 1064: 	}
 1065:     }
 1066:     return ;
 1067: }
 1068: 
 1069: ##------------------------------------------------------- write_gnuplot_file
 1070: sub write_gnuplot_file {
 1071:     my ($tmpdir,$filename,$target)= @_;
 1072:     my $gnuplot_input = '';
 1073:     my $curve;
 1074:     my $pt = $Apache::lonplot::plot{'texfont'};
 1075:     #
 1076:     # Check to be sure we do not have any empty curves
 1077:     my @curvescopy;
 1078:     foreach my $curve (@curves) {
 1079:         if (exists($curve->{'function'})) {
 1080:             if ($curve->{'function'} !~ /^\s*$/) {
 1081:                 push(@curvescopy,$curve);
 1082:             }
 1083:         } elsif (exists($curve->{'data'})) {
 1084:             foreach my $data (@{$curve->{'data'}}) {
 1085:                 if (scalar(@$data) > 0) {
 1086:                     push(@curvescopy,$curve);
 1087:                     last;
 1088:                 }
 1089:             }
 1090:         }
 1091:     }
 1092:     @curves = @curvescopy;
 1093:     # Collect all the colors
 1094:     my @Colors;
 1095:     push @Colors, $Apache::lonplot::plot{'bgcolor'};
 1096:     push @Colors, $Apache::lonplot::plot{'fgcolor'}; 
 1097:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$Apache::lonplot::plot{'fgcolor'});
 1098:     foreach $curve (@curves) {
 1099: 	push @Colors, ($curve->{'color'} ne '' ? 
 1100: 		       $curve->{'color'}       : 
 1101: 		       $Apache::lonplot::plot{'fgcolor'}        );
 1102:     }
 1103:     # set term
 1104:     if ($target eq 'web') {
 1105: 	$gnuplot_input .= 'set term '.$weboutputformat .' ';
 1106: 	$gnuplot_input .= 'transparent ' if ($Apache::lonplot::plot{'transparent'} eq 'on');
 1107: 	$gnuplot_input .= $Apache::lonplot::plot{'font'} . ' ';
 1108: 	$gnuplot_input .= 'size '.$Apache::lonplot::plot{'width'}.','.$Apache::lonplot::plot{'height'}.' ';
 1109: 	$gnuplot_input .= "@Colors\n";
 1110: 	# set output
 1111: 	$gnuplot_input .= "set output\n";
 1112:     } elsif ($target eq 'tex') {
 1113: 	$gnuplot_input .= "set term postscript eps $Apache::lonplot::plot{'plotcolor'} solid \"Helvetica\" $pt \n";
 1114: 	$gnuplot_input .= "set output \"/home/httpd/perl/tmp/".
 1115: 	    &unescape($filename).".eps\"\n";
 1116:     }
 1117:     # cartesian or polar plot?
 1118:     if (lc($Apache::lonplot::plot{'plottype'}) eq 'polar') {
 1119:         $gnuplot_input .= 'set polar'.$/;
 1120:     } else {
 1121:         # Assume Cartesian
 1122:     }
 1123:     # cartesian or polar grid?
 1124:     if (lc($Apache::lonplot::plot{'gridtype'}) eq 'polar') {
 1125:         $gnuplot_input .= 'set grid polar'.$/;
 1126:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'linear-log') {
 1127:         $gnuplot_input .= 'set logscale x'.$/;
 1128:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-linear') {
 1129:         $gnuplot_input .= 'set logscale y'.$/;
 1130:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-log') {
 1131:         $gnuplot_input .= 'set logscale x'.$/;
 1132:         $gnuplot_input .= 'set logscale y'.$/;
 1133:     } else {
 1134:         # Assume Cartesian
 1135:     }
 1136:     # solid or pattern for boxes?
 1137:     if (lc($Apache::lonplot::plot{'fillstyle'}) eq 'solid') {
 1138:         $gnuplot_input .= 'set style fill solid '.
 1139: 	    $Apache::lonplot::plot{'solid'}.$Apache::lonplot::plot{'box_border'}.$/;
 1140:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'pattern') {
 1141:         $gnuplot_input .= 'set style fill pattern '.$Apache::lonplot::plot{'pattern'}.$Apache::lonplot::plot{'box_border'}.$/;
 1142:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'empty') {
 1143:     }
 1144:     # margin
 1145:     if (lc($Apache::lonplot::plot{'lmargin'}) ne 'default') {
 1146:         $gnuplot_input .= 'set lmargin '.$Apache::lonplot::plot{'lmargin'}.$/;
 1147:     }
 1148:     if (lc($Apache::lonplot::plot{'rmargin'}) ne 'default') {
 1149:         $gnuplot_input .= 'set rmargin '.$Apache::lonplot::plot{'rmargin'}.$/;
 1150:     }
 1151:     if (lc($Apache::lonplot::plot{'tmargin'}) ne 'default') {
 1152:         $gnuplot_input .= 'set tmargin '.$Apache::lonplot::plot{'tmargin'}.$/;
 1153:     }
 1154:     if (lc($Apache::lonplot::plot{'bmargin'}) ne 'default') {
 1155:         $gnuplot_input .= 'set bmargin '.$Apache::lonplot::plot{'bmargin'}.$/;
 1156:     }
 1157:     # tic scales
 1158:     $gnuplot_input .= 'set ticscale '.
 1159:         $Apache::lonplot::plot{'major_ticscale'}.' '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
 1160:     #boxwidth
 1161:     if (lc($Apache::lonplot::plot{'boxwidth'}) ne '') {
 1162: 	$gnuplot_input .= 'set boxwidth '.$Apache::lonplot::plot{'boxwidth'}.$/;
 1163:     }
 1164:     # gridlayer
 1165:     $gnuplot_input .= 'set grid noxtics noytics front '.$/ 
 1166: 	if ($Apache::lonplot::plot{'gridlayer'} eq 'on');
 1167: 
 1168:     # grid
 1169:     $gnuplot_input .= 'set grid'.$/ if ($Apache::lonplot::plot{'grid'} eq 'on');
 1170:     # border
 1171:     $gnuplot_input .= ($Apache::lonplot::plot{'border'} eq 'on'?
 1172: 		       'set border'.$/           :
 1173: 		       'set noborder'.$/         );
 1174:     # sampling rate for non-data curves
 1175:     $gnuplot_input .= "set samples $Apache::lonplot::plot{'samples'}\n";
 1176:     # title, xlabel, ylabel
 1177:     # titles
 1178:     if ($target eq 'tex') {
 1179:         $gnuplot_input .= "set title  \"$title\" font \"Helvetica,".$pt."pt\"\n"  if (defined($title)) ;
 1180:         $gnuplot_input .= "set xlabel \"$xlabel\" font \"Helvetica,".$pt."pt\" \n" if (defined($xlabel));
 1181:         $gnuplot_input .= "set ylabel \"$ylabel\" font \"Helvetica,".$pt."pt\"\n" if (defined($ylabel));
 1182:     } else {
 1183:         $gnuplot_input .= "set title  \"$title\"  \n"  if (defined($title)) ;
 1184:         $gnuplot_input .= "set xlabel \"$xlabel\" \n" if (defined($xlabel));
 1185:         $gnuplot_input .= "set ylabel \"$ylabel\" \n" if (defined($ylabel));
 1186:     }
 1187:     # tics
 1188:     if (%xtics) {    
 1189: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
 1190: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
 1191: 	$gnuplot_input .= "$xtics{'start'}, ";
 1192: 	$gnuplot_input .= "$xtics{'increment'}, ";
 1193: 	$gnuplot_input .= "$xtics{'end'}\n";
 1194:         if ($xtics{'minorfreq'} != 0) {
 1195:             $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
 1196:         } 
 1197:     }
 1198:     if (%ytics) {    
 1199: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
 1200: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
 1201: 	$gnuplot_input .= "$ytics{'start'}, ";
 1202: 	$gnuplot_input .= "$ytics{'increment'}, ";
 1203:         $gnuplot_input .= "$ytics{'end'}\n";
 1204:         if ($ytics{'minorfreq'} != 0) {
 1205:             $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
 1206:         } 
 1207:     }
 1208:     # axis
 1209:     if (%axis) {
 1210: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
 1211: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
 1212:     }
 1213:     # Key
 1214:     if (%key) {
 1215: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
 1216: 	if ($key{'title'} ne '') {
 1217: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
 1218: 	} 
 1219: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
 1220:     } else {
 1221: 	$gnuplot_input .= 'set nokey'.$/;
 1222:     }
 1223:     # labels
 1224:     my $label;
 1225:     foreach $label (@labels) {
 1226: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
 1227: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'};
 1228:         if ($target eq 'tex') {
 1229:             $gnuplot_input .=' font "Helvetica,'.$pt.'pt"' ;
 1230:         }
 1231:         $gnuplot_input .= $/;
 1232:     }
 1233:     if ($target eq 'tex') {
 1234:         $gnuplot_input .="set size 1,".$Apache::lonplot::plot{'height'}/$Apache::lonplot::plot{'width'}*1.38;
 1235:         $gnuplot_input .="\n";
 1236:         }
 1237:     # curves
 1238:     $gnuplot_input .= 'plot ';
 1239:     for (my $i = 0;$i<=$#curves;$i++) {
 1240: 	$curve = $curves[$i];
 1241: 	$gnuplot_input.= ', ' if ($i > 0);
 1242: 	if (exists($curve->{'function'})) {
 1243: 	    $gnuplot_input.= 
 1244: 		$curve->{'function'}.' title "'.
 1245: 		$curve->{'name'}.'" with '.
 1246:                 $curve->{'linestyle'};
 1247:             $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
 1248:             if (($curve->{'linestyle'} eq 'points')      ||
 1249:                 ($curve->{'linestyle'} eq 'linespoints') ||
 1250:                 ($curve->{'linestyle'} eq 'errorbars')   ||
 1251:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
 1252:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
 1253:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
 1254:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
 1255:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
 1256:             } elsif ($curve->{'linestyle'} eq 'filledcurves') { 
 1257:                 $gnuplot_input.= ' '.$curve->{'limit'};
 1258:             }
 1259: 	} elsif (exists($curve->{'data'})) {
 1260: 	    # Store data values in $datatext
 1261: 	    my $datatext = '';
 1262: 	    #   get new filename
 1263: 	    my $datafilename = "$tmpdir/$filename.data.$i";
 1264: 	    my $fh=Apache::File->new(">$datafilename");
 1265: 	    # Compile data
 1266: 	    my @Data = @{$curve->{'data'}};
 1267: 	    my @Data0 = @{$Data[0]};
 1268: 	    for (my $i =0; $i<=$#Data0; $i++) {
 1269: 		my $dataset;
 1270: 		foreach $dataset (@Data) {
 1271: 		    $datatext .= $dataset->[$i] . ' ';
 1272: 		}
 1273: 		$datatext .= $/;
 1274: 	    }
 1275: 	    #   write file
 1276: 	    print $fh $datatext;
 1277: 	    close ($fh);
 1278: 	    #   generate gnuplot text
 1279: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
 1280: 		$curve->{'name'}.'" with '.
 1281: 		$curve->{'linestyle'};
 1282:             $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
 1283:             if (($curve->{'linestyle'} eq 'points')      ||
 1284:                 ($curve->{'linestyle'} eq 'linespoints') ||
 1285:                 ($curve->{'linestyle'} eq 'errorbars')   ||
 1286:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
 1287:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
 1288:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
 1289:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
 1290:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
 1291:             } elsif ($curve->{'linestyle'} eq 'filledcurves') { 
 1292:                 $gnuplot_input.= ' '.$curve->{'limit'};
 1293:             }
 1294: 	}
 1295:     }
 1296:     # Write the output to a file.
 1297:     my $fh=Apache::File->new(">$tmpdir$filename.data");
 1298:     print $fh $gnuplot_input;
 1299:     close($fh);
 1300:     # That's all folks.
 1301:     return ;
 1302: }
 1303: 
 1304: #---------------------------------------------- check_inputs
 1305: sub check_inputs {
 1306:     ## Note: no inputs, no outputs - this acts only on global variables.
 1307:     ## Make sure we have all the input we need:
 1308:     if (! %Apache::lonplot::plot) { &set_defaults(\%Apache::lonplot::plot,\%gnuplot_defaults); }
 1309:     if (! %key ) {} # No key for this plot, thats okay
 1310: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
 1311:     if (! defined($title )) {} # No title for this plot, thats okay
 1312:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
 1313:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
 1314:     if ($#labels < 0) { }      # No labels for this plot, thats okay
 1315:     if ($#curves < 0) { 
 1316: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
 1317: 	return '';
 1318:     }
 1319:     my $curve;
 1320:     foreach $curve (@curves) {
 1321: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
 1322: 	    &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
 1323: 	    return '';
 1324: 	}
 1325:     }
 1326: }
 1327: 
 1328: #------------------------------------------------ make_edit
 1329: sub edit_attributes {
 1330:     my ($target,$token,$defaults,$keys) = @_;
 1331:     my ($result,@keys);
 1332:     if ($keys && ref($keys) eq 'ARRAY') {
 1333:         @keys = @$keys;
 1334:     } else {
 1335: 	@keys = sort(keys(%$defaults));
 1336:     }
 1337:     foreach my $attr (@keys) {
 1338: 	# append a ' ' to the description if it doesn't have one already.
 1339: 	my $description = $defaults->{$attr}->{'description'};
 1340: 	$description .= ' ' if ($description !~ / $/);
 1341: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
 1342: 	    $result .= &Apache::edit::text_arg
 1343: 		($description,$attr,$token,
 1344: 		 $defaults->{$attr}->{'size'});
 1345: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
 1346: 	    $result .= &Apache::edit::select_or_text_arg
 1347: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
 1348: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
 1349: 	    $result .= &Apache::edit::select_or_text_arg
 1350: 		($description,$attr,['on','off'],$token);
 1351: 	}
 1352: 	$result .= '<br />';
 1353:     }
 1354:     return $result;
 1355: }
 1356: 
 1357: 
 1358: ###################################################################
 1359: ##                                                               ##
 1360: ##           Insertion functions for editing plots               ##
 1361: ##                                                               ##
 1362: ###################################################################
 1363: 
 1364: sub insert_gnuplot {
 1365:     my $result = '';
 1366:     #  plot attributes
 1367:     $result .= "\n<gnuplot ";
 1368:     foreach my $attr (keys(%gnuplot_defaults)) {
 1369: 	$result .= "\n     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
 1370:     }
 1371:     $result .= ">";
 1372:     # Add the components (most are commented out for simplicity)
 1373:     # $result .= &insert_key();
 1374:     # $result .= &insert_axis();
 1375:     # $result .= &insert_title();    
 1376:     # $result .= &insert_xlabel();    
 1377:     # $result .= &insert_ylabel();    
 1378:     $result .= &insert_curve();
 1379:     # close up the <gnuplot>
 1380:     $result .= "\n</gnuplot>";
 1381:     return $result;
 1382: }
 1383: 
 1384: sub insert_tics {
 1385:     my $result;
 1386:     $result .= &insert_xtics() . &insert_ytics;
 1387:     return $result;
 1388: }
 1389: 
 1390: sub insert_xtics {
 1391:     my $result;
 1392:     $result .= "\n    <xtics ";
 1393:     foreach my $attr (keys(%tic_defaults)) {
 1394: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1395:     }
 1396:     $result .= "/>";
 1397:     return $result;
 1398: }
 1399: 
 1400: sub insert_ytics {
 1401:     my $result;
 1402:     $result .= "\n    <ytics ";
 1403:     foreach my $attr (keys(%tic_defaults)) {
 1404: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1405:     }
 1406:     $result .= "/>";
 1407:     return $result;
 1408: }
 1409: 
 1410: sub insert_key {
 1411:     my $result;
 1412:     $result .= "\n    <key ";
 1413:     foreach my $attr (keys(%key_defaults)) {
 1414: 	$result .= "\n         $attr=\"$key_defaults{$attr}->{'default'}\"";
 1415:     }
 1416:     $result .= " />";
 1417:     return $result;
 1418: }
 1419: 
 1420: sub insert_axis{
 1421:     my $result;
 1422:     $result .= "\n    <axis ";
 1423:    foreach my $attr (keys(%axis_defaults)) {
 1424: 	$result .= "\n         $attr=\"$axis_defaults{$attr}->{'default'}\"";
 1425:     }
 1426:     $result .= " />";
 1427:     return $result;
 1428: }
 1429: 
 1430: sub insert_title  { return "\n    <title></title>"; }
 1431: sub insert_xlabel { return "\n    <xlabel></xlabel>"; }
 1432: sub insert_ylabel { return "\n    <ylabel></ylabel>"; }
 1433: 
 1434: sub insert_label {
 1435:     my $result;
 1436:     $result .= "\n    <label ";
 1437:     foreach my $attr (keys(%label_defaults)) {
 1438: 	$result .= "\n         $attr=\"".
 1439:             $label_defaults{$attr}->{'default'}."\"";
 1440:     }
 1441:     $result .= "></label>";
 1442:     return $result;
 1443: }
 1444: 
 1445: sub insert_curve {
 1446:     my $result;
 1447:     $result .= "\n    <curve ";
 1448:     foreach my $attr (keys(%curve_defaults)) {
 1449: 	$result .= "\n         $attr=\"".
 1450: 	    $curve_defaults{$attr}->{'default'}."\"";
 1451:     }
 1452:     $result .= " >";
 1453:     $result .= &insert_data().&insert_data()."\n    </curve>";
 1454: }
 1455: 
 1456: sub insert_function {
 1457:     my $result;
 1458:     $result .= "\n        <function></function>";
 1459:     return $result;
 1460: }
 1461: 
 1462: sub insert_data {
 1463:     my $result;
 1464:     $result .= "\n        <data></data>";
 1465:     return $result;
 1466: }
 1467: 
 1468: ##----------------------------------------------------------------------
 1469: 1;
 1470: __END__
 1471: 
 1472: 

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