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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.132   ! albertel    4: # $Id: lonplot.pm,v 1.131 2007/10/09 22:11:51 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 LONCAPA;
                     40:  
1.10      matthew    41: 
1.129     albertel   42: use vars qw/$weboutputformat $version/;
1.97      matthew    43: 
1.109     foxr       44: 
1.108     foxr       45: 
1.33      harris41   46: BEGIN {
1.97      matthew    47:     &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
                     48:     #
                     49:     # Determine the version of GNUPLOT
                     50:     $weboutputformat = 'gif';
1.129     albertel   51:     my $versionstring = `gnuplot --version 2>/dev/null`;
                     52:     ($version) = ($versionstring =~ /^gnuplot ([\d.]+)/);
                     53:     if ($version >= 4) {
1.97      matthew    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.126     albertel  211: 	 choices     => [['5','5 (small)'],'6','7','8',['9','9 (medium)'],'10',['11','11 (large)'],'12','15']
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.132   ! albertel  438: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax','xformat', 'yformat');
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.132   ! albertel  475:         },
        !           476:      xformat      => {
        !           477:          default     => 'on',
        !           478:          test        => sub {$_[0]=~/^(on|off|\d+(f|F|e|E))$/},
        !           479:          description => 'X-axis number formatting',
        !           480:          edit_type   => 'choice',
        !           481:          choices     => ['on', 'off', '2e', '2f'],
        !           482:          },
        !           483:      yformat      => {
        !           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: 
1.1       matthew   491:      );
                    492: 
1.119     albertel  493: my @curve_edit_order = ('color','name','linestyle','linewidth','pointtype','pointsize','limit');
1.60      matthew   494: 
1.1       matthew   495: my %curve_defaults = 
                    496:     (
1.20      matthew   497:      color     => {
                    498: 	 default => 'x000000',
                    499: 	 test => $color_test,
1.120     albertel  500: 	 description => 'Color of curve (x000000)',
1.38      matthew   501: 	 edit_type   => 'entry',
                    502: 	 size        => '10'
1.20      matthew   503: 	 },
                    504:      name      => {
                    505: 	 default => '',
                    506: 	 test => $words_test,
1.120     albertel  507: 	 description => 'Name of curve to appear in key',
1.38      matthew   508: 	 edit_type   => 'entry',
                    509: 	 size        => '20'
1.20      matthew   510: 	 },
                    511:      linestyle => {
                    512: 	 default => 'lines',
                    513: 	 test => $linestyle_test,
1.35      matthew   514: 	 description => 'Line style',
1.20      matthew   515: 	 edit_type   => 'choice',
1.38      matthew   516: 	 choices     => [keys(%linestyles)]
1.60      matthew   517: 	 },
1.119     albertel  518:      linewidth => {
1.130     albertel  519:          default     => 1,
1.119     albertel  520:          test        => $int_test,
                    521:          description => 'Line width (may not apply to all line styles)',
                    522:          edit_type   => 'choice',
                    523:          choices     => [1,2,3,4,5,6,7,8,9,10]
                    524:          },
1.60      matthew   525:      pointsize => {
                    526:          default     => 1,
1.62      matthew   527:          test        => $pos_real_test,
1.120     albertel  528:          description => 'Point size (may not apply to all line styles)',
1.62      matthew   529:          edit_type   => 'entry',
                    530:          size        => '5'
                    531:          },
                    532:      pointtype => {
                    533:          default     => 1,
1.60      matthew   534:          test        => $int_test,
1.120     albertel  535:          description => 'Point type (may not apply to all line styles)',
1.60      matthew   536:          edit_type   => 'choice',
1.62      matthew   537:          choices     => [0,1,2,3,4,5,6]
1.110     albertel  538:          },
                    539:      limit     => {
                    540:          default     => 'closed',
                    541: 	 test        => sub {$_[0]=~/^(closed|x1|x2|y1|y2)$/},
1.120     albertel  542:          description => 'Point to fill -- for filledcurves',
1.110     albertel  543:          edit_type   => 'choice',
                    544:          choices     => ['closed','x1','x2','y1','y2']
                    545:          },
1.1       matthew   546:      );
                    547: 
1.21      matthew   548: ###################################################################
                    549: ##                                                               ##
                    550: ##                    parsing and edit rendering                 ##
                    551: ##                                                               ##
                    552: ###################################################################
1.107     foxr      553: 
                    554: undef %Apache::lonplot::plot;
                    555: my (%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
1.1       matthew   556: 
1.47      matthew   557: sub start_gnuplot {
1.107     foxr      558:     undef(%Apache::lonplot::plot);   undef(%key);    undef(%axis);
1.89      matthew   559:     undef($title);  undef($xlabel); undef($ylabel);
                    560:     undef(@labels); undef(@curves);
                    561:     undef(%xtics);  undef(%ytics);
1.6       matthew   562:     #
1.1       matthew   563:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    564:     my $result='';
1.25      matthew   565:     &Apache::lonxml::register('Apache::lonplot',
1.45      matthew   566: 	     ('title','xlabel','ylabel','key','axis','label','curve',
                    567: 	      'xtics','ytics'));
1.29      matthew   568:     push (@Apache::lonxml::namespace,'lonplot');
1.51      matthew   569:     if ($target eq 'web' || $target eq 'tex') {
1.107     foxr      570: 	&get_attributes(\%Apache::lonplot::plot,\%gnuplot_defaults,$parstack,$safeeval,
1.17      matthew   571: 			$tagstack->[-1]);
1.20      matthew   572:     } elsif ($target eq 'edit') {
1.47      matthew   573: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
                    574: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
1.116     albertel  575: 				    \@gnuplot_edit_order)
                    576: 	    .&Apache::edit::end_row()
                    577: 	    .&Apache::edit::start_spanning_row();
1.20      matthew   578:     } elsif ($target eq 'modified') {
                    579: 	my $constructtag=&Apache::edit::get_new_args
1.47      matthew   580: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
1.20      matthew   581: 	if ($constructtag) {
                    582: 	    $result = &Apache::edit::rebuild_tag($token);
                    583: 	}
1.4       matthew   584:     }
1.21      matthew   585:     return $result;
1.1       matthew   586: }
                    587: 
1.47      matthew   588: sub end_gnuplot {
1.1       matthew   589:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    590:     pop @Apache::lonxml::namespace;
1.4       matthew   591:     &Apache::lonxml::deregister('Apache::lonplot',
                    592: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    593:     my $result = '';
1.56      albertel  594:     my $randnumber;
                    595:     # need to call rand everytime start_script would evaluate, as the
                    596:     # safe space rand number generator and the global rand generator 
1.95      www       597:     # are not separate
1.56      albertel  598:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    599: 	$target eq 'answer') {
                    600:       $randnumber=int(rand(1000));
                    601:     }
1.51      matthew   602:     if ($target eq 'web' || $target eq 'tex') {
1.21      matthew   603: 	&check_inputs(); # Make sure we have all the data we need
1.13      matthew   604: 	##
                    605: 	## Determine filename
1.4       matthew   606: 	my $tmpdir = '/home/httpd/perl/tmp/';
1.106     albertel  607: 	my $filename = $env{'user.name'}.'_'.$env{'user.domain'}.
1.69      matthew   608: 	    '_'.time.'_'.$$.$randnumber.'_plot';
1.4       matthew   609: 	## Write the plot description to the file
1.51      matthew   610: 	&write_gnuplot_file($tmpdir,$filename,$target);
1.113     www       611: 	$filename = &escape($filename);
1.4       matthew   612: 	## return image tag for the plot
1.51      matthew   613: 	if ($target eq 'web') {
                    614: 	    $result .= <<"ENDIMAGE";
1.114     albertel  615: <img src    = "/cgi-bin/plot.$weboutputformat?file=$filename.data" 
1.107     foxr      616:      width  = "$Apache::lonplot::plot{'width'}"
                    617:      height = "$Apache::lonplot::plot{'height'}"
                    618:      align  = "$Apache::lonplot::plot{'align'}"
                    619:      alt    = "$Apache::lonplot::plot{'alttag'}" />
1.12      matthew   620: ENDIMAGE
1.51      matthew   621:         } elsif ($target eq 'tex') {
1.107     foxr      622: 	    &Apache::lonxml::debug(" gnuplot wid = $Apache::lonplot::plot{'width'}");
                    623: 	    &Apache::lonxml::debug(" gnuplot ht  = $Apache::lonplot::plot{'height'}");
1.91      albertel  624: 	    #might be inside the safe space, register the URL for later
                    625: 	    &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
1.111     albertel  626: 	    $result  = "%DYNAMICIMAGE:$Apache::lonplot::plot{'width'}:$Apache::lonplot::plot{'height'}:$Apache::lonplot::plot{'texwidth'}\n";
1.109     foxr      627: 	    $result .= '\graphicspath{{/home/httpd/perl/tmp/}}'."\n";
1.113     www       628: 	    $result .= '\includegraphics[width='.$Apache::lonplot::plot{'texwidth'}.' mm]{'.&unescape($filename).'.eps}';
1.51      matthew   629: 	}
1.20      matthew   630:     } elsif ($target eq 'edit') {
1.21      matthew   631: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   632:     }
1.1       matthew   633:     return $result;
                    634: }
1.2       matthew   635: 
1.45      matthew   636: 
                    637: ##--------------------------------------------------------------- xtics
                    638: sub start_xtics {
                    639:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    640:     my $result='';
1.51      matthew   641:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   642: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
                    643: 		    $tagstack->[-1]);
                    644:     } elsif ($target eq 'edit') {
                    645: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
                    646: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    647: 				    \@tic_edit_order);
                    648:     } elsif ($target eq 'modified') {
                    649: 	my $constructtag=&Apache::edit::get_new_args
                    650: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    651: 	if ($constructtag) {
                    652: 	    $result = &Apache::edit::rebuild_tag($token);
                    653: 	}
                    654:     }
                    655:     return $result;
                    656: }
                    657: 
                    658: sub end_xtics {
                    659:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    660:     my $result = '';
1.51      matthew   661:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   662:     } elsif ($target eq 'edit') {
                    663: 	$result.=&Apache::edit::tag_end($target,$token);
                    664:     }
                    665:     return $result;
                    666: }
                    667: 
                    668: ##--------------------------------------------------------------- ytics
                    669: sub start_ytics {
                    670:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    671:     my $result='';
1.51      matthew   672:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   673: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
                    674: 		    $tagstack->[-1]);
                    675:     } elsif ($target eq 'edit') {
                    676: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
                    677: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    678: 				    \@tic_edit_order);
                    679:     } elsif ($target eq 'modified') {
                    680: 	my $constructtag=&Apache::edit::get_new_args
                    681: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    682: 	if ($constructtag) {
                    683: 	    $result = &Apache::edit::rebuild_tag($token);
                    684: 	}
                    685:     }
                    686:     return $result;
                    687: }
                    688: 
                    689: sub end_ytics {
                    690:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    691:     my $result = '';
1.51      matthew   692:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   693:     } elsif ($target eq 'edit') {
                    694: 	$result.=&Apache::edit::tag_end($target,$token);
                    695:     }
                    696:     return $result;
                    697: }
                    698: 
