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

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

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