File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.134: download - view: text, annotated - select for diffs
Sun Jan 20 02:47:06 2008 UTC (16 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_6_2, version_2_6_1, HEAD
Phil's addition of a rotation option for plot labels.

    1: # The LearningOnline Network with CAPA
    2: # Dynamic plot
    3: #
    4: # $Id: lonplot.pm,v 1.134 2008/01/20 02:47:06 raeburn 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 LONCAPA;
   40:  
   41: 
   42: use vars qw/$weboutputformat $version/;
   43: 
   44: 
   45: 
   46: BEGIN {
   47:     &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
   48:     #
   49:     # Determine the version of GNUPLOT
   50:     $weboutputformat = 'gif';
   51:     my $versionstring = `gnuplot --version 2>/dev/null`;
   52:     ($version) = ($versionstring =~ /^gnuplot ([\d.]+)/);
   53:     if ($version >= 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]=~/^(\d+|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 texwidth fontface font texfont
  131:     transparent grid samples 
  132:     border align plotcolor plottype gridtype lmargin rmargin
  133:     tmargin bmargin major_ticscale minor_ticscale boxwidth gridlayer fillstyle
  134:     pattern solid/;
  135: 
  136: my $margin_choices = ['default',0..20];
  137: 
  138: my %gnuplot_defaults = 
  139:     (
  140:      alttag       => {
  141: 	 default     => 'dynamically generated plot',
  142: 	 test        => $words_test,
  143: 	 description => 'Brief description of the plot',
  144:       	 edit_type   => 'entry',
  145: 	 size        => '40'
  146: 	 },
  147:      height       => {
  148: 	 default     => 300,
  149: 	 test        => $int_test,
  150: 	 description => 'Height of image (pixels)',
  151:       	 edit_type   => 'entry',
  152: 	 size        => '10'
  153: 	 },
  154:      width        => {
  155: 	 default     => 400,
  156: 	 test        => $int_test,
  157: 	 description => 'Width of image (pixels)',
  158: 	 edit_type   => 'entry',
  159: 	 size        => '10'
  160: 	 },
  161:      bgcolor      => {
  162: 	 default     => 'xffffff',
  163: 	 test        => $color_test, 
  164: 	 description => 'Background color of image (xffffff)',
  165: 	 edit_type   => 'entry',
  166: 	 size        => '10'
  167: 	 },
  168:      fgcolor      => {
  169: 	 default     => 'x000000',
  170: 	 test        => $color_test,
  171: 	 description => 'Foreground color of image (x000000)',
  172: 	 edit_type   => 'entry',
  173: 	 size        => '10'
  174: 	 },
  175:      transparent  => {
  176: 	 default     => 'off',
  177: 	 test        => $onoff_test, 
  178: 	 description => 'Transparent image',
  179: 	 edit_type   => 'onoff'
  180: 	 },
  181:      grid         => {
  182: 	 default     => 'on',
  183: 	 test        => $onoff_test, 
  184: 	 description => 'Display grid',
  185: 	 edit_type   => 'onoff'
  186: 	 },
  187:      gridlayer    => {
  188: 	 default     => 'off',
  189: 	 test        => $onoff_test, 
  190: 	 description => 'Display grid front layer over filled boxes or filled curves',
  191: 	 edit_type   => 'onoff'
  192: 	 },
  193:      box_border   => {
  194: 	 default     => 'noborder',
  195: 	 test        => sub {$_[0]=~/^(noborder|border)$/},
  196: 	 description => 'Draw border for boxes',
  197: 	 edit_type   => 'choice',
  198: 	 choices     => ['border','noborder']
  199: 	 },
  200:      border       => {
  201: 	 default     => 'on',
  202: 	 test        => $onoff_test, 
  203: 	 description => 'Draw border around plot',
  204: 	 edit_type   => 'onoff'
  205: 	 },
  206:      font         => {
  207: 	 default     => '9',
  208: 	 test        => $sml_test,
  209: 	 description => 'Font size to use in web output (pts)',
  210: 	 edit_type   => 'choice',
  211: 	 choices     => [['5','5 (small)'],'6','7','8',['9','9 (medium)'],'10',['11','11 (large)'],'12','15']
  212: 	 },
  213:      fontface     => {
  214:         default     => 'sans-serif',
  215:         test        => sub {$_[0]=~/^(sans-serif|serif|classic)$/},
  216:         description => 'Type of font to use',
  217:         edit_type   => 'choice',
  218:         choices     => ['sans-serif','serif', 'classic']
  219:         },
  220:      samples      => {
  221: 	 default     => '100',
  222: 	 test        => $int_test,
  223: 	 description => 'Number of samples for non-data plots',
  224: 	 edit_type   => 'choice',
  225: 	 choices     => ['100','200','500','1000','2000','5000']
  226: 	 },
  227:      align        => {
  228: 	 default     => 'middle',
  229: 	 test        => sub {$_[0]=~/^(left|right|middle|center)$/},
  230: 	 description => 'Alignment for image in HTML',
  231: 	 edit_type   => 'choice',
  232: 	 choices     => ['left','right','middle']
  233: 	 },
  234:      texwidth     => {
  235:          default     => '93',
  236:          test        => $int_test,
  237:          description => 'Width of plot when printed (mm)',
  238:          edit_type   => 'entry',
  239:          size        => '5'
  240:          },
  241:      texfont      => {
  242:          default     => '22',
  243:          test        => $int_test,
  244:          description => 'Font size to use in TeX output (pts):',
  245:          edit_type   => 'choice',
  246:          choices     => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
  247:          },
  248:      plotcolor    => {
  249:          default     => 'monochrome',
  250:          test        => sub {$_[0]=~/^(monochrome|color|colour)$/},
  251:          description => 'Color setting for printing:',
  252:          edit_type   => 'choice',
  253:          choices     => [qw/monochrome color colour/],
  254:          },
  255:      pattern      => {
  256: 	 default     => '',
  257: 	 test        => $int_test,
  258: 	 description => 'Pattern value for boxes:',
  259: 	 edit_type   => 'choice',
  260:          choices     => [0,1,2,3,4,5,6]
  261:          },
  262:      solid        => {
  263:          default     => 0,
  264:          test        => $real_test,
  265:          description => 'The density of fill style for boxes',
  266:          edit_type   => 'entry',
  267:          size        => '5'
  268:          },
  269:      fillstyle    => {
  270: 	 default     => 'empty',
  271: 	 test        => sub {$_[0]=~/^(empty|solid|pattern)$/},
  272: 	 description => 'Filled style for boxes:',
  273: 	 edit_type   => 'choice',
  274:          choices     => ['empty','solid','pattern']
  275:          },
  276:      plottype     => {
  277: 	 default     => 'Cartesian',
  278: 	 test        => sub {$_[0]=~/^(Polar|Cartesian)$/},
  279: 	 description => 'Plot type:',
  280: 	 edit_type   => 'choice',
  281:          choices     => ['Cartesian','Polar']
  282:          },
  283:      gridtype     => {
  284: 	 default     => 'Cartesian',
  285: 	 test        => sub {$_[0]=~/^(Polar|Cartesian|Linear-Log|Log-Linear|Log-Log)$/},
  286: 	 description => 'Grid type:',
  287: 	 edit_type   => 'choice',
  288:          choices     => ['Cartesian','Polar','Linear-Log','Log-Linear','Log-Log']
  289:          },
  290:      lmargin      => {
  291: 	 default     => 'default',
  292: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  293: 	 description => 'Left margin width (pts):',
  294: 	 edit_type   => 'choice',
  295:          choices     => $margin_choices,
  296:          },
  297:      rmargin      => {
  298: 	 default     => 'default',
  299: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  300: 	 description => 'Right margin width (pts):',
  301: 	 edit_type   => 'choice',
  302:          choices     => $margin_choices,
  303:          },
  304:      tmargin      => {
  305: 	 default     => 'default',
  306: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  307: 	 description => 'Top margin width (pts):',
  308: 	 edit_type   => 'choice',
  309:          choices     => $margin_choices,
  310:          },
  311:      bmargin      => {
  312: 	 default     => 'default',
  313: 	 test        => sub {$_[0]=~/^(default|\d+)$/},
  314: 	 description => 'Bottom margin width (pts):',
  315: 	 edit_type   => 'choice',
  316:          choices     => $margin_choices,
  317:          },
  318:      boxwidth     => {
  319: 	 default     => '',
  320: 	 test        => $real_test, 
  321: 	 description => 'Width of boxes, default is auto',
  322: 	 edit_type   => 'entry',
  323:          size        => '5'
  324:          },
  325:      major_ticscale  => {
  326:          default     => '1',
  327:          test        => $real_test,
  328:          description => 'Size of major tic marks (plot coordinates)',
  329:          edit_type   => 'entry',
  330:          size        => '5'
  331:          },
  332:      minor_ticscale  => {
  333:          default     => '0.5',
  334:          test        => $real_test,
  335:          description => 'Size of minor tic mark (plot coordinates)',
  336:          edit_type   => 'entry',
  337:          size        => '5'
  338:          },
  339:      );
  340: 
  341: my %key_defaults = 
  342:     (
  343:      title => { 
  344: 	 default => '',
  345: 	 test => $words_test,
  346: 	 description => 'Title of key',
  347: 	 edit_type   => 'entry',
  348: 	 size        => '40'
  349: 	 },
  350:      box   => { 
  351: 	 default => 'off',
  352: 	 test => $onoff_test,
  353: 	 description => 'Draw a box around the key?',
  354: 	 edit_type   => 'onoff'
  355: 	 },
  356:      pos   => { 
  357: 	 default => 'top right', 
  358: 	 test => $key_pos_test, 
  359: 	 description => 'Position of the key on the plot',
  360: 	 edit_type   => 'choice',
  361: 	 choices     => ['top left','top right','bottom left','bottom right',
  362: 			 'outside','below']
  363: 	 }
  364:      );
  365: 
  366: my %label_defaults = 
  367:     (
  368:      xpos    => {
  369: 	 default => 0,
  370: 	 test => $real_test,
  371: 	 description => 'X position of label (graph coordinates)',
  372: 	 edit_type   => 'entry',
  373: 	 size        => '10'
  374: 	 },
  375:      ypos    => {
  376: 	 default => 0, 
  377: 	 test => $real_test,
  378: 	 description => 'Y position of label (graph coordinates)',
  379: 	 edit_type   => 'entry',
  380: 	 size        => '10'
  381: 	 },
  382:      justify => {
  383: 	 default => 'left',    
  384: 	 test => sub {$_[0]=~/^(left|right|center)$/},
  385: 	 description => 'justification of the label text on the plot',
  386: 	 edit_type   => 'choice',
  387: 	 choices     => ['left','right','center']
  388:      },
  389:      rotate => {
  390:          default => 0,
  391:          test => $real_test,
  392:          description => 'Rotation of label (degrees)',
  393:          edit_type   => 'entry',
  394:          size        => '10',
  395:      }
  396:      );
  397: 
  398: my @tic_edit_order = ('location','mirror','start','increment','end',
  399:                       'minorfreq');
  400: my %tic_defaults =
  401:     (
  402:      location => {
  403: 	 default => 'border', 
  404: 	 test => sub {$_[0]=~/^(border|axis)$/},
  405: 	 description => 'Location of major tic marks',
  406: 	 edit_type   => 'choice',
  407: 	 choices     => ['border','axis']
  408: 	 },
  409:      mirror => {
  410: 	 default => 'on', 
  411: 	 test => $onoff_test,
  412: 	 description => 'Mirror tics on opposite axis?',
  413: 	 edit_type   => 'onoff'
  414: 	 },
  415:      start => {
  416: 	 default => '-10.0',
  417: 	 test => $real_test,
  418: 	 description => 'Start major tics at',
  419: 	 edit_type   => 'entry',
  420: 	 size        => '10'
  421: 	 },
  422:      increment => {
  423: 	 default => '1.0',
  424: 	 test => $real_test,
  425: 	 description => 'Place a major tic every',
  426: 	 edit_type   => 'entry',
  427: 	 size        => '10'
  428: 	 },
  429:      end => {
  430: 	 default => ' 10.0',
  431: 	 test => $real_test,
  432: 	 description => 'Stop major tics at ',
  433: 	 edit_type   => 'entry',
  434: 	 size        => '10'
  435: 	 },
  436:      minorfreq => {
  437: 	 default => '0',
  438: 	 test => $int_test,
  439: 	 description => 'Number of minor tics per major tic mark',
  440: 	 edit_type   => 'entry',
  441: 	 size        => '10'
  442: 	 },         
  443:      );
  444: 
  445: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax','xformat', 'yformat');
  446: my %axis_defaults = 
  447:     (
  448:      color   => {
  449: 	 default => 'x000000', 
  450: 	 test => $color_test,
  451: 	 description => 'Color of grid lines (x000000)',
  452: 	 edit_type   => 'entry',
  453: 	 size        => '10'
  454: 	 },
  455:      xmin      => {
  456: 	 default => '-10.0',
  457: 	 test => $real_test,
  458: 	 description => 'Minimum x-value shown in plot',
  459: 	 edit_type   => 'entry',
  460: 	 size        => '10'
  461: 	 },
  462:      xmax      => {
  463: 	 default => ' 10.0',
  464: 	 test => $real_test,
  465: 	 description => 'Maximum x-value shown in plot',	 
  466: 	 edit_type   => 'entry',
  467: 	 size        => '10'
  468: 	 },
  469:      ymin      => {
  470: 	 default => '-10.0',
  471: 	 test => $real_test,
  472: 	 description => 'Minimum y-value shown in plot',	 
  473: 	 edit_type   => 'entry',
  474: 	 size        => '10'
  475: 	 },
  476:      ymax      => {
  477: 	 default => ' 10.0',
  478: 	 test => $real_test,
  479: 	 description => 'Maximum y-value shown in plot',	 
  480: 	 edit_type   => 'entry',
  481: 	 size        => '10'
  482:         },
  483:      xformat      => {
  484:          default     => 'on',
  485:          test        => sub {$_[0]=~/^(on|off|\d+(f|F|e|E))$/},
  486:          description => 'X-axis number formatting',
  487:          edit_type   => 'choice',
  488:          choices     => ['on', 'off', '2e', '2f'],
  489:          },
  490:      yformat      => {
  491:          default     => 'on',
  492:          test        => sub {$_[0]=~/^(on|off|\d+(f|F|e|E))$/},
  493:          description => 'X-axis number formatting',
  494:          edit_type   => 'choice',
  495:          choices     => ['on', 'off', '2e', '2f'],
  496:          },
  497: 
  498:      );
  499: 
  500: my @curve_edit_order = ('color','name','linestyle','linewidth','pointtype','pointsize','limit');
  501: 
  502: my %curve_defaults = 
  503:     (
  504:      color     => {
  505: 	 default => 'x000000',
  506: 	 test => $color_test,
  507: 	 description => 'Color of curve (x000000)',
  508: 	 edit_type   => 'entry',
  509: 	 size        => '10'
  510: 	 },
  511:      name      => {
  512: 	 default => '',
  513: 	 test => $words_test,
  514: 	 description => 'Name of curve to appear in key',
  515: 	 edit_type   => 'entry',
  516: 	 size        => '20'
  517: 	 },
  518:      linestyle => {
  519: 	 default => 'lines',
  520: 	 test => $linestyle_test,
  521: 	 description => 'Line style',
  522: 	 edit_type   => 'choice',
  523: 	 choices     => [keys(%linestyles)]
  524: 	 },
  525:      linewidth => {
  526:          default     => 1,
  527:          test        => $int_test,
  528:          description => 'Line width (may not apply to all line styles)',
  529:          edit_type   => 'choice',
  530:          choices     => [1,2,3,4,5,6,7,8,9,10]
  531:          },
  532:      pointsize => {
  533:          default     => 1,
  534:          test        => $pos_real_test,
  535:          description => 'Point size (may not apply to all line styles)',
  536:          edit_type   => 'entry',
  537:          size        => '5'
  538:          },
  539:      pointtype => {
  540:          default     => 1,
  541:          test        => $int_test,
  542:          description => 'Point type (may not apply to all line styles)',
  543:          edit_type   => 'choice',
  544:          choices     => [0,1,2,3,4,5,6]
  545:          },
  546:      limit     => {
  547:          default     => 'closed',
  548: 	 test        => sub {$_[0]=~/^(closed|x1|x2|y1|y2)$/},
  549:          description => 'Point to fill -- for filledcurves',
  550:          edit_type   => 'choice',
  551:          choices     => ['closed','x1','x2','y1','y2']
  552:          },
  553:      );
  554: 
  555: ###################################################################
  556: ##                                                               ##
  557: ##                    parsing and edit rendering                 ##
  558: ##                                                               ##
  559: ###################################################################
  560: 
  561: undef %Apache::lonplot::plot;
  562: my (%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
  563: 
  564: sub start_gnuplot {
  565:     undef(%Apache::lonplot::plot);   undef(%key);    undef(%axis);
  566:     undef($title);  undef($xlabel); undef($ylabel);
  567:     undef(@labels); undef(@curves);
  568:     undef(%xtics);  undef(%ytics);
  569:     #
  570:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  571:     my $result='';
  572:     &Apache::lonxml::register('Apache::lonplot',
  573: 	     ('title','xlabel','ylabel','key','axis','label','curve',
  574: 	      'xtics','ytics'));
  575:     push (@Apache::lonxml::namespace,'lonplot');
  576:     if ($target eq 'web' || $target eq 'tex') {
  577: 	&get_attributes(\%Apache::lonplot::plot,\%gnuplot_defaults,$parstack,$safeeval,
  578: 			$tagstack->[-1]);
  579:     } elsif ($target eq 'edit') {
  580: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
  581: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
  582: 				    \@gnuplot_edit_order)
  583: 	    .&Apache::edit::end_row()
  584: 	    .&Apache::edit::start_spanning_row();
  585:     } elsif ($target eq 'modified') {
  586: 	my $constructtag=&Apache::edit::get_new_args
  587: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
  588: 	if ($constructtag) {
  589: 	    $result = &Apache::edit::rebuild_tag($token);
  590: 	}
  591:     }
  592:     return $result;
  593: }
  594: 
  595: sub end_gnuplot {
  596:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  597:     pop @Apache::lonxml::namespace;
  598:     &Apache::lonxml::deregister('Apache::lonplot',
  599: 	('title','xlabel','ylabel','key','axis','label','curve'));
  600:     my $result = '';
  601:     my $randnumber;
  602:     # need to call rand everytime start_script would evaluate, as the
  603:     # safe space rand number generator and the global rand generator 
  604:     # are not separate
  605:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
  606: 	$target eq 'answer') {
  607:       $randnumber=int(rand(1000));
  608:     }
  609:     if ($target eq 'web' || $target eq 'tex') {
  610: 	&check_inputs(); # Make sure we have all the data we need
  611: 	##
  612: 	## Determine filename
  613: 	my $tmpdir = '/home/httpd/perl/tmp/';
  614: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
  615: 	    '_'.time.'_'.$$.$randnumber.'_plot';
  616: 	## Write the plot description to the file
  617: 	&write_gnuplot_file($tmpdir,$filename,$target);
  618: 	$filename = &escape($filename);
  619: 	## return image tag for the plot
  620: 	if ($target eq 'web') {
  621: 	    $result .= <<"ENDIMAGE";
  622: <img src    = "/cgi-bin/plot.$weboutputformat?file=$filename.data" 
  623:      width  = "$Apache::lonplot::plot{'width'}"
  624:      height = "$Apache::lonplot::plot{'height'}"
  625:      align  = "$Apache::lonplot::plot{'align'}"
  626:      alt    = "$Apache::lonplot::plot{'alttag'}" />
  627: ENDIMAGE
  628:         } elsif ($target eq 'tex') {
  629: 	    &Apache::lonxml::debug(" gnuplot wid = $Apache::lonplot::plot{'width'}");
  630: 	    &Apache::lonxml::debug(" gnuplot ht  = $Apache::lonplot::plot{'height'}");
  631: 	    #might be inside the safe space, register the URL for later
  632: 	    &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
  633: 	    $result  = "%DYNAMICIMAGE:$Apache::lonplot::plot{'width'}:$Apache::lonplot::plot{'height'}:$Apache::lonplot::plot{'texwidth'}\n";
  634: 	    $result .= '\graphicspath{{/home/httpd/perl/tmp/}}'."\n";
  635: 	    $result .= '\includegraphics[width='.$Apache::lonplot::plot{'texwidth'}.' mm]{'.&unescape($filename).'.eps}';
  636: 	}
  637:     } elsif ($target eq 'edit') {
  638: 	$result.=&Apache::edit::tag_end($target,$token);
  639:     }
  640:     return $result;
  641: }
  642: 
  643: 
  644: ##--------------------------------------------------------------- xtics
  645: sub start_xtics {
  646:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  647:     my $result='';
  648:     if ($target eq 'web' || $target eq 'tex') {
  649: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
  650: 		    $tagstack->[-1]);
  651:     } elsif ($target eq 'edit') {
  652: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
  653: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  654: 				    \@tic_edit_order);
  655:     } elsif ($target eq 'modified') {
  656: 	my $constructtag=&Apache::edit::get_new_args
  657: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  658: 	if ($constructtag) {
  659: 	    $result = &Apache::edit::rebuild_tag($token);
  660: 	}
  661:     }
  662:     return $result;
  663: }
  664: 
  665: sub end_xtics {
  666:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  667:     my $result = '';
  668:     if ($target eq 'web' || $target eq 'tex') {
  669:     } elsif ($target eq 'edit') {
  670: 	$result.=&Apache::edit::tag_end($target,$token);
  671:     }
  672:     return $result;
  673: }
  674: 
  675: ##--------------------------------------------------------------- ytics
  676: sub start_ytics {
  677:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  678:     my $result='';
  679:     if ($target eq 'web' || $target eq 'tex') {
  680: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
  681: 		    $tagstack->[-1]);
  682:     } elsif ($target eq 'edit') {
  683: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
  684: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
  685: 				    \@tic_edit_order);
  686:     } elsif ($target eq 'modified') {
  687: 	my $constructtag=&Apache::edit::get_new_args
  688: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
  689: 	if ($constructtag) {
  690: 	    $result = &Apache::edit::rebuild_tag($token);
  691: 	}
  692:     }
  693:     return $result;
  694: }
  695: 
  696: sub end_ytics {
  697:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  698:     my $result = '';
  699:     if ($target eq 'web' || $target eq 'tex') {
  700:     } elsif ($target eq 'edit') {
  701: 	$result.=&Apache::edit::tag_end($target,$token);
  702:     }
  703:     return $result;
  704: }
  705: 
  706: ##-----------------------------------------------------------------font
  707: my %font_properties =
  708:     (
  709:      'classic'    => {
  710: 	 face       => 'classic',
  711: 	 file       => 'DejaVuSansMono-Bold',
  712: 	 printname  => 'Helvetica',
  713: 	 tex_no_file => 1,
  714:      },
  715:      'sans-serif' => {
  716: 	 face       => 'sans-serif',
  717: 	 file       => 'DejaVuSans',
  718: 	 printname  => 'DejaVuSans',
  719:      },
  720:      'serif'      => {
  721: 	 face       => 'serif',
  722: 	 file       => 'DejaVuSerif',
  723: 	 printname  => 'DejaVuSerif',
  724:      },
  725:      );
  726: 
  727: sub get_font {
  728:     my ($target) = @_;
  729:     my ($size, $selected_font);
  730: 
  731:     if ( $Apache::lonplot::plot{'font'} =~ /^(small|medium|large)/) {
  732: 	$selected_font = $font_properties{'classic'};
  733: 	if ( $Apache::lonplot::plot{'font'} eq 'small') {
  734: 	    $size = '5';
  735: 	} elsif ( $Apache::lonplot::plot{'font'} eq 'medium') {
  736: 	    $size = '9';
  737: 	} elsif ( $Apache::lonplot::plot{'font'} eq 'large') {
  738: 	    $size = '11';
  739: 	} else {
  740: 	    $size = '9';
  741: 	}
  742:     } else {
  743: 	$size = $Apache::lonplot::plot{'font'};
  744: 	$selected_font = $font_properties{$Apache::lonplot::plot{'fontface'}};
  745:     }
  746:     if ($target eq 'tex' && defined($Apache::lonplot::plot{'texfont'})) {
  747: 	$size = $Apache::lonplot::plot{'texfont'};
  748:     }
  749:     return ($size, $selected_font);
  750: }
  751: 
  752: ##----------------------------------------------------------------- key
  753: sub start_key {
  754:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  755:     my $result='';
  756:     if ($target eq 'web' || $target eq 'tex') {
  757: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
  758: 		    $tagstack->[-1]);
  759:     } elsif ($target eq 'edit') {
  760: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
  761: 	$result .= &edit_attributes($target,$token,\%key_defaults);
  762:     } elsif ($target eq 'modified') {
  763: 	my $constructtag=&Apache::edit::get_new_args
  764: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
  765: 	if ($constructtag) {
  766: 	    $result = &Apache::edit::rebuild_tag($token);
  767: 	}
  768:     }
  769:     return $result;
  770: }
  771: 
  772: sub end_key {
  773:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  774:     my $result = '';
  775:     if ($target eq 'web' || $target eq 'tex') {
  776:     } elsif ($target eq 'edit') {
  777: 	$result.=&Apache::edit::tag_end($target,$token);
  778:     }
  779:     return $result;
  780: }
  781: 
  782: sub parse_label {
  783:     my ($target,$text) = @_;
  784:     my $parser=HTML::LCParser->new(\$text);
  785:     my $result;
  786:     while (my $token=$parser->get_token) {
  787: 	if ($token->[0] eq 'S') {
  788: 	    if ($token->[1] eq 'sub') {
  789: 		$result .= '_{';
  790: 	    } elsif ($token->[1] eq 'sup') {
  791: 		$result .= '^{';
  792: 	    } else {
  793: 		$result .= $token->[4];
  794: 	    }
  795: 	} elsif ($token->[0] eq 'E') {
  796: 	    if ($token->[1] eq 'sub'
  797: 		|| $token->[1] eq 'sup') {
  798: 		$result .= '}';
  799: 	    } else {
  800: 		$result .= $token->[2];
  801: 	    }
  802: 	} elsif ($token->[0] eq 'T') {
  803: 	    $result .= &replace_entities($target,$token->[1]);
  804: 	}
  805:     }
  806:     return $result;
  807: }
  808: 
  809: 
  810: my %lookup = 
  811:    ('(Alpha|#913)'    => {'tex' => '{/Symbol A}', 'web' => "\x{391}"},
  812:      '(Beta|#914)'    => {'tex' => '{/Symbol B}', 'web' => "\x{392}"},
  813:      '(Chi|#935)'     => {'tex' => '{/Symbol C}', 'web' => "\x{3A7}"},
  814:      '(Delta|#916)'   => {'tex' => '{/Symbol D}', 'web' => "\x{394}"},
  815:      '(Epsilon|#917)' => {'tex' => '{/Symbol E}', 'web' => "\x{395}"},
  816:      '(Phi|#934)'     => {'tex' => '{/Symbol F}', 'web' => "\x{3A6}"},
  817:      '(Gamma|#915)'   => {'tex' => '{/Symbol G}', 'web' => "\x{393}"},
  818:      '(Eta|#919)'     => {'tex' => '{/Symbol H}', 'web' => "\x{397}"},
  819:      '(Iota|#921)'    => {'tex' => '{/Symbol I}', 'web' => "\x{399}"},
  820:      '(Kappa|#922)'   => {'tex' => '{/Symbol K}', 'web' => "\x{39A}"},
  821:      '(Lambda|#923)'  => {'tex' => '{/Symbol L}', 'web' => "\x{39B}"},
  822:      '(Mu|#924)'      => {'tex' => '{/Symbol M}', 'web' => "\x{39C}"},
  823:      '(Nu|#925)'      => {'tex' => '{/Symbol N}', 'web' => "\x{39D}"},
  824:      '(Omicron|#927)' => {'tex' => '{/Symbol O}', 'web' => "\x{39F}"},
  825:      '(Pi|#928)'      => {'tex' => '{/Symbol P}', 'web' => "\x{3A0}"},
  826:      '(Theta|#920)'   => {'tex' => '{/Symbol Q}', 'web' => "\x{398}"},
  827:      '(Rho|#929)'     => {'tex' => '{/Symbol R}', 'web' => "\x{3A1}"},
  828:      '(Sigma|#931)'   => {'tex' => '{/Symbol S}', 'web' => "\x{3A3}"},
  829:      '(Tau|#932)'     => {'tex' => '{/Symbol T}', 'web' => "\x{3A4}"},
  830:      '(Upsilon|#933)' => {'tex' => '{/Symbol U}', 'web' => "\x{3A5}"},
  831:      '(Omega|#937)'   => {'tex' => '{/Symbol W}', 'web' => "\x{3A9}"},
  832:      '(Xi|#926)'      => {'tex' => '{/Symbol X}', 'web' => "\x{39E}"},
  833:      '(Psi|#936)'     => {'tex' => '{/Symbol Y}', 'web' => "\x{3A8}"},
  834:      '(Zeta|#918)'    => {'tex' => '{/Symbol Z}', 'web' => "\x{396}"},
  835:      '(alpha|#945)'   => {'tex' => '{/Symbol a}', 'web' => "\x{3B1}"},
  836:      '(beta|#946)'    => {'tex' => '{/Symbol b}', 'web' => "\x{3B2}"},
  837:      '(chi|#967)'     => {'tex' => '{/Symbol c}', 'web' => "\x{3C7}"},
  838:      '(delta|#948)'   => {'tex' => '{/Symbol d}', 'web' => "\x{3B4}"},
  839:      '(epsilon|#949)' => {'tex' => '{/Symbol e}', 'web' => "\x{3B5}"},
  840:      '(phi|#966)'     => {'tex' => '{/Symbol f}', 'web' => "\x{3C6}"},
  841:      '(gamma|#947)'   => {'tex' => '{/Symbol g}', 'web' => "\x{3B3}"},
  842:      '(eta|#951)'     => {'tex' => '{/Symbol h}', 'web' => "\x{3B7}"},
  843:      '(iota|#953)'    => {'tex' => '{/Symbol i}', 'web' => "\x{3B9}"},
  844:      '(kappa|#954)'   => {'tex' => '{/Symbol k}', 'web' => "\x{3BA}"},
  845:      '(lambda|#955)'  => {'tex' => '{/Symbol k}', 'web' => "\x{3BB}"},
  846:      '(mu|#956)'      => {'tex' => '{/Symbol m}', 'web' => "\x{3BC}"},
  847:      '(nu|#957)'      => {'tex' => '{/Symbol n}', 'web' => "\x{3BD}"},
  848:      '(omicron|#959)' => {'tex' => '{/Symbol o}', 'web' => "\x{3BF}"},
  849:      '(pi|#960)'      => {'tex' => '{/Symbol p}', 'web' => "\x{3C0}"},
  850:      '(theta|#952)'   => {'tex' => '{/Symbol q}', 'web' => "\x{3B8}"},
  851:      '(rho|#961)'     => {'tex' => '{/Symbol r}', 'web' => "\x{3C1}"},
  852:      '(sigma|#963)'   => {'tex' => '{/Symbol s}', 'web' => "\x{3C3}"},
  853:      '(tau|#964)'     => {'tex' => '{/Symbol t}', 'web' => "\x{3C4}"},
  854:      '(upsilon|#965)' => {'tex' => '{/Symbol u}', 'web' => "\x{3C5}"},
  855:      '(omega|#969)'   => {'tex' => '{/Symbol w}', 'web' => "\x{3C9}"},
  856:      '(xi|#958)'      => {'tex' => '{/Symbol x}', 'web' => "\x{3BE}"},
  857:      '(psi|#968)'     => {'tex' => '{/Symbol y}', 'web' => "\x{3C8}"},
  858:      '(zeta|#950)'    => {'tex' => '{/Symbol z}', 'web' => "\x{3B6}"},
  859:     );
  860: 
  861: 
  862: sub replace_entities {
  863:     my ($target,$text) = @_;
  864:     $text =~ s{([_^~\{\}]|\\\\)}{\\\\$1}g;
  865:     while (my ($re, $replace) = each(%lookup)) {
  866: 	$text =~ s/&$re;/$replace->{$target}/g;
  867:     }
  868:     $text =~ s{(&)}{\\\\$1}g;
  869:     return $text;
  870: }
  871: 
  872: ##------------------------------------------------------------------- title
  873: sub start_title {
  874:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  875:     my $result='';
  876:     if ($target eq 'web' || $target eq 'tex') {
  877: 	$title = &Apache::lonxml::get_all_text("/title",$parser,$style);
  878: 	$title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
  879: 	$title =~ s/\n/ /g;
  880: 	if (length($title) > $max_str_len) {
  881: 	    $title = substr($title,0,$max_str_len);
  882: 	}
  883: 	$title = &parse_label($target,$title);
  884:     } elsif ($target eq 'edit') {
  885: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
  886: 	my $text=&Apache::lonxml::get_all_text("/title",$parser,$style);
  887: 	$result.=&Apache::edit::editline('',$text,'',60);
  888:     } elsif ($target eq 'modified') {
  889: 	$result.=&Apache::edit::rebuild_tag($token);
  890: 	$result.=&Apache::edit::modifiedfield("/title",$parser);
  891:     }
  892:     return $result;
  893: }
  894: 
  895: sub end_title {
  896:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  897:     my $result = '';
  898:     if ($target eq 'web' || $target eq 'tex') {
  899:     } elsif ($target eq 'edit') {
  900: 	$result.=&Apache::edit::tag_end($target,$token);
  901:     }
  902:     return $result;
  903: }
  904: ##------------------------------------------------------------------- xlabel
  905: sub start_xlabel {
  906:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  907:     my $result='';
  908:     if ($target eq 'web' || $target eq 'tex') {
  909: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser,$style);
  910: 	$xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
  911: 	$xlabel =~ s/\n/ /g;
  912: 	if (length($xlabel) > $max_str_len) {
  913: 	    $xlabel = substr($xlabel,0,$max_str_len);
  914: 	}
  915: 	$xlabel = &parse_label($target,$xlabel);
  916:     } elsif ($target eq 'edit') {
  917: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
  918: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$parser,$style);
  919: 	$result.=&Apache::edit::editline('',$text,'',60);
  920:     } elsif ($target eq 'modified') {
  921: 	$result.=&Apache::edit::rebuild_tag($token);	
  922: 	$result.=&Apache::edit::modifiedfield("/xlabel",$parser);
  923:     }
  924:     return $result;
  925: }
  926: 
  927: sub end_xlabel {
  928:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  929:     my $result = '';
  930:     if ($target eq 'web' || $target eq 'tex') {
  931:     } elsif ($target eq 'edit') {
  932: 	$result.=&Apache::edit::tag_end($target,$token);
  933:     }
  934:     return $result;
  935: }
  936: 
  937: ##------------------------------------------------------------------- ylabel
  938: sub start_ylabel {
  939:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  940:     my $result='';
  941:     if ($target eq 'web' || $target eq 'tex') {
  942: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
  943: 	$ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
  944: 	$ylabel =~ s/\n/ /g;
  945: 	if (length($ylabel) > $max_str_len) {
  946: 	    $ylabel = substr($ylabel,0,$max_str_len);
  947: 	}
  948: 	$ylabel = &parse_label($target,$ylabel);
  949:     } elsif ($target eq 'edit') {
  950: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
  951: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
  952: 	$result .= &Apache::edit::editline('',$text,'',60);
  953:     } elsif ($target eq 'modified') {
  954: 	$result.=&Apache::edit::rebuild_tag($token);
  955: 	$result.=&Apache::edit::modifiedfield("/ylabel",$parser);
  956:     }
  957:     return $result;
  958: }
  959: 
  960: sub end_ylabel {
  961:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  962:     my $result = '';
  963:     if ($target eq 'web' || $target eq 'tex') {
  964:     } elsif ($target eq 'edit') {
  965: 	$result.=&Apache::edit::tag_end($target,$token);
  966:     }
  967:     return $result;
  968: }
  969: 
  970: ##------------------------------------------------------------------- label
  971: sub start_label {
  972:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  973:     my $result='';
  974:     if ($target eq 'web' || $target eq 'tex') {
  975: 	my %label;
  976: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
  977: 		    $tagstack->[-1]);
  978: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
  979: 	$text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
  980: 	$text =~ s/\n/ /g;
  981: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
  982: 	$label{'text'} = &parse_label($target,$text);
  983: 	push(@labels,\%label);
  984:     } elsif ($target eq 'edit') {
  985: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
  986: 	$result .= &edit_attributes($target,$token,\%label_defaults);
  987: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
  988: 	$result .= &Apache::edit::end_row().
  989: 	    &Apache::edit::start_spanning_row().
  990: 	    &Apache::edit::editline('',$text,'',60);
  991:     } elsif ($target eq 'modified') {
  992: 	&Apache::edit::get_new_args
  993: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
  994: 	$result.=&Apache::edit::rebuild_tag($token);
  995: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
  996:     }
  997:     return $result;
  998: }
  999: 
 1000: sub end_label {
 1001:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1002:     my $result = '';
 1003:     if ($target eq 'web' || $target eq 'tex') {
 1004:     } elsif ($target eq 'edit') {
 1005: 	$result.=&Apache::edit::tag_end($target,$token);
 1006:     }
 1007:     return $result;
 1008: }
 1009: 
 1010: ##------------------------------------------------------------------- curve
 1011: sub start_curve {
 1012:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1013:     my $result='';
 1014:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
 1015:     push (@Apache::lonxml::namespace,'curve');
 1016:     if ($target eq 'web' || $target eq 'tex') {
 1017: 	my %curve;
 1018: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
 1019: 		    $tagstack->[-1]);
 1020: 	push (@curves,\%curve);
 1021:     } elsif ($target eq 'edit') {
 1022: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
 1023: 	$result .= &edit_attributes($target,$token,\%curve_defaults,
 1024:                                     \@curve_edit_order)
 1025: 	    .&Apache::edit::end_row()
 1026: 	    .&Apache::edit::start_spanning_row();
 1027: 
 1028:     } elsif ($target eq 'modified') {
 1029: 	my $constructtag=&Apache::edit::get_new_args
 1030: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
 1031: 	if ($constructtag) {
 1032: 	    $result = &Apache::edit::rebuild_tag($token);
 1033: 	}
 1034:     }
 1035:     return $result;
 1036: }
 1037: 
 1038: sub end_curve {
 1039:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1040:     my $result = '';
 1041:     pop @Apache::lonxml::namespace;
 1042:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
 1043:     if ($target eq 'web' || $target eq 'tex') {
 1044:     } elsif ($target eq 'edit') {
 1045: 	$result.=&Apache::edit::tag_end($target,$token);
 1046:     }
 1047:     return $result;
 1048: }
 1049: 
 1050: ##------------------------------------------------------------ curve function
 1051: sub start_function {
 1052:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1053:     my $result='';
 1054:     if ($target eq 'web' || $target eq 'tex') {
 1055: 	if (exists($curves[-1]->{'data'})) {
 1056: 	    &Apache::lonxml::warning
 1057:                 ('Use of the <b>curve function</b> tag precludes use of '.
 1058:                  ' the <b>curve data</b> tag.  '.
 1059:                  'The curve data tag will be omitted in favor of the '.
 1060:                  'curve function declaration.');
 1061: 	    delete $curves[-1]->{'data'} ;
 1062: 	}
 1063:         my $function = &Apache::lonxml::get_all_text("/function",$parser,
 1064: 						     $style);
 1065: 	$function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
 1066: 	$curves[-1]->{'function'} = $function; 
 1067:     } elsif ($target eq 'edit') {
 1068: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
 1069: 	my $text = &Apache::lonxml::get_all_text("/function",$parser,$style);
 1070: 	$result .= &Apache::edit::editline('',$text,'',60);
 1071:     } elsif ($target eq 'modified') {
 1072: 	$result.=&Apache::edit::rebuild_tag($token);
 1073: 	$result.=&Apache::edit::modifiedfield("/function",$parser);
 1074:     }
 1075:     return $result;
 1076: }
 1077: 
 1078: sub end_function {
 1079:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1080:     my $result = '';
 1081:     if ($target eq 'web' || $target eq 'tex') {
 1082:     } elsif ($target eq 'edit') {
 1083: 	$result .= &Apache::edit::end_table();
 1084:     }
 1085:     return $result;
 1086: }
 1087: 
 1088: ##------------------------------------------------------------ curve  data
 1089: sub start_data {
 1090:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1091:     my $result='';
 1092:     if ($target eq 'web' || $target eq 'tex') {
 1093: 	if (exists($curves[-1]->{'function'})) {
 1094: 	    &Apache::lonxml::warning
 1095:                 ('Use of the <b>curve function</b> tag precludes use of '.
 1096:                  ' the <b>curve data</b> tag.  '.
 1097:                  'The curve function tag will be omitted in favor of the '.
 1098:                  'curve data declaration.');
 1099: 	    delete($curves[-1]->{'function'});
 1100: 	}
 1101: 	my $datatext = &Apache::lonxml::get_all_text("/data",$parser,$style);
 1102: 	$datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
 1103: 	# Deal with cases where we're given an array...
 1104: 	if ($datatext =~ /^\@/) {
 1105: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
 1106: 					  $safeeval,1);
 1107: 	}
 1108: 	$datatext =~ s/\s+/ /g;
 1109: 	# Need to do some error checking on the @data array - 
 1110: 	# make sure it's all numbers and make sure each array 
 1111: 	# is of the same length.
 1112: 	my @data;
 1113: 	if ($datatext =~ /,/) { # comma deliminated
 1114: 	    @data = split /,/,$datatext;
 1115: 	} else { # Assume it's space separated.
 1116: 	    @data = split / /,$datatext;
 1117: 	}
 1118: 	for (my $i=0;$i<=$#data;$i++) {
 1119: 	    # Check that it's non-empty
 1120: 	    if (! defined($data[$i])) {
 1121: 		&Apache::lonxml::warning(
 1122: 		    'undefined curve data value.  Replacing with '.
 1123: 		    ' pi/e = 1.15572734979092');
 1124: 		$data[$i] = 1.15572734979092;
 1125: 	    }
 1126: 	    # Check that it's a number
 1127: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
 1128: 		&Apache::lonxml::warning(
 1129: 		    'Bad curve data value of '.$data[$i].'  Replacing with '.
 1130: 		    ' pi/e = 1.15572734979092');
 1131: 		$data[$i] = 1.15572734979092;
 1132: 	    }
 1133: 	}
 1134: 	# complain if the number of data points is not the same as
 1135: 	# in previous sets of data.
 1136: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
 1137: 	    &Apache::lonxml::warning
 1138: 		('Number of data points is not consistent with previous '.
 1139: 		 'number of data points');
 1140: 	}
 1141: 	push  @{$curves[-1]->{'data'}},\@data;
 1142:     } elsif ($target eq 'edit') {
 1143: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
 1144: 	my $text = &Apache::lonxml::get_all_text("/data",$parser,$style);
 1145: 	$result .= &Apache::edit::editline('',$text,'',60);
 1146:     } elsif ($target eq 'modified') {
 1147: 	$result.=&Apache::edit::rebuild_tag($token);
 1148: 	$result.=&Apache::edit::modifiedfield("/data",$parser);
 1149:     }
 1150:     return $result;
 1151: }
 1152: 
 1153: sub end_data {
 1154:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1155:     my $result = '';
 1156:     if ($target eq 'web' || $target eq 'tex') {
 1157:     } elsif ($target eq 'edit') {
 1158: 	$result .= &Apache::edit::end_table();
 1159:     }
 1160:     return $result;
 1161: }
 1162: 
 1163: ##------------------------------------------------------------------- axis
 1164: sub start_axis {
 1165:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1166:     my $result='';
 1167:     if ($target eq 'web' || $target eq 'tex') {
 1168: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
 1169: 			$tagstack->[-1]);
 1170:     } elsif ($target eq 'edit') {
 1171: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
 1172: 	$result .= &edit_attributes($target,$token,\%axis_defaults,
 1173: 				    \@axis_edit_order);
 1174:     } elsif ($target eq 'modified') {
 1175: 	my $constructtag=&Apache::edit::get_new_args
 1176: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
 1177: 	if ($constructtag) {
 1178: 	    $result = &Apache::edit::rebuild_tag($token);
 1179: 	}
 1180:     }
 1181:     return $result;
 1182: }
 1183: 
 1184: sub end_axis {
 1185:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1186:     my $result = '';
 1187:     if ($target eq 'web' || $target eq 'tex') {
 1188:     } elsif ($target eq 'edit') {
 1189: 	$result.=&Apache::edit::tag_end($target,$token);
 1190:     } elsif ($target eq 'modified') {
 1191:     }
 1192:     return $result;
 1193: }
 1194: 
 1195: ###################################################################
 1196: ##                                                               ##
 1197: ##        Utility Functions                                      ##
 1198: ##                                                               ##
 1199: ###################################################################
 1200: 
 1201: ##----------------------------------------------------------- set_defaults
 1202: sub set_defaults {
 1203:     my ($var,$defaults) = @_;
 1204:     my $key;
 1205:     foreach $key (keys(%$defaults)) {
 1206: 	$var->{$key} = $defaults->{$key}->{'default'};
 1207:     }
 1208: }
 1209: 
 1210: ##------------------------------------------------------------------- misc
 1211: sub get_attributes{
 1212:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
 1213:     foreach my $attr (keys(%{$defaults})) {
 1214: 	if ($attr eq 'texwidth' || $attr eq 'texfont') {
 1215: 	    $values->{$attr} = 
 1216: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
 1217: 	} else {
 1218: 	    $values->{$attr} = 
 1219: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval);
 1220: 	}
 1221: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
 1222: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
 1223: 	    next;
 1224: 	}
 1225: 	my $test = $defaults->{$attr}->{'test'};
 1226: 	if (! &$test($values->{$attr})) {
 1227: 	    &Apache::lonxml::warning
 1228: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
 1229: 		 .$defaults->{$attr}->{'default'} );
 1230: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
 1231: 	}
 1232:     }
 1233:     return ;
 1234: }
 1235: 
 1236: ##------------------------------------------------------- write_gnuplot_file
 1237: sub write_gnuplot_file {
 1238:     my ($tmpdir,$filename,$target)= @_;
 1239:     my ($fontsize, $font_properties) =  &get_font($target);
 1240:     my $gnuplot_input = '';
 1241:     my $curve;
 1242:     #
 1243:     # Check to be sure we do not have any empty curves
 1244:     my @curvescopy;
 1245:     foreach my $curve (@curves) {
 1246:         if (exists($curve->{'function'})) {
 1247:             if ($curve->{'function'} !~ /^\s*$/) {
 1248:                 push(@curvescopy,$curve);
 1249:             }
 1250:         } elsif (exists($curve->{'data'})) {
 1251:             foreach my $data (@{$curve->{'data'}}) {
 1252:                 if (scalar(@$data) > 0) {
 1253:                     push(@curvescopy,$curve);
 1254:                     last;
 1255:                 }
 1256:             }
 1257:         }
 1258:     }
 1259:     @curves = @curvescopy;
 1260:     # Collect all the colors
 1261:     my @Colors;
 1262:     push @Colors, $Apache::lonplot::plot{'bgcolor'};
 1263:     push @Colors, $Apache::lonplot::plot{'fgcolor'}; 
 1264:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$Apache::lonplot::plot{'fgcolor'});
 1265:     foreach $curve (@curves) {
 1266: 	push @Colors, ($curve->{'color'} ne '' ? 
 1267: 		       $curve->{'color'}       : 
 1268: 		       $Apache::lonplot::plot{'fgcolor'}        );
 1269:     }
 1270:     # set term
 1271:     if ($target eq 'web') {
 1272: 	$gnuplot_input .= 'set terminal png enhanced nocrop ';
 1273: 	$gnuplot_input .= 'transparent ' if ($Apache::lonplot::plot{'transparent'} eq 'on');
 1274: 	$gnuplot_input .= 'font "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
 1275: 	    '/'.$font_properties->{'file'}.'.ttf" ';
 1276: 	$gnuplot_input .= $fontsize;
 1277: 	$gnuplot_input .= ' size '.$Apache::lonplot::plot{'width'}.','.$Apache::lonplot::plot{'height'}.' ';
 1278: 	$gnuplot_input .= "@Colors\n";
 1279: 	# set output
 1280: 	$gnuplot_input .= "set output\n";
 1281:     } elsif ($target eq 'tex') {
 1282: 	$gnuplot_input .= "set term postscript eps enhanced $Apache::lonplot::plot{'plotcolor'} solid ";
 1283: 	if (!$font_properties->{'tex_no_file'}) {
 1284: 	    $gnuplot_input .=
 1285: 		'fontfile "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
 1286: 		'/'.$font_properties->{'file'}.'.pfb" ';
 1287: 	}
 1288: 	$gnuplot_input .= ' "'.$font_properties->{'printname'}.'" ';
 1289: 	$gnuplot_input .= $fontsize;
 1290: 	$gnuplot_input .= "\nset output \"/home/httpd/perl/tmp/".
 1291: 	    &unescape($filename).".eps\"\n";
 1292:     }
 1293:     # cartesian or polar plot?
 1294:     if (lc($Apache::lonplot::plot{'plottype'}) eq 'polar') {
 1295:         $gnuplot_input .= 'set polar'.$/;
 1296:     } else {
 1297:         # Assume Cartesian
 1298:     }
 1299:     # cartesian or polar grid?
 1300:     if (lc($Apache::lonplot::plot{'gridtype'}) eq 'polar') {
 1301:         $gnuplot_input .= 'set grid polar'.$/;
 1302:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'linear-log') {
 1303:         $gnuplot_input .= 'set logscale x'.$/;
 1304:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-linear') {
 1305:         $gnuplot_input .= 'set logscale y'.$/;
 1306:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-log') {
 1307:         $gnuplot_input .= 'set logscale x'.$/;
 1308:         $gnuplot_input .= 'set logscale y'.$/;
 1309:     } else {
 1310:         # Assume Cartesian
 1311:     }
 1312:     # solid or pattern for boxes?
 1313:     if (lc($Apache::lonplot::plot{'fillstyle'}) eq 'solid') {
 1314:         $gnuplot_input .= 'set style fill solid '.
 1315: 	    $Apache::lonplot::plot{'solid'}.$Apache::lonplot::plot{'box_border'}.$/;
 1316:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'pattern') {
 1317:         $gnuplot_input .= 'set style fill pattern '.$Apache::lonplot::plot{'pattern'}.$Apache::lonplot::plot{'box_border'}.$/;
 1318:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'empty') {
 1319:     }
 1320:     # margin
 1321:     if (lc($Apache::lonplot::plot{'lmargin'}) ne 'default') {
 1322:         $gnuplot_input .= 'set lmargin '.$Apache::lonplot::plot{'lmargin'}.$/;
 1323:     }
 1324:     if (lc($Apache::lonplot::plot{'rmargin'}) ne 'default') {
 1325:         $gnuplot_input .= 'set rmargin '.$Apache::lonplot::plot{'rmargin'}.$/;
 1326:     }
 1327:     if (lc($Apache::lonplot::plot{'tmargin'}) ne 'default') {
 1328:         $gnuplot_input .= 'set tmargin '.$Apache::lonplot::plot{'tmargin'}.$/;
 1329:     }
 1330:     if (lc($Apache::lonplot::plot{'bmargin'}) ne 'default') {
 1331:         $gnuplot_input .= 'set bmargin '.$Apache::lonplot::plot{'bmargin'}.$/;
 1332:     }
 1333: 
 1334:     # tic scales
 1335:     if ($version > 4) {
 1336: 	$gnuplot_input .= 'set tics scale '.
 1337: 	    $Apache::lonplot::plot{'major_ticscale'}.', '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
 1338:     } else {
 1339:     	$gnuplot_input .= 'set ticscale '.
 1340: 	    $Apache::lonplot::plot{'major_ticscale'}.' '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
 1341:     }
 1342:     #boxwidth
 1343:     if (lc($Apache::lonplot::plot{'boxwidth'}) ne '') {
 1344: 	$gnuplot_input .= 'set boxwidth '.$Apache::lonplot::plot{'boxwidth'}.$/;
 1345:     }
 1346:     # gridlayer
 1347:     $gnuplot_input .= 'set grid noxtics noytics front '.$/ 
 1348: 	if ($Apache::lonplot::plot{'gridlayer'} eq 'on');
 1349: 
 1350:     # grid
 1351:     $gnuplot_input .= 'set grid'.$/ if ($Apache::lonplot::plot{'grid'} eq 'on');
 1352:     # border
 1353:     $gnuplot_input .= ($Apache::lonplot::plot{'border'} eq 'on'?
 1354: 		       'set border'.$/           :
 1355: 		       'set noborder'.$/         );
 1356:     # sampling rate for non-data curves
 1357:     $gnuplot_input .= "set samples $Apache::lonplot::plot{'samples'}\n";
 1358:     # title, xlabel, ylabel
 1359:     # titles
 1360:     my $extra_space_x = ($xtics{'location'} eq 'axis') ? ' 0, -0.5 ' : '';
 1361:     my $extra_space_y = ($ytics{'location'} eq 'axis') ? ' -0.5, 0 ' : '';
 1362: 
 1363:     if ($target eq 'tex') {
 1364: 	$gnuplot_input .= "set title  \"$title\"          font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($title)) ;
 1365: 	$gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($xlabel));
 1366: 	$gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($ylabel));
 1367:     } else {
 1368:         $gnuplot_input .= "set title  \"$title\"          \n" if (defined($title)) ;
 1369:         $gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x \n" if (defined($xlabel));
 1370:         $gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y \n" if (defined($ylabel));
 1371:     }
 1372:     # tics
 1373:     if (%xtics) {    
 1374: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
 1375: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
 1376: 	$gnuplot_input .= "$xtics{'start'}, ";
 1377: 	$gnuplot_input .= "$xtics{'increment'}, ";
 1378: 	$gnuplot_input .= "$xtics{'end'}\n";
 1379:         if ($xtics{'minorfreq'} != 0) {
 1380:             $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
 1381:         } 
 1382:     }
 1383:     if (%ytics) {    
 1384: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
 1385: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
 1386: 	$gnuplot_input .= "$ytics{'start'}, ";
 1387: 	$gnuplot_input .= "$ytics{'increment'}, ";
 1388:         $gnuplot_input .= "$ytics{'end'}\n";
 1389:         if ($ytics{'minorfreq'} != 0) {
 1390:             $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
 1391:         } 
 1392:     }
 1393:     # axis
 1394:     if (%axis) {
 1395:         if ($axis{'xformat'} ne 'on') {
 1396:             $gnuplot_input .= "set format x ";
 1397:             if ($axis{'xformat'} eq 'off') {
 1398:                 $gnuplot_input .= "\"\"\n";
 1399:             } else {
 1400:                 $gnuplot_input .= "\"\%.".$axis{'xformat'}."\"\n";
 1401:             }
 1402:         }
 1403:         if ($axis{'yformat'} ne 'on') {
 1404:             $gnuplot_input .= "set format y ";
 1405:             if ($axis{'yformat'} eq 'off') {
 1406:                 $gnuplot_input .= "\"\"\n";
 1407:             } else {
 1408:                 $gnuplot_input .= "\"\%.".$axis{'yformat'}."\"\n";
 1409:             }
 1410:         }
 1411: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
 1412: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
 1413:     }
 1414:     # Key
 1415:     if (%key) {
 1416: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
 1417: 	if ($key{'title'} ne '') {
 1418: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
 1419: 	} 
 1420: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
 1421:     } else {
 1422: 	$gnuplot_input .= 'set nokey'.$/;
 1423:     }
 1424:     # labels
 1425:     my $label;
 1426:     foreach $label (@labels) {
 1427: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
 1428:                           $label->{'xpos'}.','.$label->{'ypos'};
 1429:         if ($label->{'rotate'} ne '') {
 1430:             $gnuplot_input .= ' rotate by '.$label->{'rotate'};
 1431:         }
 1432:         $gnuplot_input .= ' '.$label->{'justify'};
 1433: 
 1434:         if ($target eq 'tex') {
 1435: 	    $gnuplot_input .=' font "'.$font_properties->{'printname'}.','.$fontsize.'pt"' ;
 1436:         }
 1437:         $gnuplot_input .= $/;
 1438:     }
 1439:     if ($target eq 'tex') {
 1440:         $gnuplot_input .="set size 1,".$Apache::lonplot::plot{'height'}/$Apache::lonplot::plot{'width'}*1.38;
 1441:         $gnuplot_input .="\n";
 1442:     }
 1443:     # curves
 1444:     $gnuplot_input .= 'plot ';
 1445:     for (my $i = 0;$i<=$#curves;$i++) {
 1446: 	$curve = $curves[$i];
 1447: 	$gnuplot_input.= ', ' if ($i > 0);
 1448: 	if ($target eq 'tex') {
 1449: 	    $curve->{'linewidth'} *= 2;
 1450: 	}
 1451: 	if (exists($curve->{'function'})) {
 1452: 	    $gnuplot_input.= 
 1453: 		$curve->{'function'}.' title "'.
 1454: 		$curve->{'name'}.'" with '.
 1455:                 $curve->{'linestyle'};
 1456: 
 1457:             if (($curve->{'linestyle'} eq 'points')      ||
 1458:                 ($curve->{'linestyle'} eq 'linespoints') ||
 1459:                 ($curve->{'linestyle'} eq 'errorbars')   ||
 1460:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
 1461:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
 1462:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
 1463:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
 1464:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
 1465:             } elsif ($curve->{'linestyle'} eq 'filledcurves') { 
 1466:                 $gnuplot_input.= ' '.$curve->{'limit'};
 1467:             }
 1468:             $gnuplot_input.= ' linewidth '.$curve->{'linewidth'};
 1469: 
 1470: 	} elsif (exists($curve->{'data'})) {
 1471: 	    # Store data values in $datatext
 1472: 	    my $datatext = '';
 1473: 	    #   get new filename
 1474: 	    my $datafilename = "$tmpdir/$filename.data.$i";
 1475: 	    my $fh=Apache::File->new(">$datafilename");
 1476: 	    # Compile data
 1477: 	    my @Data = @{$curve->{'data'}};
 1478: 	    my @Data0 = @{$Data[0]};
 1479: 	    for (my $i =0; $i<=$#Data0; $i++) {
 1480: 		my $dataset;
 1481: 		foreach $dataset (@Data) {
 1482: 		    $datatext .= $dataset->[$i] . ' ';
 1483: 		}
 1484: 		$datatext .= $/;
 1485: 	    }
 1486: 	    #   write file
 1487: 	    print $fh $datatext;
 1488: 	    close($fh);
 1489: 	    #   generate gnuplot text
 1490: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
 1491: 		$curve->{'name'}.'" with '.
 1492: 		$curve->{'linestyle'};
 1493:             if (($curve->{'linestyle'} eq 'points')      ||
 1494:                 ($curve->{'linestyle'} eq 'linespoints') ||
 1495:                 ($curve->{'linestyle'} eq 'errorbars')   ||
 1496:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
 1497:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
 1498:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
 1499:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
 1500:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
 1501:             } elsif ($curve->{'linestyle'} eq 'filledcurves') { 
 1502:                 $gnuplot_input.= ' '.$curve->{'limit'};
 1503:             }
 1504:             $gnuplot_input.= ' linewidth '.$curve->{'linewidth'};
 1505: 	}
 1506:     }
 1507:     # Write the output to a file.
 1508:     open (my $fh,">$tmpdir$filename.data");
 1509:     binmode($fh, ":utf8");
 1510:     print $fh $gnuplot_input;
 1511:     close($fh);
 1512:     # That's all folks.
 1513:     return ;
 1514: }
 1515: 
 1516: #---------------------------------------------- check_inputs
 1517: sub check_inputs {
 1518:     ## Note: no inputs, no outputs - this acts only on global variables.
 1519:     ## Make sure we have all the input we need:
 1520:     if (! %Apache::lonplot::plot) { &set_defaults(\%Apache::lonplot::plot,\%gnuplot_defaults); }
 1521:     if (! %key ) {} # No key for this plot, thats okay
 1522: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
 1523:     if (! defined($title )) {} # No title for this plot, thats okay
 1524:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
 1525:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
 1526:     if ($#labels < 0) { }      # No labels for this plot, thats okay
 1527:     if ($#curves < 0) { 
 1528: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
 1529: 	return '';
 1530:     }
 1531:     my $curve;
 1532:     foreach $curve (@curves) {
 1533: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
 1534: 	    &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
 1535: 	    return '';
 1536: 	}
 1537:     }
 1538: }
 1539: 
 1540: #------------------------------------------------ make_edit
 1541: sub edit_attributes {
 1542:     my ($target,$token,$defaults,$keys) = @_;
 1543:     my ($result,@keys);
 1544:     if ($keys && ref($keys) eq 'ARRAY') {
 1545:         @keys = @$keys;
 1546:     } else {
 1547: 	@keys = sort(keys(%$defaults));
 1548:     }
 1549:     foreach my $attr (@keys) {
 1550: 	# append a ' ' to the description if it doesn't have one already.
 1551: 	my $description = $defaults->{$attr}->{'description'};
 1552: 	$description .= ' ' if ($description !~ / $/);
 1553: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
 1554: 	    $result .= &Apache::edit::text_arg
 1555: 		($description,$attr,$token,
 1556: 		 $defaults->{$attr}->{'size'});
 1557: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
 1558: 	    $result .= &Apache::edit::select_or_text_arg
 1559: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
 1560: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
 1561: 	    $result .= &Apache::edit::select_or_text_arg
 1562: 		($description,$attr,['on','off'],$token);
 1563: 	}
 1564: 	$result .= '<br />';
 1565:     }
 1566:     return $result;
 1567: }
 1568: 
 1569: 
 1570: ###################################################################
 1571: ##                                                               ##
 1572: ##           Insertion functions for editing plots               ##
 1573: ##                                                               ##
 1574: ###################################################################
 1575: 
 1576: sub insert_gnuplot {
 1577:     my $result = '';
 1578:     #  plot attributes
 1579:     $result .= "\n<gnuplot ";
 1580:     foreach my $attr (keys(%gnuplot_defaults)) {
 1581: 	$result .= "\n     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
 1582:     }
 1583:     $result .= ">";
 1584:     # Add the components (most are commented out for simplicity)
 1585:     # $result .= &insert_key();
 1586:     # $result .= &insert_axis();
 1587:     # $result .= &insert_title();    
 1588:     # $result .= &insert_xlabel();    
 1589:     # $result .= &insert_ylabel();    
 1590:     $result .= &insert_curve();
 1591:     # close up the <gnuplot>
 1592:     $result .= "\n</gnuplot>";
 1593:     return $result;
 1594: }
 1595: 
 1596: sub insert_tics {
 1597:     my $result;
 1598:     $result .= &insert_xtics() . &insert_ytics;
 1599:     return $result;
 1600: }
 1601: 
 1602: sub insert_xtics {
 1603:     my $result;
 1604:     $result .= "\n    <xtics ";
 1605:     foreach my $attr (keys(%tic_defaults)) {
 1606: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1607:     }
 1608:     $result .= "/>";
 1609:     return $result;
 1610: }
 1611: 
 1612: sub insert_ytics {
 1613:     my $result;
 1614:     $result .= "\n    <ytics ";
 1615:     foreach my $attr (keys(%tic_defaults)) {
 1616: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
 1617:     }
 1618:     $result .= "/>";
 1619:     return $result;
 1620: }
 1621: 
 1622: sub insert_key {
 1623:     my $result;
 1624:     $result .= "\n    <key ";
 1625:     foreach my $attr (keys(%key_defaults)) {
 1626: 	$result .= "\n         $attr=\"$key_defaults{$attr}->{'default'}\"";
 1627:     }
 1628:     $result .= " />";
 1629:     return $result;
 1630: }
 1631: 
 1632: sub insert_axis{
 1633:     my $result;
 1634:     $result .= "\n    <axis ";
 1635:    foreach my $attr (keys(%axis_defaults)) {
 1636: 	$result .= "\n         $attr=\"$axis_defaults{$attr}->{'default'}\"";
 1637:     }
 1638:     $result .= " />";
 1639:     return $result;
 1640: }
 1641: 
 1642: sub insert_title  { return "\n    <title></title>"; }
 1643: sub insert_xlabel { return "\n    <xlabel></xlabel>"; }
 1644: sub insert_ylabel { return "\n    <ylabel></ylabel>"; }
 1645: 
 1646: sub insert_label {
 1647:     my $result;
 1648:     $result .= "\n    <label ";
 1649:     foreach my $attr (keys(%label_defaults)) {
 1650: 	$result .= "\n         $attr=\"".
 1651:             $label_defaults{$attr}->{'default'}."\"";
 1652:     }
 1653:     $result .= "></label>";
 1654:     return $result;
 1655: }
 1656: 
 1657: sub insert_curve {
 1658:     my $result;
 1659:     $result .= "\n    <curve ";
 1660:     foreach my $attr (keys(%curve_defaults)) {
 1661: 	$result .= "\n         $attr=\"".
 1662: 	    $curve_defaults{$attr}->{'default'}."\"";
 1663:     }
 1664:     $result .= " >";
 1665:     $result .= &insert_data().&insert_data()."\n    </curve>";
 1666: }
 1667: 
 1668: sub insert_function {
 1669:     my $result;
 1670:     $result .= "\n        <function></function>";
 1671:     return $result;
 1672: }
 1673: 
 1674: sub insert_data {
 1675:     my $result;
 1676:     $result .= "\n        <data></data>";
 1677:     return $result;
 1678: }
 1679: 
 1680: ##----------------------------------------------------------------------
 1681: 1;
 1682: __END__
 1683: 
 1684: 

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