1.119     albertel  699: ##-----------------------------------------------------------------font
1.120     albertel  700: my %font_properties =
                    701:     (
                    702:      'classic'    => {
                    703: 	 face       => 'classic',
                    704: 	 file       => 'DejaVuSansMono-Bold',
                    705: 	 printname  => 'Helvetica',
1.121     albertel  706: 	 tex_no_file => 1,
1.120     albertel  707:      },
                    708:      'sans-serif' => {
                    709: 	 face       => 'sans-serif',
                    710: 	 file       => 'DejaVuSans',
                    711: 	 printname  => 'DejaVuSans',
                    712:      },
                    713:      'serif'      => {
                    714: 	 face       => 'serif',
                    715: 	 file       => 'DejaVuSerif',
                    716: 	 printname  => 'DejaVuSerif',
                    717:      },
                    718:      );
                    719: 
1.119     albertel  720: sub get_font {
1.124     albertel  721:     my ($target) = @_;
1.120     albertel  722:     my ($size, $selected_font);
                    723: 
1.119     albertel  724:     if ( $Apache::lonplot::plot{'font'} =~ /^(small|medium|large)/) {
1.120     albertel  725: 	$selected_font = $font_properties{'classic'};
1.119     albertel  726: 	if ( $Apache::lonplot::plot{'font'} eq 'small') {
                    727: 	    $size = '5';
                    728: 	} elsif ( $Apache::lonplot::plot{'font'} eq 'medium') {
                    729: 	    $size = '9';
                    730: 	} elsif ( $Apache::lonplot::plot{'font'} eq 'large') {
1.126     albertel  731: 	    $size = '11';
1.119     albertel  732: 	} else {
                    733: 	    $size = '9';
                    734: 	}
                    735:     } else {
1.122     albertel  736: 	$size = $Apache::lonplot::plot{'font'};
1.120     albertel  737: 	$selected_font = $font_properties{$Apache::lonplot::plot{'fontface'}};
1.119     albertel  738:     }
1.124     albertel  739:     if ($target eq 'tex' && defined($Apache::lonplot::plot{'texfont'})) {
                    740: 	$size = $Apache::lonplot::plot{'texfont'};
                    741:     }
1.120     albertel  742:     return ($size, $selected_font);
1.119     albertel  743: }
1.45      matthew   744: 
1.1       matthew   745: ##----------------------------------------------------------------- key
                    746: sub start_key {
                    747:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    748:     my $result='';
1.51      matthew   749:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   750: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   751: 		    $tagstack->[-1]);
1.20      matthew   752:     } elsif ($target eq 'edit') {
1.25      matthew   753: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21      matthew   754: 	$result .= &edit_attributes($target,$token,\%key_defaults);
1.20      matthew   755:     } elsif ($target eq 'modified') {
                    756: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   757: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
1.20      matthew   758: 	if ($constructtag) {
                    759: 	    $result = &Apache::edit::rebuild_tag($token);
                    760: 	}
1.4       matthew   761:     }
1.1       matthew   762:     return $result;
                    763: }
                    764: 
                    765: sub end_key {
                    766:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    767:     my $result = '';
1.51      matthew   768:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   769:     } elsif ($target eq 'edit') {
1.21      matthew   770: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   771:     }
1.1       matthew   772:     return $result;
                    773: }
