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

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

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