Annotation of loncom/xml/lonplot.pm, revision 1.125

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

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