1.21      matthew   774: 
1.128     albertel  775: sub parse_label {
                    776:     my ($target,$text) = @_;
                    777:     my $parser=HTML::LCParser->new(\$text);
                    778:     my $result;
                    779:     while (my $token=$parser->get_token) {
                    780: 	if ($token->[0] eq 'S') {
                    781: 	    if ($token->[1] eq 'sub') {
                    782: 		$result .= '_{';
                    783: 	    } elsif ($token->[1] eq 'sup') {
                    784: 		$result .= '^{';
                    785: 	    } else {
                    786: 		$result .= $token->[4];
                    787: 	    }
                    788: 	} elsif ($token->[0] eq 'E') {
                    789: 	    if ($token->[1] eq 'sub'
                    790: 		|| $token->[1] eq 'sup') {
                    791: 		$result .= '}';
                    792: 	    } else {
                    793: 		$result .= $token->[2];
                    794: 	    }
                    795: 	} elsif ($token->[0] eq 'T') {
                    796: 	    $result .= &replace_entities($target,$token->[1]);
                    797: 	}
                    798:     }
                    799:     return $result;
                    800: }
                    801: 
                    802: 
                    803: my %lookup = 
                    804:     ('(pi|#960)' => {'tex' => '{/Symbol p}', 'web' => "\x{3C0}"},);
                    805: 
                    806: sub replace_entities {
                    807:     my ($target,$text) = @_;
                    808:     $text =~ s{([_^~\{\}]|\\\\)}{\\\\$1}g;
                    809:     while (my ($re, $replace) = each(%lookup)) {
                    810: 	$text =~ s/&$re;/$replace->{$target}/g;
                    811:     }
                    812:     $text =~ s{(&)}{\\\\$1}g;
                    813:     return $text;
1.127     albertel  814: }
                    815: 
1.1       matthew   816: ##------------------------------------------------------------------- title
                    817: sub start_title {
                    818:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    819:     my $result='';
1.51      matthew   820:     if ($target eq 'web' || $target eq 'tex') {
1.112     albertel  821: 	$title = &Apache::lonxml::get_all_text("/title",$parser,$style);
1.58      matthew   822: 	$title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
1.49      matthew   823: 	$title =~ s/\n/ /g;
1.32      matthew   824: 	if (length($title) > $max_str_len) {
                    825: 	    $title = substr($title,0,$max_str_len);
                    826: 	}
1.128     albertel  827: 	$title = &parse_label($target,$title);
1.20      matthew   828:     } elsif ($target eq 'edit') {
1.25      matthew   829: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.112     albertel  830: 	my $text=&Apache::lonxml::get_all_text("/title",$parser,$style);
1.116     albertel  831: 	$result.=&Apache::edit::editline('',$text,'',60);
1.20      matthew   832:     } elsif ($target eq 'modified') {
1.42      matthew   833: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  834: 	$result.=&Apache::edit::modifiedfield("/title",$parser);
1.4       matthew   835:     }
1.1       matthew   836:     return $result;
                    837: }
                    838: 
                    839: sub end_title {
                    840:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    841:     my $result = '';
1.51      matthew   842:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   843:     } elsif ($target eq 'edit') {
1.27      matthew   844: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   845:     }
1.1       matthew   846:     return $result;
                    847: }
                    848: ##------------------------------------------------------------------- xlabel
                    849: sub start_xlabel {
                    850:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    851:     my $result='';
1.51      matthew   852:     if ($target eq 'web' || $target eq 'tex') {
1.112     albertel  853: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1.58      matthew   854: 	$xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
1.49      matthew   855: 	$xlabel =~ s/\n/ /g;
1.32      matthew   856: 	if (length($xlabel) > $max_str_len) {
                    857: 	    $xlabel = substr($xlabel,0,$max_str_len);
                    858: 	}
1.128     albertel  859: 	$xlabel = &parse_label($target,$xlabel);
1.20      matthew   860:     } elsif ($target eq 'edit') {
1.25      matthew   861: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.112     albertel  862: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$parser,$style);
1.116     albertel  863: 	$result.=&Apache::edit::editline('',$text,'',60);
1.20      matthew   864:     } elsif ($target eq 'modified') {
1.42      matthew   865: 	$result.=&Apache::edit::rebuild_tag($token);	
1.93      albertel  866: 	$result.=&Apache::edit::modifiedfield("/xlabel",$parser);
1.4       matthew   867:     }
1.1       matthew   868:     return $result;
                    869: }
                    870: 
                    871: sub end_xlabel {
                    872:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    873:     my $result = '';
1.51      matthew   874:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   875:     } elsif ($target eq 'edit') {
1.27      matthew   876: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   877:     }
1.1       matthew   878:     return $result;
                    879: }
1.21      matthew   880: 
1.1       matthew   881: ##------------------------------------------------------------------- ylabel
                    882: sub start_ylabel {
                    883:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    884:     my $result='';
1.51      matthew   885:     if ($target eq 'web' || $target eq 'tex') {
1.112     albertel  886: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1.58      matthew   887: 	$ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
1.49      matthew   888: 	$ylabel =~ s/\n/ /g;
1.32      matthew   889: 	if (length($ylabel) > $max_str_len) {
                    890: 	    $ylabel = substr($ylabel,0,$max_str_len);
                    891: 	}
1.128     albertel  892: 	$ylabel = &parse_label($target,$ylabel);
1.20      matthew   893:     } elsif ($target eq 'edit') {
1.25      matthew   894: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.112     albertel  895: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$parser,$style);
1.116     albertel  896: 	$result .= &Apache::edit::editline('',$text,'',60);
1.20      matthew   897:     } elsif ($target eq 'modified') {
1.42      matthew   898: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  899: 	$result.=&Apache::edit::modifiedfield("/ylabel",$parser);
1.4       matthew   900:     }
1.1       matthew   901:     return $result;
                    902: }
                    903: 
                    904: sub end_ylabel {
                    905:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    906:     my $result = '';
1.51      matthew   907:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   908:     } elsif ($target eq 'edit') {
1.27      matthew   909: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   910:     }
1.1       matthew   911:     return $result;
                    912: }
1.21      matthew   913: 
1.1       matthew   914: ##------------------------------------------------------------------- label
                    915: sub start_label {
                    916:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    917:     my $result='';
1.51      matthew   918:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   919: 	my %label;
                    920: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew   921: 		    $tagstack->[-1]);
1.112     albertel  922: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1.58      matthew   923: 	$text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
1.49      matthew   924: 	$text =~ s/\n/ /g;
1.32      matthew   925: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
1.128     albertel  926: 	$label{'text'} = &parse_label($target,$text);
1.17      matthew   927: 	push(@labels,\%label);
1.20      matthew   928:     } elsif ($target eq 'edit') {
1.25      matthew   929: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21      matthew   930: 	$result .= &edit_attributes($target,$token,\%label_defaults);
1.112     albertel  931: 	my $text = &Apache::lonxml::get_all_text("/label",$parser,$style);
1.39      matthew   932: 	$result .= &Apache::edit::end_row().
                    933: 	    &Apache::edit::start_spanning_row().
1.63      albertel  934: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   935:     } elsif ($target eq 'modified') {
1.42      matthew   936: 	&Apache::edit::get_new_args
1.24      matthew   937: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
1.42      matthew   938: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  939: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
1.4       matthew   940:     }
1.1       matthew   941:     return $result;
                    942: }
                    943: 
                    944: sub end_label {
                    945:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    946:     my $result = '';
1.51      matthew   947:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   948:     } elsif ($target eq 'edit') {
1.21      matthew   949: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   950:     }
1.1       matthew   951:     return $result;
                    952: }
                    953: 
                    954: ##------------------------------------------------------------------- curve
                    955: sub start_curve {
                    956:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    957:     my $result='';
1.25      matthew   958:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
                    959:     push (@Apache::lonxml::namespace,'curve');
1.51      matthew   960:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   961: 	my %curve;
                    962: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew   963: 		    $tagstack->[-1]);
1.17      matthew   964: 	push (@curves,\%curve);
1.20      matthew   965:     } elsif ($target eq 'edit') {
1.26      matthew   966: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
1.60      matthew   967: 	$result .= &edit_attributes($target,$token,\%curve_defaults,
1.116     albertel  968:                                     \@curve_edit_order)
                    969: 	    .&Apache::edit::end_row()
                    970: 	    .&Apache::edit::start_spanning_row();
                    971: 
1.20      matthew   972:     } elsif ($target eq 'modified') {
                    973: 	my $constructtag=&Apache::edit::get_new_args
1.35      matthew   974: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
1.20      matthew   975: 	if ($constructtag) {
                    976: 	    $result = &Apache::edit::rebuild_tag($token);
                    977: 	}
1.4       matthew   978:     }
1.1       matthew   979:     return $result;
                    980: }
                    981: 
                    982: sub end_curve {
                    983:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    984:     my $result = '';
1.25      matthew   985:     pop @Apache::lonxml::namespace;
                    986:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.51      matthew   987:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   988:     } elsif ($target eq 'edit') {
1.21      matthew   989: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   990:     }
1.1       matthew   991:     return $result;
                    992: }
1.21      matthew   993: 
1.1       matthew   994: ##------------------------------------------------------------ curve function
                    995: sub start_function {
                    996:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    997:     my $result='';
1.51      matthew   998:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   999: 	if (exists($curves[-1]->{'data'})) {
1.85      matthew  1000: 	    &Apache::lonxml::warning
                   1001:                 ('Use of the <b>curve function</b> tag precludes use of '.
                   1002:                  ' the <b>curve data</b> tag.  '.
                   1003:                  'The curve data tag will be omitted in favor of the '.
                   1004:                  'curve function declaration.');
1.17      matthew  1005: 	    delete $curves[-1]->{'data'} ;
                   1006: 	}
1.112     albertel 1007:         my $function = &Apache::lonxml::get_all_text("/function",$parser,
                   1008: 						     $style);
1.58      matthew  1009: 	$function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
                   1010: 	$curves[-1]->{'function'} = $function; 
1.20      matthew  1011:     } elsif ($target eq 'edit') {
1.37      matthew  1012: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1.112     albertel 1013: 	my $text = &Apache::lonxml::get_all_text("/function",$parser,$style);
1.116     albertel 1014: 	$result .= &Apache::edit::editline('',$text,'',60);
1.20      matthew  1015:     } elsif ($target eq 'modified') {
1.42      matthew  1016: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel 1017: 	$result.=&Apache::edit::modifiedfield("/function",$parser);
1.4       matthew  1018:     }
1.1       matthew  1019:     return $result;
                   1020: }
                   1021: 
                   1022: sub end_function {
                   1023:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1024:     my $result = '';
1.51      matthew  1025:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1026:     } elsif ($target eq 'edit') {
1.26      matthew  1027: 	$result .= &Apache::edit::end_table();
1.4       matthew  1028:     }
1.1       matthew  1029:     return $result;
                   1030: }
1.21      matthew  1031: 
1.1       matthew  1032: ##------------------------------------------------------------ curve  data
                   1033: sub start_data {
                   1034:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1035:     my $result='';
1.51      matthew  1036:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew  1037: 	if (exists($curves[-1]->{'function'})) {
1.85      matthew  1038: 	    &Apache::lonxml::warning
                   1039:                 ('Use of the <b>curve function</b> tag precludes use of '.
                   1040:                  ' the <b>curve data</b> tag.  '.
                   1041:                  'The curve function tag will be omitted in favor of the '.
                   1042:                  'curve data declaration.');
1.17      matthew  1043: 	    delete($curves[-1]->{'function'});
                   1044: 	}
1.112     albertel 1045: 	my $datatext = &Apache::lonxml::get_all_text("/data",$parser,$style);
1.58      matthew  1046: 	$datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
1.40      matthew  1047: 	# Deal with cases where we're given an array...
                   1048: 	if ($datatext =~ /^\@/) {
                   1049: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
                   1050: 					  $safeeval,1);
                   1051: 	}
1.49      matthew  1052: 	$datatext =~ s/\s+/ /g;
1.17      matthew  1053: 	# Need to do some error checking on the @data array - 
                   1054: 	# make sure it's all numbers and make sure each array 
                   1055: 	# is of the same length.
                   1056: 	my @data;
1.35      matthew  1057: 	if ($datatext =~ /,/) { # comma deliminated
1.17      matthew  1058: 	    @data = split /,/,$datatext;
1.95      www      1059: 	} else { # Assume it's space separated.
1.17      matthew  1060: 	    @data = split / /,$datatext;
                   1061: 	}
                   1062: 	for (my $i=0;$i<=$#data;$i++) {
                   1063: 	    # Check that it's non-empty
1.19      matthew  1064: 	    if (! defined($data[$i])) {
                   1065: 		&Apache::lonxml::warning(
1.85      matthew  1066: 		    'undefined curve data value.  Replacing with '.
1.19      matthew  1067: 		    ' pi/e = 1.15572734979092');
                   1068: 		$data[$i] = 1.15572734979092;
                   1069: 	    }
1.17      matthew  1070: 	    # Check that it's a number
1.19      matthew  1071: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
                   1072: 		&Apache::lonxml::warning(
1.85      matthew  1073: 		    'Bad curve data value of '.$data[$i].'  Replacing with '.
1.19      matthew  1074: 		    ' pi/e = 1.15572734979092');
                   1075: 		$data[$i] = 1.15572734979092;
                   1076: 	    }
1.17      matthew  1077: 	}
1.35      matthew  1078: 	# complain if the number of data points is not the same as
                   1079: 	# in previous sets of data.
1.36      matthew  1080: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
1.35      matthew  1081: 	    &Apache::lonxml::warning
                   1082: 		('Number of data points is not consistent with previous '.
                   1083: 		 'number of data points');
                   1084: 	}
1.17      matthew  1085: 	push  @{$curves[-1]->{'data'}},\@data;
1.20      matthew  1086:     } elsif ($target eq 'edit') {
1.37      matthew  1087: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1.112     albertel 1088: 	my $text = &Apache::lonxml::get_all_text("/data",$parser,$style);
1.116     albertel 1089: 	$result .= &Apache::edit::editline('',$text,'',60);
1.20      matthew  1090:     } elsif ($target eq 'modified') {
1.42      matthew  1091: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel 1092: 	$result.=&Apache::edit::modifiedfield("/data",$parser);
1.4       matthew  1093:     }
1.1       matthew  1094:     return $result;
                   1095: }
                   1096: 
                   1097: sub end_data {
                   1098:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1099:     my $result = '';
1.51      matthew  1100:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1101:     } elsif ($target eq 'edit') {
1.26      matthew  1102: 	$result .= &Apache::edit::end_table();
1.4       matthew  1103:     }
1.1       matthew  1104:     return $result;
                   1105: }
                   1106: 
                   1107: ##------------------------------------------------------------------- axis
                   1108: sub start_axis {
                   1109:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1110:     my $result='';
1.51      matthew  1111:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew  1112: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                   1113: 			$tagstack->[-1]);
1.20      matthew  1114:     } elsif ($target eq 'edit') {
1.25      matthew  1115: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.65      matthew  1116: 	$result .= &edit_attributes($target,$token,\%axis_defaults,
                   1117: 				    \@axis_edit_order);
1.20      matthew  1118:     } elsif ($target eq 'modified') {
1.29      matthew  1119: 	my $constructtag=&Apache::edit::get_new_args
                   1120: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
                   1121: 	if ($constructtag) {
                   1122: 	    $result = &Apache::edit::rebuild_tag($token);
                   1123: 	}
1.4       matthew  1124:     }
1.1       matthew  1125:     return $result;
                   1126: }
                   1127: 
                   1128: sub end_axis {
                   1129:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1130:     my $result = '';
1.51      matthew  1131:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew  1132:     } elsif ($target eq 'edit') {
1.21      matthew  1133: 	$result.=&Apache::edit::tag_end($target,$token);
1.20      matthew  1134:     } elsif ($target eq 'modified') {
1.4       matthew  1135:     }
1.1       matthew  1136:     return $result;
                   1137: }
                   1138: 
1.21      matthew  1139: ###################################################################
                   1140: ##                                                               ##
                   1141: ##        Utility Functions                                      ##
                   1142: ##                                                               ##
                   1143: ###################################################################
                   1144: 
1.13      matthew  1145: ##----------------------------------------------------------- set_defaults
                   1146: sub set_defaults {
1.21      matthew  1147:     my ($var,$defaults) = @_;
1.13      matthew  1148:     my $key;
1.24      matthew  1149:     foreach $key (keys(%$defaults)) {
1.13      matthew  1150: 	$var->{$key} = $defaults->{$key}->{'default'};
                   1151:     }
                   1152: }
                   1153: 
1.1       matthew  1154: ##------------------------------------------------------------------- misc
1.2       matthew  1155: sub get_attributes{
1.21      matthew  1156:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24      matthew  1157:     foreach my $attr (keys(%{$defaults})) {
1.92      matthew  1158: 	if ($attr eq 'texwidth' || $attr eq 'texfont') {
1.86      albertel 1159: 	    $values->{$attr} = 
                   1160: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
                   1161: 	} else {
                   1162: 	    $values->{$attr} = 
                   1163: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval);
                   1164: 	}
1.10      matthew  1165: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew  1166: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew  1167: 	    next;
                   1168: 	}
1.10      matthew  1169: 	my $test = $defaults->{$attr}->{'test'};
                   1170: 	if (! &$test($values->{$attr})) {
1.6       matthew  1171: 	    &Apache::lonxml::warning
                   1172: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew  1173: 		 .$defaults->{$attr}->{'default'} );
                   1174: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew  1175: 	}
1.2       matthew  1176:     }
1.11      matthew  1177:     return ;
1.6       matthew  1178: }
1.40      matthew  1179: 
1.15      matthew  1180: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew  1181: sub write_gnuplot_file {
1.51      matthew  1182:     my ($tmpdir,$filename,$target)= @_;
1.124     albertel 1183:     my ($fontsize, $font_properties) =  &get_font($target);
1.6       matthew  1184:     my $gnuplot_input = '';
1.10      matthew  1185:     my $curve;
1.100     matthew  1186:     #
                   1187:     # Check to be sure we do not have any empty curves
                   1188:     my @curvescopy;
                   1189:     foreach my $curve (@curves) {
                   1190:         if (exists($curve->{'function'})) {
                   1191:             if ($curve->{'function'} !~ /^\s*$/) {
                   1192:                 push(@curvescopy,$curve);
                   1193:             }
                   1194:         } elsif (exists($curve->{'data'})) {
                   1195:             foreach my $data (@{$curve->{'data'}}) {
                   1196:                 if (scalar(@$data) > 0) {
                   1197:                     push(@curvescopy,$curve);
                   1198:                     last;
                   1199:                 }
                   1200:             }
                   1201:         }
                   1202:     }
                   1203:     @curves = @curvescopy;
1.6       matthew  1204:     # Collect all the colors
                   1205:     my @Colors;
1.107     foxr     1206:     push @Colors, $Apache::lonplot::plot{'bgcolor'};
                   1207:     push @Colors, $Apache::lonplot::plot{'fgcolor'}; 
                   1208:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$Apache::lonplot::plot{'fgcolor'});
1.9       matthew  1209:     foreach $curve (@curves) {
                   1210: 	push @Colors, ($curve->{'color'} ne '' ? 
                   1211: 		       $curve->{'color'}       : 
1.107     foxr     1212: 		       $Apache::lonplot::plot{'fgcolor'}        );
1.6       matthew  1213:     }
                   1214:     # set term
1.51      matthew  1215:     if ($target eq 'web') {
1.120     albertel 1216: 	$gnuplot_input .= 'set terminal png enhanced nocrop ';
1.107     foxr     1217: 	$gnuplot_input .= 'transparent ' if ($Apache::lonplot::plot{'transparent'} eq 'on');
1.120     albertel 1218: 	$gnuplot_input .= 'font "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
                   1219: 	    '/'.$font_properties->{'file'}.'.ttf" ';
                   1220: 	$gnuplot_input .= $fontsize;
                   1221: 	$gnuplot_input .= ' size '.$Apache::lonplot::plot{'width'}.','.$Apache::lonplot::plot{'height'}.' ';
1.51      matthew  1222: 	$gnuplot_input .= "@Colors\n";
                   1223: 	# set output
                   1224: 	$gnuplot_input .= "set output\n";
                   1225:     } elsif ($target eq 'tex') {
1.120     albertel 1226: 	$gnuplot_input .= "set term postscript eps enhanced $Apache::lonplot::plot{'plotcolor'} solid ";
1.121     albertel 1227: 	if (!$font_properties->{'tex_no_file'}) {
                   1228: 	    $gnuplot_input .=
                   1229: 		'fontfile "'.$Apache::lonnet::perlvar{'lonFontsDir'}.
                   1230: 		'/'.$font_properties->{'file'}.'.pfb" ';
                   1231: 	}
1.120     albertel 1232: 	$gnuplot_input .= ' "'.$font_properties->{'printname'}.'" ';
                   1233: 	$gnuplot_input .= $fontsize;
                   1234: 	$gnuplot_input .= "\nset output \"/home/httpd/perl/tmp/".
1.113     www      1235: 	    &unescape($filename).".eps\"\n";
1.87      matthew  1236:     }
1.115     albertel 1237:     # cartesian or polar plot?
1.107     foxr     1238:     if (lc($Apache::lonplot::plot{'plottype'}) eq 'polar') {
1.87      matthew  1239:         $gnuplot_input .= 'set polar'.$/;
                   1240:     } else {
                   1241:         # Assume Cartesian
1.51      matthew  1242:     }
1.115     albertel 1243:     # cartesian or polar grid?
                   1244:     if (lc($Apache::lonplot::plot{'gridtype'}) eq 'polar') {
                   1245:         $gnuplot_input .= 'set grid polar'.$/;
1.118     albertel 1246:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'linear-log') {
                   1247:         $gnuplot_input .= 'set logscale x'.$/;
                   1248:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-linear') {
                   1249:         $gnuplot_input .= 'set logscale y'.$/;
                   1250:     } elsif (lc($Apache::lonplot::plot{'gridtype'}) eq 'log-log') {
                   1251:         $gnuplot_input .= 'set logscale x'.$/;
                   1252:         $gnuplot_input .= 'set logscale y'.$/;
1.115     albertel 1253:     } else {
                   1254:         # Assume Cartesian
                   1255:     }
1.110     albertel 1256:     # solid or pattern for boxes?
                   1257:     if (lc($Apache::lonplot::plot{'fillstyle'}) eq 'solid') {
                   1258:         $gnuplot_input .= 'set style fill solid '.
                   1259: 	    $Apache::lonplot::plot{'solid'}.$Apache::lonplot::plot{'box_border'}.$/;
                   1260:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'pattern') {
                   1261:         $gnuplot_input .= 'set style fill pattern '.$Apache::lonplot::plot{'pattern'}.$Apache::lonplot::plot{'box_border'}.$/;
                   1262:     } elsif (lc($Apache::lonplot::plot{'fillstyle'}) eq 'empty') {
                   1263:     }
1.101     matthew  1264:     # margin
1.107     foxr     1265:     if (lc($Apache::lonplot::plot{'lmargin'}) ne 'default') {
                   1266:         $gnuplot_input .= 'set lmargin '.$Apache::lonplot::plot{'lmargin'}.$/;
1.101     matthew  1267:     }
1.107     foxr     1268:     if (lc($Apache::lonplot::plot{'rmargin'}) ne 'default') {
                   1269:         $gnuplot_input .= 'set rmargin '.$Apache::lonplot::plot{'rmargin'}.$/;
1.101     matthew  1270:     }
1.107     foxr     1271:     if (lc($Apache::lonplot::plot{'tmargin'}) ne 'default') {
                   1272:         $gnuplot_input .= 'set tmargin '.$Apache::lonplot::plot{'tmargin'}.$/;
1.101     matthew  1273:     }
1.107     foxr     1274:     if (lc($Apache::lonplot::plot{'bmargin'}) ne 'default') {
                   1275:         $gnuplot_input .= 'set bmargin '.$Apache::lonplot::plot{'bmargin'}.$/;
1.101     matthew  1276:     }
1.129     albertel 1277: 
1.101     matthew  1278:     # tic scales
1.129     albertel 1279:     if ($version > 4) {
                   1280: 	$gnuplot_input .= 'set tics scale '.
                   1281: 	    $Apache::lonplot::plot{'major_ticscale'}.', '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
                   1282:     } else {
                   1283:     	$gnuplot_input .= 'set ticscale '.
                   1284: 	    $Apache::lonplot::plot{'major_ticscale'}.' '.$Apache::lonplot::plot{'minor_ticscale'}.$/;
                   1285:     }
1.110     albertel 1286:     #boxwidth
                   1287:     if (lc($Apache::lonplot::plot{'boxwidth'}) ne '') {
                   1288: 	$gnuplot_input .= 'set boxwidth '.$Apache::lonplot::plot{'boxwidth'}.$/;
                   1289:     }
                   1290:     # gridlayer
                   1291:     $gnuplot_input .= 'set grid noxtics noytics front '.$/ 
                   1292: 	if ($Apache::lonplot::plot{'gridlayer'} eq 'on');
                   1293: 
1.7       matthew  1294:     # grid
1.107     foxr     1295:     $gnuplot_input .= 'set grid'.$/ if ($Apache::lonplot::plot{'grid'} eq 'on');
1.7       matthew  1296:     # border
1.107     foxr     1297:     $gnuplot_input .= ($Apache::lonplot::plot{'border'} eq 'on'?
1.9       matthew  1298: 		       'set border'.$/           :
1.67      matthew  1299: 		       'set noborder'.$/         );
1.77      matthew  1300:     # sampling rate for non-data curves
1.107     foxr     1301:     $gnuplot_input .= "set samples $Apache::lonplot::plot{'samples'}\n";
1.67      matthew  1302:     # title, xlabel, ylabel
1.45      matthew  1303:     # titles
1.125     albertel 1304:     my $extra_space_x = ($xtics{'location'} eq 'axis') ? ' 0, -0.5 ' : '';
                   1305:     my $extra_space_y = ($ytics{'location'} eq 'axis') ? ' -0.5, 0 ' : '';
                   1306: 
1.89      matthew  1307:     if ($target eq 'tex') {
1.125     albertel 1308: 	$gnuplot_input .= "set title  \"$title\"          font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($title)) ;
                   1309: 	$gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($xlabel));
                   1310: 	$gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y font \"".$font_properties->{'printname'}.",".$fontsize."pt\"\n" if (defined($ylabel));
1.89      matthew  1311:     } else {
1.125     albertel 1312:         $gnuplot_input .= "set title  \"$title\"          \n" if (defined($title)) ;
                   1313:         $gnuplot_input .= "set xlabel \"$xlabel\" $extra_space_x \n" if (defined($xlabel));
                   1314:         $gnuplot_input .= "set ylabel \"$ylabel\" $extra_space_y \n" if (defined($ylabel));
1.89      matthew  1315:     }
1.45      matthew  1316:     # tics
                   1317:     if (%xtics) {    
                   1318: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
1.46      matthew  1319: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1320: 	$gnuplot_input .= "$xtics{'start'}, ";
                   1321: 	$gnuplot_input .= "$xtics{'increment'}, ";
                   1322: 	$gnuplot_input .= "$xtics{'end'}\n";
1.89      matthew  1323:         if ($xtics{'minorfreq'} != 0) {
                   1324:             $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
                   1325:         } 
1.45      matthew  1326:     }
                   1327:     if (%ytics) {    
                   1328: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
1.46      matthew  1329: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1330: 	$gnuplot_input .= "$ytics{'start'}, ";
                   1331: 	$gnuplot_input .= "$ytics{'increment'}, ";
                   1332:         $gnuplot_input .= "$ytics{'end'}\n";
1.89      matthew  1333:         if ($ytics{'minorfreq'} != 0) {
                   1334:             $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
                   1335:         } 
1.45      matthew  1336:     }
                   1337:     # axis
1.23      matthew  1338:     if (%axis) {
1.132   ! albertel 1339:         if ($axis{'xformat'} ne 'on') {
        !          1340:             $gnuplot_input .= "set format x ";
        !          1341:             if ($axis{'xformat'} eq 'off') {
        !          1342:                 $gnuplot_input .= "\"\"\n";
        !          1343:             } else {
        !          1344:                 $gnuplot_input .= "\"\%.".$axis{'xformat'}."\"\n";
        !          1345:             }
        !          1346:         }
        !          1347:         if ($axis{'yformat'} ne 'on') {
        !          1348:             $gnuplot_input .= "set format y ";
        !          1349:             if ($axis{'yformat'} eq 'off') {
        !          1350:                 $gnuplot_input .= "\"\"\n";
        !          1351:             } else {
        !          1352:                 $gnuplot_input .= "\"\%.".$axis{'yformat'}."\"\n";
        !          1353:             }
        !          1354:         }
1.13      matthew  1355: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                   1356: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6       matthew  1357:     }
                   1358:     # Key
1.23      matthew  1359:     if (%key) {
1.9       matthew  1360: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                   1361: 	if ($key{'title'} ne '') {
1.67      matthew  1362: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
1.11      matthew  1363: 	} 
                   1364: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew  1365:     } else {
1.9       matthew  1366: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew  1367:     }
1.6       matthew  1368:     # labels
1.10      matthew  1369:     my $label;
1.6       matthew  1370:     foreach $label (@labels) {
                   1371: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.103     matthew  1372: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'};
                   1373:         if ($target eq 'tex') {
1.124     albertel 1374: 	    $gnuplot_input .=' font "'.$font_properties->{'printname'}.','.$fontsize.'pt"' ;
1.103     matthew  1375:         }
                   1376:         $gnuplot_input .= $/;
1.6       matthew  1377:     }
1.74      matthew  1378:     if ($target eq 'tex') {
1.107     foxr     1379:         $gnuplot_input .="set size 1,".$Apache::lonplot::plot{'height'}/$Apache::lonplot::plot{'width'}*1.38;
1.74      matthew  1380:         $gnuplot_input .="\n";
1.120     albertel 1381:     }
1.6       matthew  1382:     # curves
                   1383:     $gnuplot_input .= 'plot ';
1.9       matthew  1384:     for (my $i = 0;$i<=$#curves;$i++) {
                   1385: 	$curve = $curves[$i];
                   1386: 	$gnuplot_input.= ', ' if ($i > 0);
1.119     albertel 1387: 	if ($target eq 'tex') {
                   1388: 	    $curve->{'linewidth'} *= 2;
                   1389: 	}
1.6       matthew  1390: 	if (exists($curve->{'function'})) {
1.9       matthew  1391: 	    $gnuplot_input.= 
                   1392: 		$curve->{'function'}.' title "'.
                   1393: 		$curve->{'name'}.'" with '.
1.72      matthew  1394:                 $curve->{'linestyle'};
1.119     albertel 1395: 
1.60      matthew  1396:             if (($curve->{'linestyle'} eq 'points')      ||
                   1397:                 ($curve->{'linestyle'} eq 'linespoints') ||
                   1398:                 ($curve->{'linestyle'} eq 'errorbars')   ||
                   1399:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
                   1400:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
                   1401:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
1.62      matthew  1402:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
1.60      matthew  1403:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
1.110     albertel 1404:             } elsif ($curve->{'linestyle'} eq 'filledcurves') { 
                   1405:                 $gnuplot_input.= ' '.$curve->{'limit'};
1.60      matthew  1406:             }
1.131     albertel 1407:             $gnuplot_input.= ' linewidth '.$curve->{'linewidth'};
                   1408: 
1.6       matthew  1409: 	} elsif (exists($curve->{'data'})) {
1.40      matthew  1410: 	    # Store data values in $datatext
                   1411: 	    my $datatext = '';
                   1412: 	    #   get new filename
1.70      matthew  1413: 	    my $datafilename = "$tmpdir/$filename.data.$i";
1.40      matthew  1414: 	    my $fh=Apache::File->new(">$datafilename");
                   1415: 	    # Compile data
1.6       matthew  1416: 	    my @Data = @{$curve->{'data'}};
1.9       matthew  1417: 	    my @Data0 = @{$Data[0]};
                   1418: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew  1419: 		my $dataset;
1.6       matthew  1420: 		foreach $dataset (@Data) {
1.9       matthew  1421: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew  1422: 		}
1.9       matthew  1423: 		$datatext .= $/;
1.6       matthew  1424: 	    }
1.40      matthew  1425: 	    #   write file
                   1426: 	    print $fh $datatext;
1.119     albertel 1427: 	    close($fh);
1.40      matthew  1428: 	    #   generate gnuplot text
                   1429: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
                   1430: 		$curve->{'name'}.'" with '.
                   1431: 		$curve->{'linestyle'};
1.62      matthew  1432:             if (($curve->{'linestyle'} eq 'points')      ||
                   1433:                 ($curve->{'linestyle'} eq 'linespoints') ||
                   1434:                 ($curve->{'linestyle'} eq 'errorbars')   ||
                   1435:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
                   1436:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
                   1437:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
                   1438:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
                   1439:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
1.110     albertel 1440:             } elsif ($curve->{'linestyle'} eq 'filledcurves') { 
                   1441:                 $gnuplot_input.= ' '.$curve->{'limit'};
1.62      matthew  1442:             }
1.131     albertel 1443:             $gnuplot_input.= ' linewidth '.$curve->{'linewidth'};
1.6       matthew  1444: 	}
                   1445:     }
1.40      matthew  1446:     # Write the output to a file.
1.128     albertel 1447:     open (my $fh,">$tmpdir$filename.data");
                   1448:     binmode($fh, ":utf8");
1.40      matthew  1449:     print $fh $gnuplot_input;
                   1450:     close($fh);
                   1451:     # That's all folks.
                   1452:     return ;
1.2       matthew  1453: }
1.21      matthew  1454: 
                   1455: #---------------------------------------------- check_inputs
                   1456: sub check_inputs {
                   1457:     ## Note: no inputs, no outputs - this acts only on global variables.
                   1458:     ## Make sure we have all the input we need:
1.107     foxr     1459:     if (! %Apache::lonplot::plot) { &set_defaults(\%Apache::lonplot::plot,\%gnuplot_defaults); }
1.23      matthew  1460:     if (! %key ) {} # No key for this plot, thats okay
1.34      matthew  1461: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21      matthew  1462:     if (! defined($title )) {} # No title for this plot, thats okay
                   1463:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
                   1464:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
                   1465:     if ($#labels < 0) { }      # No labels for this plot, thats okay
                   1466:     if ($#curves < 0) { 
                   1467: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
                   1468: 	return '';
                   1469:     }
                   1470:     my $curve;
                   1471:     foreach $curve (@curves) {
                   1472: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
1.85      matthew  1473: 	    &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
1.21      matthew  1474: 	    return '';
                   1475: 	}
                   1476:     }
                   1477: }
                   1478: 
1.20      matthew  1479: #------------------------------------------------ make_edit
                   1480: sub edit_attributes {
1.34      matthew  1481:     my ($target,$token,$defaults,$keys) = @_;
                   1482:     my ($result,@keys);
                   1483:     if ($keys && ref($keys) eq 'ARRAY') {
                   1484:         @keys = @$keys;
                   1485:     } else {
                   1486: 	@keys = sort(keys(%$defaults));
                   1487:     }
                   1488:     foreach my $attr (@keys) {
1.35      matthew  1489: 	# append a ' ' to the description if it doesn't have one already.
                   1490: 	my $description = $defaults->{$attr}->{'description'};
                   1491: 	$description .= ' ' if ($description !~ / $/);
1.20      matthew  1492: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1.35      matthew  1493: 	    $result .= &Apache::edit::text_arg
1.38      matthew  1494: 		($description,$attr,$token,
                   1495: 		 $defaults->{$attr}->{'size'});
1.20      matthew  1496: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1.102     albertel 1497: 	    $result .= &Apache::edit::select_or_text_arg
1.35      matthew  1498: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
1.45      matthew  1499: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1.102     albertel 1500: 	    $result .= &Apache::edit::select_or_text_arg
1.35      matthew  1501: 		($description,$attr,['on','off'],$token);
1.20      matthew  1502: 	}
1.25      matthew  1503: 	$result .= '<br />';
1.20      matthew  1504:     }
                   1505:     return $result;
                   1506: }
1.1       matthew  1507: 
1.21      matthew  1508: 
                   1509: ###################################################################
                   1510: ##                                                               ##
                   1511: ##           Insertion functions for editing plots               ##
                   1512: ##                                                               ##
                   1513: ###################################################################
                   1514: 
1.47      matthew  1515: sub insert_gnuplot {
1.29      matthew  1516:     my $result = '';
1.20      matthew  1517:     #  plot attributes
1.61      matthew  1518:     $result .= "\n<gnuplot ";
1.47      matthew  1519:     foreach my $attr (keys(%gnuplot_defaults)) {
1.61      matthew  1520: 	$result .= "\n     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
1.20      matthew  1521:     }
1.61      matthew  1522:     $result .= ">";
1.47      matthew  1523:     # Add the components (most are commented out for simplicity)
1.44      matthew  1524:     # $result .= &insert_key();
                   1525:     # $result .= &insert_axis();
                   1526:     # $result .= &insert_title();    
                   1527:     # $result .= &insert_xlabel();    
                   1528:     # $result .= &insert_ylabel();    
1.20      matthew  1529:     $result .= &insert_curve();
1.50      matthew  1530:     # close up the <gnuplot>
1.61      matthew  1531:     $result .= "\n</gnuplot>";
1.45      matthew  1532:     return $result;
                   1533: }
                   1534: 
1.46      matthew  1535: sub insert_tics {
                   1536:     my $result;
                   1537:     $result .= &insert_xtics() . &insert_ytics;
                   1538:     return $result;
                   1539: }
                   1540: 
1.45      matthew  1541: sub insert_xtics {
                   1542:     my $result;
1.46      matthew  1543:     $result .= "\n    <xtics ";
1.45      matthew  1544:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  1545: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1546:     }
1.61      matthew  1547:     $result .= "/>";
1.45      matthew  1548:     return $result;
                   1549: }
                   1550: 
                   1551: sub insert_ytics {
                   1552:     my $result;
1.46      matthew  1553:     $result .= "\n    <ytics ";
1.45      matthew  1554:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  1555: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1556:     }
1.61      matthew  1557:     $result .= "/>";
1.20      matthew  1558:     return $result;
                   1559: }
                   1560: 
                   1561: sub insert_key {
                   1562:     my $result;
1.61      matthew  1563:     $result .= "\n    <key ";
1.30      matthew  1564:     foreach my $attr (keys(%key_defaults)) {
1.61      matthew  1565: 	$result .= "\n         $attr=\"$key_defaults{$attr}->{'default'}\"";
1.20      matthew  1566:     }
1.61      matthew  1567:     $result .= " />";
1.20      matthew  1568:     return $result;
                   1569: }
                   1570: 
                   1571: sub insert_axis{
                   1572:     my $result;
1.46      matthew  1573:     $result .= "\n    <axis ";
1.30      matthew  1574:    foreach my $attr (keys(%axis_defaults)) {
1.61      matthew  1575: 	$result .= "\n         $attr=\"$axis_defaults{$attr}->{'default'}\"";
1.20      matthew  1576:     }
1.61      matthew  1577:     $result .= " />";
1.20      matthew  1578:     return $result;
                   1579: }
1.28      matthew  1580: 
1.61      matthew  1581: sub insert_title  { return "\n    <title></title>"; }
                   1582: sub insert_xlabel { return "\n    <xlabel></xlabel>"; }
                   1583: sub insert_ylabel { return "\n    <ylabel></ylabel>"; }
1.20      matthew  1584: 
                   1585: sub insert_label {
                   1586:     my $result;
1.46      matthew  1587:     $result .= "\n    <label ";
1.30      matthew  1588:     foreach my $attr (keys(%label_defaults)) {
1.61      matthew  1589: 	$result .= "\n         $attr=\"".
                   1590:             $label_defaults{$attr}->{'default'}."\"";
1.20      matthew  1591:     }
1.61      matthew  1592:     $result .= "></label>";
1.20      matthew  1593:     return $result;
                   1594: }
                   1595: 
                   1596: sub insert_curve {
                   1597:     my $result;
1.41      matthew  1598:     $result .= "\n    <curve ";
1.30      matthew  1599:     foreach my $attr (keys(%curve_defaults)) {
1.61      matthew  1600: 	$result .= "\n         $attr=\"".
                   1601: 	    $curve_defaults{$attr}->{'default'}."\"";
1.20      matthew  1602:     }
1.61      matthew  1603:     $result .= " >";
                   1604:     $result .= &insert_data().&insert_data()."\n    </curve>";
1.20      matthew  1605: }
1.4       matthew  1606: 
1.20      matthew  1607: sub insert_function {
                   1608:     my $result;
1.61      matthew  1609:     $result .= "\n        <function></function>";
1.20      matthew  1610:     return $result;
                   1611: }
1.4       matthew  1612: 
1.20      matthew  1613: sub insert_data {
                   1614:     my $result;
1.61      matthew  1615:     $result .= "\n        <data></data>";
1.20      matthew  1616:     return $result;
                   1617: }
1.4       matthew  1618: 
1.48      matthew  1619: ##----------------------------------------------------------------------
1.20      matthew  1620: 1;
                   1621: __END__
1.4       matthew  1622: 
                   1623: 

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