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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.96    ! matthew     4: # $Id: lonplot.pm,v 1.95 2004/03/08 17:31:37 www 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.10      matthew    38: 
1.33      harris41   39: BEGIN {
1.47      matthew    40:   &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
1.1       matthew    41: }
                     42: 
1.10      matthew    43: ## 
                     44: ## Description of data structures:
                     45: ##
                     46: ##  %plot       %key    %axis
                     47: ## --------------------------
                     48: ##  height      title   color
                     49: ##  width       box     xmin
                     50: ##  bgcolor     pos     xmax
                     51: ##  fgcolor             ymin
                     52: ##  transparent         ymax
                     53: ##  grid
                     54: ##  border
                     55: ##  font
1.19      matthew    56: ##  align
1.10      matthew    57: ##
                     58: ##  @labels: $labels[$i] = \%label
                     59: ##           %label: text, xpos, ypos, justify
1.14      matthew    60: ##
1.10      matthew    61: ##  @curves: $curves[$i] = \%curve
1.14      matthew    62: ##           %curve: name, linestyle, ( function | data )
1.10      matthew    63: ##
                     64: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
                     65: ##                            [y1,y2,y3,y4] ]
                     66: ##
1.21      matthew    67: 
                     68: ###################################################################
                     69: ##                                                               ##
                     70: ##        Tests used in checking the validitity of input         ##
                     71: ##                                                               ##
                     72: ###################################################################
1.29      matthew    73: 
1.32      matthew    74: my $max_str_len = 50;    # if a label, title, xlabel, or ylabel text
                     75:                          # is longer than this, it will be truncated.
                     76: 
1.29      matthew    77: my %linestyles = 
                     78:     (
                     79:      lines          => 2,     # Maybe this will be used in the future
                     80:      linespoints    => 2,     # to check on whether or not they have 
                     81:      dots	    => 2,     # supplied enough <data></data> fields
                     82:      points         => 2,     # to use the given line style.  But for
                     83:      steps	    => 2,     # now there are more important things 
                     84:      fsteps	    => 2,     # for me to deal with.
                     85:      histeps        => 2,
1.34      matthew    86:      errorbars	    => 3,
                     87:      xerrorbars	    => [3,4],
                     88:      yerrorbars	    => [3,4],
1.35      matthew    89:      xyerrorbars    => [4,6],
1.34      matthew    90:      boxes          => 3,
1.35      matthew    91:      vector	    => 4
1.29      matthew    92:     );		    
                     93: 
1.11      matthew    94: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19      matthew    95: my $real_test      = 
                     96:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.62      matthew    97: my $pos_real_test  =
                     98:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
1.79      matthew    99: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-fA-F]{6}$/};
1.1       matthew   100: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
1.15      matthew   101: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.1       matthew   102: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
1.29      matthew   103: my $linestyle_test = sub {exists($linestyles{$_[0]})};
1.67      matthew   104: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w~!\@\#\$\%^&\*\(\)-=_\+\[\]\{\}:\;\'<>,\.\/\?\\]+ ?)+$/};
1.21      matthew   105: 
                    106: ###################################################################
                    107: ##                                                               ##
                    108: ##                      Attribute metadata                       ##
                    109: ##                                                               ##
                    110: ###################################################################
1.47      matthew   111: my @gnuplot_edit_order = 
1.82      matthew   112:     qw/alttag bgcolor fgcolor height width font transparent grid samples 
1.92      matthew   113:     border align texwidth texfont plottype/;
1.48      matthew   114: 
1.47      matthew   115: my %gnuplot_defaults = 
1.1       matthew   116:     (
1.64      matthew   117:      alttag       => {
                    118: 	 default     => 'dynamically generated plot',
                    119: 	 test        => $words_test,
                    120: 	 description => 'brief description of the plot',
                    121:       	 edit_type   => 'entry',
                    122: 	 size        => '40'
                    123: 	 },
1.20      matthew   124:      height       => {
1.65      matthew   125: 	 default     => 300,
1.20      matthew   126: 	 test        => $int_test,
1.29      matthew   127: 	 description => 'height of image (pixels)',
1.38      matthew   128:       	 edit_type   => 'entry',
                    129: 	 size        => '10'
1.20      matthew   130: 	 },
                    131:      width        => {
1.65      matthew   132: 	 default     => 400,
1.20      matthew   133: 	 test        => $int_test,
1.29      matthew   134: 	 description => 'width of image (pixels)',
1.38      matthew   135: 	 edit_type   => 'entry',
                    136: 	 size        => '10'
1.20      matthew   137: 	 },
                    138:      bgcolor      => {
                    139: 	 default     => 'xffffff',
                    140: 	 test        => $color_test, 
                    141: 	 description => 'background color of image (xffffff)',
1.38      matthew   142: 	 edit_type   => 'entry',
                    143: 	 size        => '10'
1.20      matthew   144: 	 },
                    145:      fgcolor      => {
                    146: 	 default     => 'x000000',
                    147: 	 test        => $color_test,
                    148: 	 description => 'foreground color of image (x000000)',
1.38      matthew   149: 	 edit_type   => 'entry',
                    150: 	 size        => '10'
1.20      matthew   151: 	 },
                    152:      transparent  => {
                    153: 	 default     => 'off',
                    154: 	 test        => $onoff_test, 
1.34      matthew   155: 	 description => 'Transparent image',
1.45      matthew   156: 	 edit_type   => 'onoff'
1.20      matthew   157: 	 },
                    158:      grid         => {
1.65      matthew   159: 	 default     => 'on',
1.20      matthew   160: 	 test        => $onoff_test, 
1.34      matthew   161: 	 description => 'Display grid',
1.45      matthew   162: 	 edit_type   => 'onoff'
1.20      matthew   163: 	 },
                    164:      border       => {
                    165: 	 default     => 'on',
                    166: 	 test        => $onoff_test, 
1.34      matthew   167: 	 description => 'Draw border around plot',
1.45      matthew   168: 	 edit_type   => 'onoff'
1.20      matthew   169: 	 },
                    170:      font         => {
                    171: 	 default     => 'medium',
                    172: 	 test        => $sml_test,
                    173: 	 description => 'Size of font to use',
                    174: 	 edit_type   => 'choice',
                    175: 	 choices     => ['small','medium','large']
                    176: 	 },
1.77      matthew   177:      samples         => {
                    178: 	 default     => '100',
                    179: 	 test        => $int_test,
                    180: 	 description => 'Number of samples for non-data plots',
                    181: 	 edit_type   => 'choice',
                    182: 	 choices     => ['100','200','500','1000','2000','5000']
                    183: 	 },
1.20      matthew   184:      align        => {
1.65      matthew   185: 	 default     => 'center',
1.20      matthew   186: 	 test        => sub {$_[0]=~/^(left|right|center)$/},
                    187: 	 description => 'alignment for image in html',
                    188: 	 edit_type   => 'choice',
                    189: 	 choices     => ['left','right','center']
1.82      matthew   190: 	 },
                    191:      texwidth     => {
                    192:          default     => '93',
                    193:          test        => $int_test,
                    194:          description => 'Width of plot when printed (mm)',
                    195:          edit_type   => 'entry',
                    196:          size        => '5'
                    197:          },
1.92      matthew   198:      texfont     => {
                    199:          default     => '22',
                    200:          test        => $int_test,
                    201:          description => 'Font size to use in TeX output (pts):',
                    202:          edit_type   => 'choice',
1.96    ! matthew   203:          choices     => [qw/8 10 12 14 16 18 20 22 24 26 28 30 32 34 36/],
1.92      matthew   204:          },
1.87      matthew   205:      plottype  => {
                    206: 	 default     => 'Cartesian',
                    207: 	 test        => sub {$_[0]=~/^(Polar|Cartesian)$/},
                    208: 	 description => 'Plot type:',
                    209: 	 edit_type   => 'choice',
1.94      matthew   210:          choices     => ['Cartesian','Polar']
1.87      matthew   211:          },
1.1       matthew   212:      );
                    213: 
                    214: my %key_defaults = 
                    215:     (
1.20      matthew   216:      title => { 
                    217: 	 default => '',
                    218: 	 test => $words_test,
                    219: 	 description => 'Title of key',
1.38      matthew   220: 	 edit_type   => 'entry',
                    221: 	 size        => '40'
1.20      matthew   222: 	 },
                    223:      box   => { 
                    224: 	 default => 'off',
                    225: 	 test => $onoff_test,
                    226: 	 description => 'Draw a box around the key?',
1.45      matthew   227: 	 edit_type   => 'onoff'
1.20      matthew   228: 	 },
                    229:      pos   => { 
                    230: 	 default => 'top right', 
                    231: 	 test => $key_pos_test, 
                    232: 	 description => 'position of the key on the plot',
                    233: 	 edit_type   => 'choice',
                    234: 	 choices     => ['top left','top right','bottom left','bottom right',
                    235: 			 'outside','below']
                    236: 	 }
1.1       matthew   237:      );
                    238: 
                    239: my %label_defaults = 
                    240:     (
1.20      matthew   241:      xpos    => {
                    242: 	 default => 0,
                    243: 	 test => $real_test,
                    244: 	 description => 'x position of label (graph coordinates)',
1.38      matthew   245: 	 edit_type   => 'entry',
                    246: 	 size        => '10'
1.20      matthew   247: 	 },
                    248:      ypos    => {
                    249: 	 default => 0, 
                    250: 	 test => $real_test,
                    251: 	 description => 'y position of label (graph coordinates)',
1.38      matthew   252: 	 edit_type   => 'entry',
                    253: 	 size        => '10'
1.20      matthew   254: 	 },
                    255:      justify => {
                    256: 	 default => 'left',    
                    257: 	 test => sub {$_[0]=~/^(left|right|center)$/},
                    258: 	 description => 'justification of the label text on the plot',
                    259: 	 edit_type   => 'choice',
                    260: 	 choices     => ['left','right','center']
                    261:      }
1.1       matthew   262:      );
                    263: 
1.89      matthew   264: my @tic_edit_order = ('location','mirror','start','increment','end',
                    265:                       'minorfreq');
1.45      matthew   266: my %tic_defaults =
                    267:     (
                    268:      location => {
                    269: 	 default => 'border', 
                    270: 	 test => sub {$_[0]=~/^(border|axis)$/},
1.90      matthew   271: 	 description => 'Location of major tic marks',
1.45      matthew   272: 	 edit_type   => 'choice',
                    273: 	 choices     => ['border','axis']
                    274: 	 },
                    275:      mirror => {
                    276: 	 default => 'on', 
                    277: 	 test => $onoff_test,
1.90      matthew   278: 	 description => 'mirror tics on opposite axis?',
1.45      matthew   279: 	 edit_type   => 'onoff'
                    280: 	 },
                    281:      start => {
                    282: 	 default => '-10.0',
                    283: 	 test => $real_test,
1.90      matthew   284: 	 description => 'Start major tics at',
1.45      matthew   285: 	 edit_type   => 'entry',
                    286: 	 size        => '10'
                    287: 	 },
                    288:      increment => {
                    289: 	 default => '1.0',
                    290: 	 test => $real_test,
1.90      matthew   291: 	 description => 'Place a major tic every',
1.45      matthew   292: 	 edit_type   => 'entry',
                    293: 	 size        => '10'
                    294: 	 },
                    295:      end => {
                    296: 	 default => ' 10.0',
                    297: 	 test => $real_test,
1.90      matthew   298: 	 description => 'Stop major tics at ',
1.45      matthew   299: 	 edit_type   => 'entry',
                    300: 	 size        => '10'
                    301: 	 },
1.89      matthew   302:      minorfreq => {
                    303: 	 default => '0',
                    304: 	 test => $int_test,
1.90      matthew   305: 	 description => 'Number of minor tics between major tic marks',
1.89      matthew   306: 	 edit_type   => 'entry',
                    307: 	 size        => '10'
                    308: 	 },         
1.45      matthew   309:      );
                    310: 
1.65      matthew   311: my @axis_edit_order = ('color','xmin','xmax','ymin','ymax');
1.1       matthew   312: my %axis_defaults = 
                    313:     (
1.28      matthew   314:      color   => {
1.20      matthew   315: 	 default => 'x000000', 
                    316: 	 test => $color_test,
1.78      matthew   317: 	 description => 'color of grid lines (x000000)',
1.38      matthew   318: 	 edit_type   => 'entry',
                    319: 	 size        => '10'
1.20      matthew   320: 	 },
                    321:      xmin      => {
                    322: 	 default => '-10.0',
                    323: 	 test => $real_test,
                    324: 	 description => 'minimum x-value shown in plot',
1.38      matthew   325: 	 edit_type   => 'entry',
                    326: 	 size        => '10'
1.20      matthew   327: 	 },
                    328:      xmax      => {
                    329: 	 default => ' 10.0',
                    330: 	 test => $real_test,
                    331: 	 description => 'maximum x-value shown in plot',	 
1.38      matthew   332: 	 edit_type   => 'entry',
                    333: 	 size        => '10'
1.20      matthew   334: 	 },
                    335:      ymin      => {
                    336: 	 default => '-10.0',
                    337: 	 test => $real_test,
                    338: 	 description => 'minimum y-value shown in plot',	 
1.38      matthew   339: 	 edit_type   => 'entry',
                    340: 	 size        => '10'
1.20      matthew   341: 	 },
                    342:      ymax      => {
                    343: 	 default => ' 10.0',
                    344: 	 test => $real_test,
                    345: 	 description => 'maximum y-value shown in plot',	 
1.38      matthew   346: 	 edit_type   => 'entry',
                    347: 	 size        => '10'
1.20      matthew   348: 	 }
1.1       matthew   349:      );
                    350: 
1.62      matthew   351: my @curve_edit_order = ('color','name','linestyle','pointtype','pointsize');
1.60      matthew   352: 
1.1       matthew   353: my %curve_defaults = 
                    354:     (
1.20      matthew   355:      color     => {
                    356: 	 default => 'x000000',
                    357: 	 test => $color_test,
                    358: 	 description => 'color of curve (x000000)',
1.38      matthew   359: 	 edit_type   => 'entry',
                    360: 	 size        => '10'
1.20      matthew   361: 	 },
                    362:      name      => {
                    363: 	 default => '',
                    364: 	 test => $words_test,
                    365: 	 description => 'name of curve to appear in key',
1.38      matthew   366: 	 edit_type   => 'entry',
                    367: 	 size        => '20'
1.20      matthew   368: 	 },
                    369:      linestyle => {
                    370: 	 default => 'lines',
                    371: 	 test => $linestyle_test,
1.35      matthew   372: 	 description => 'Line style',
1.20      matthew   373: 	 edit_type   => 'choice',
1.38      matthew   374: 	 choices     => [keys(%linestyles)]
1.60      matthew   375: 	 },
                    376: # gnuplots term=gif driver does not handle linewidth :(
                    377: #     linewidth => {
                    378: #         default     => 1,
                    379: #         test        => $int_test,
                    380: #         description => 'Line width (may not apply to all line styles)',
                    381: #         edit_type   => 'choice',
                    382: #         choices     => [1,2,3,4,5,6,7,8,9,10]
                    383: #         },
                    384:      pointsize => {
                    385:          default     => 1,
1.62      matthew   386:          test        => $pos_real_test,
                    387:          description => 'point size (may not apply to all line styles)',
                    388:          edit_type   => 'entry',
                    389:          size        => '5'
                    390:          },
                    391:      pointtype => {
                    392:          default     => 1,
1.60      matthew   393:          test        => $int_test,
1.62      matthew   394:          description => 'point type (may not apply to all line styles)',
1.60      matthew   395:          edit_type   => 'choice',
1.62      matthew   396:          choices     => [0,1,2,3,4,5,6]
1.60      matthew   397:          }
1.1       matthew   398:      );
                    399: 
1.21      matthew   400: ###################################################################
                    401: ##                                                               ##
                    402: ##                    parsing and edit rendering                 ##
                    403: ##                                                               ##
                    404: ###################################################################
1.45      matthew   405: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
1.1       matthew   406: 
1.47      matthew   407: sub start_gnuplot {
1.89      matthew   408:     undef(%plot);   undef(%key);    undef(%axis);
                    409:     undef($title);  undef($xlabel); undef($ylabel);
                    410:     undef(@labels); undef(@curves);
                    411:     undef(%xtics);  undef(%ytics);
1.6       matthew   412:     #
1.1       matthew   413:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    414:     my $result='';
1.25      matthew   415:     &Apache::lonxml::register('Apache::lonplot',
1.45      matthew   416: 	     ('title','xlabel','ylabel','key','axis','label','curve',
                    417: 	      'xtics','ytics'));
1.29      matthew   418:     push (@Apache::lonxml::namespace,'lonplot');
1.51      matthew   419:     if ($target eq 'web' || $target eq 'tex') {
1.47      matthew   420: 	&get_attributes(\%plot,\%gnuplot_defaults,$parstack,$safeeval,
1.17      matthew   421: 			$tagstack->[-1]);
1.20      matthew   422:     } elsif ($target eq 'edit') {
1.47      matthew   423: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
                    424: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
                    425: 				    \@gnuplot_edit_order);
1.20      matthew   426:     } elsif ($target eq 'modified') {
                    427: 	my $constructtag=&Apache::edit::get_new_args
1.47      matthew   428: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
1.20      matthew   429: 	if ($constructtag) {
                    430: 	    $result = &Apache::edit::rebuild_tag($token);
                    431: 	}
1.4       matthew   432:     }
1.21      matthew   433:     return $result;
1.1       matthew   434: }
                    435: 
1.47      matthew   436: sub end_gnuplot {
1.1       matthew   437:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    438:     pop @Apache::lonxml::namespace;
1.4       matthew   439:     &Apache::lonxml::deregister('Apache::lonplot',
                    440: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    441:     my $result = '';
1.56      albertel  442:     my $randnumber;
                    443:     # need to call rand everytime start_script would evaluate, as the
                    444:     # safe space rand number generator and the global rand generator 
1.95      www       445:     # are not separate
1.56      albertel  446:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    447: 	$target eq 'answer') {
                    448:       $randnumber=int(rand(1000));
                    449:     }
1.51      matthew   450:     if ($target eq 'web' || $target eq 'tex') {
1.21      matthew   451: 	&check_inputs(); # Make sure we have all the data we need
1.13      matthew   452: 	##
                    453: 	## Determine filename
1.4       matthew   454: 	my $tmpdir = '/home/httpd/perl/tmp/';
1.12      matthew   455: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.69      matthew   456: 	    '_'.time.'_'.$$.$randnumber.'_plot';
1.4       matthew   457: 	## Write the plot description to the file
1.51      matthew   458: 	&write_gnuplot_file($tmpdir,$filename,$target);
1.54      matthew   459: 	$filename = &Apache::lonnet::escape($filename);
1.4       matthew   460: 	## return image tag for the plot
1.51      matthew   461: 	if ($target eq 'web') {
                    462: 	    $result .= <<"ENDIMAGE";
1.69      matthew   463: <img src    = "/cgi-bin/plot.gif?file=$filename.data&output=gif" 
1.64      matthew   464:      width  = "$plot{'width'}"
1.16      matthew   465:      height = "$plot{'height'}"
                    466:      align  = "$plot{'align'}"
1.64      matthew   467:      alt    = "$plot{'alttag'}" />
1.12      matthew   468: ENDIMAGE
1.51      matthew   469:         } elsif ($target eq 'tex') {
1.91      albertel  470: 	    #might be inside the safe space, register the URL for later
                    471: 	    &Apache::lonxml::register_ssi("/cgi-bin/plot.gif?file=$filename.data&output=eps");
1.84      sakharuk  472: 	    $result = '\graphicspath{{/home/httpd/perl/tmp/}}\includegraphics[width='.$plot{'texwidth'}.' mm]{'.&Apache::lonnet::unescape($filename).'.eps}';
1.51      matthew   473: 	}
1.20      matthew   474:     } elsif ($target eq 'edit') {
1.21      matthew   475: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   476:     }
1.1       matthew   477:     return $result;
                    478: }
1.2       matthew   479: 
1.45      matthew   480: 
                    481: ##--------------------------------------------------------------- xtics
                    482: sub start_xtics {
                    483:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    484:     my $result='';
1.51      matthew   485:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   486: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
                    487: 		    $tagstack->[-1]);
                    488:     } elsif ($target eq 'edit') {
                    489: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
                    490: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    491: 				    \@tic_edit_order);
                    492:     } elsif ($target eq 'modified') {
                    493: 	my $constructtag=&Apache::edit::get_new_args
                    494: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    495: 	if ($constructtag) {
                    496: 	    $result = &Apache::edit::rebuild_tag($token);
                    497: 	}
                    498:     }
                    499:     return $result;
                    500: }
                    501: 
                    502: sub end_xtics {
                    503:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    504:     my $result = '';
1.51      matthew   505:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   506:     } elsif ($target eq 'edit') {
                    507: 	$result.=&Apache::edit::tag_end($target,$token);
                    508:     }
                    509:     return $result;
                    510: }
                    511: 
                    512: ##--------------------------------------------------------------- ytics
                    513: sub start_ytics {
                    514:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    515:     my $result='';
1.51      matthew   516:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   517: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
                    518: 		    $tagstack->[-1]);
                    519:     } elsif ($target eq 'edit') {
                    520: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
                    521: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    522: 				    \@tic_edit_order);
                    523:     } elsif ($target eq 'modified') {
                    524: 	my $constructtag=&Apache::edit::get_new_args
                    525: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    526: 	if ($constructtag) {
                    527: 	    $result = &Apache::edit::rebuild_tag($token);
                    528: 	}
                    529:     }
                    530:     return $result;
                    531: }
                    532: 
                    533: sub end_ytics {
                    534:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    535:     my $result = '';
1.51      matthew   536:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   537:     } elsif ($target eq 'edit') {
                    538: 	$result.=&Apache::edit::tag_end($target,$token);
                    539:     }
                    540:     return $result;
                    541: }
                    542: 
                    543: 
1.1       matthew   544: ##----------------------------------------------------------------- key
                    545: sub start_key {
                    546:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    547:     my $result='';
1.51      matthew   548:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   549: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   550: 		    $tagstack->[-1]);
1.20      matthew   551:     } elsif ($target eq 'edit') {
1.25      matthew   552: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21      matthew   553: 	$result .= &edit_attributes($target,$token,\%key_defaults);
1.20      matthew   554:     } elsif ($target eq 'modified') {
                    555: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   556: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
1.20      matthew   557: 	if ($constructtag) {
                    558: 	    $result = &Apache::edit::rebuild_tag($token);
                    559: 	}
1.4       matthew   560:     }
1.1       matthew   561:     return $result;
                    562: }
                    563: 
                    564: sub end_key {
                    565:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    566:     my $result = '';
1.51      matthew   567:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   568:     } elsif ($target eq 'edit') {
1.21      matthew   569: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   570:     }
1.1       matthew   571:     return $result;
                    572: }
1.21      matthew   573: 
1.1       matthew   574: ##------------------------------------------------------------------- title
                    575: sub start_title {
                    576:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    577:     my $result='';
1.51      matthew   578:     if ($target eq 'web' || $target eq 'tex') {
1.81      albertel  579: 	$title = &Apache::lonxml::get_all_text("/title",$parser);
1.58      matthew   580: 	$title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
1.49      matthew   581: 	$title =~ s/\n/ /g;
1.32      matthew   582: 	if (length($title) > $max_str_len) {
                    583: 	    $title = substr($title,0,$max_str_len);
                    584: 	}
1.20      matthew   585:     } elsif ($target eq 'edit') {
1.25      matthew   586: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.81      albertel  587: 	my $text=&Apache::lonxml::get_all_text("/title",$parser);
1.39      matthew   588: 	$result.=&Apache::edit::end_row().
                    589: 	    &Apache::edit::start_spanning_row().
1.63      albertel  590: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   591:     } elsif ($target eq 'modified') {
1.42      matthew   592: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  593: 	$result.=&Apache::edit::modifiedfield("/title",$parser);
1.4       matthew   594:     }
1.1       matthew   595:     return $result;
                    596: }
                    597: 
                    598: sub end_title {
                    599:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    600:     my $result = '';
1.51      matthew   601:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   602:     } elsif ($target eq 'edit') {
1.27      matthew   603: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   604:     }
1.1       matthew   605:     return $result;
                    606: }
                    607: ##------------------------------------------------------------------- xlabel
                    608: sub start_xlabel {
                    609:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    610:     my $result='';
1.51      matthew   611:     if ($target eq 'web' || $target eq 'tex') {
1.81      albertel  612: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$parser);
1.58      matthew   613: 	$xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
1.49      matthew   614: 	$xlabel =~ s/\n/ /g;
1.32      matthew   615: 	if (length($xlabel) > $max_str_len) {
                    616: 	    $xlabel = substr($xlabel,0,$max_str_len);
                    617: 	}
1.20      matthew   618:     } elsif ($target eq 'edit') {
1.25      matthew   619: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.81      albertel  620: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$parser);
1.39      matthew   621: 	$result.=&Apache::edit::end_row().
                    622: 	    &Apache::edit::start_spanning_row().
1.63      albertel  623: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   624:     } elsif ($target eq 'modified') {
1.42      matthew   625: 	$result.=&Apache::edit::rebuild_tag($token);	
1.93      albertel  626: 	$result.=&Apache::edit::modifiedfield("/xlabel",$parser);
1.4       matthew   627:     }
1.1       matthew   628:     return $result;
                    629: }
                    630: 
                    631: sub end_xlabel {
                    632:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    633:     my $result = '';
1.51      matthew   634:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   635:     } elsif ($target eq 'edit') {
1.27      matthew   636: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   637:     }
1.1       matthew   638:     return $result;
                    639: }
1.21      matthew   640: 
1.1       matthew   641: ##------------------------------------------------------------------- ylabel
                    642: sub start_ylabel {
                    643:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    644:     my $result='';
1.51      matthew   645:     if ($target eq 'web' || $target eq 'tex') {
1.81      albertel  646: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$parser);
1.58      matthew   647: 	$ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
1.49      matthew   648: 	$ylabel =~ s/\n/ /g;
1.32      matthew   649: 	if (length($ylabel) > $max_str_len) {
                    650: 	    $ylabel = substr($ylabel,0,$max_str_len);
                    651: 	}
1.20      matthew   652:     } elsif ($target eq 'edit') {
1.25      matthew   653: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.81      albertel  654: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$parser);
1.39      matthew   655: 	$result .= &Apache::edit::end_row().
                    656: 	    &Apache::edit::start_spanning_row().
1.63      albertel  657: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   658:     } elsif ($target eq 'modified') {
1.42      matthew   659: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  660: 	$result.=&Apache::edit::modifiedfield("/ylabel",$parser);
1.4       matthew   661:     }
1.1       matthew   662:     return $result;
                    663: }
                    664: 
                    665: sub end_ylabel {
                    666:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    667:     my $result = '';
1.51      matthew   668:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   669:     } elsif ($target eq 'edit') {
1.27      matthew   670: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   671:     }
1.1       matthew   672:     return $result;
                    673: }
1.21      matthew   674: 
1.1       matthew   675: ##------------------------------------------------------------------- label
                    676: sub start_label {
                    677:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    678:     my $result='';
1.51      matthew   679:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   680: 	my %label;
                    681: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew   682: 		    $tagstack->[-1]);
1.81      albertel  683: 	my $text = &Apache::lonxml::get_all_text("/label",$parser);
1.58      matthew   684: 	$text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
1.49      matthew   685: 	$text =~ s/\n/ /g;
1.32      matthew   686: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
                    687: 	$label{'text'} = $text;
1.17      matthew   688: 	push(@labels,\%label);
1.20      matthew   689:     } elsif ($target eq 'edit') {
1.25      matthew   690: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21      matthew   691: 	$result .= &edit_attributes($target,$token,\%label_defaults);
1.81      albertel  692: 	my $text = &Apache::lonxml::get_all_text("/label",$parser);
1.39      matthew   693: 	$result .= &Apache::edit::end_row().
                    694: 	    &Apache::edit::start_spanning_row().
1.63      albertel  695: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   696:     } elsif ($target eq 'modified') {
1.42      matthew   697: 	&Apache::edit::get_new_args
1.24      matthew   698: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
1.42      matthew   699: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  700: 	$result.=&Apache::edit::modifiedfield("/label",$parser);
1.4       matthew   701:     }
1.1       matthew   702:     return $result;
                    703: }
                    704: 
                    705: sub end_label {
                    706:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    707:     my $result = '';
1.51      matthew   708:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   709:     } elsif ($target eq 'edit') {
1.21      matthew   710: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   711:     }
1.1       matthew   712:     return $result;
                    713: }
                    714: 
                    715: ##------------------------------------------------------------------- curve
                    716: sub start_curve {
                    717:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    718:     my $result='';
1.25      matthew   719:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
                    720:     push (@Apache::lonxml::namespace,'curve');
1.51      matthew   721:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   722: 	my %curve;
                    723: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew   724: 		    $tagstack->[-1]);
1.17      matthew   725: 	push (@curves,\%curve);
1.20      matthew   726:     } elsif ($target eq 'edit') {
1.26      matthew   727: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
1.60      matthew   728: 	$result .= &edit_attributes($target,$token,\%curve_defaults,
                    729:                                     \@curve_edit_order);
1.20      matthew   730:     } elsif ($target eq 'modified') {
                    731: 	my $constructtag=&Apache::edit::get_new_args
1.35      matthew   732: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
1.20      matthew   733: 	if ($constructtag) {
                    734: 	    $result = &Apache::edit::rebuild_tag($token);
                    735: 	    $result.= &Apache::edit::handle_insert();
                    736: 	}
1.4       matthew   737:     }
1.1       matthew   738:     return $result;
                    739: }
                    740: 
                    741: sub end_curve {
                    742:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    743:     my $result = '';
1.25      matthew   744:     pop @Apache::lonxml::namespace;
                    745:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.51      matthew   746:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   747:     } elsif ($target eq 'edit') {
1.21      matthew   748: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   749:     }
1.1       matthew   750:     return $result;
                    751: }
1.21      matthew   752: 
1.1       matthew   753: ##------------------------------------------------------------ curve function
                    754: sub start_function {
                    755:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    756:     my $result='';
1.51      matthew   757:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   758: 	if (exists($curves[-1]->{'data'})) {
1.85      matthew   759: 	    &Apache::lonxml::warning
                    760:                 ('Use of the <b>curve function</b> tag precludes use of '.
                    761:                  ' the <b>curve data</b> tag.  '.
                    762:                  'The curve data tag will be omitted in favor of the '.
                    763:                  'curve function declaration.');
1.17      matthew   764: 	    delete $curves[-1]->{'data'} ;
                    765: 	}
1.81      albertel  766:         my $function = &Apache::lonxml::get_all_text("/function",$parser);
1.58      matthew   767: 	$function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
                    768: 	$curves[-1]->{'function'} = $function; 
1.20      matthew   769:     } elsif ($target eq 'edit') {
1.37      matthew   770: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1.81      albertel  771: 	my $text = &Apache::lonxml::get_all_text("/function",$parser);
1.39      matthew   772: 	$result .= &Apache::edit::end_row().
                    773: 	    &Apache::edit::start_spanning_row().
1.63      albertel  774: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   775:     } elsif ($target eq 'modified') {
1.42      matthew   776: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  777: 	$result.=&Apache::edit::modifiedfield("/function",$parser);
1.4       matthew   778:     }
1.1       matthew   779:     return $result;
                    780: }
                    781: 
                    782: sub end_function {
                    783:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    784:     my $result = '';
1.51      matthew   785:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   786:     } elsif ($target eq 'edit') {
1.26      matthew   787: 	$result .= &Apache::edit::end_table();
1.4       matthew   788:     }
1.1       matthew   789:     return $result;
                    790: }
1.21      matthew   791: 
1.1       matthew   792: ##------------------------------------------------------------ curve  data
                    793: sub start_data {
                    794:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    795:     my $result='';
1.51      matthew   796:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   797: 	if (exists($curves[-1]->{'function'})) {
1.85      matthew   798: 	    &Apache::lonxml::warning
                    799:                 ('Use of the <b>curve function</b> tag precludes use of '.
                    800:                  ' the <b>curve data</b> tag.  '.
                    801:                  'The curve function tag will be omitted in favor of the '.
                    802:                  'curve data declaration.');
1.17      matthew   803: 	    delete($curves[-1]->{'function'});
                    804: 	}
1.81      albertel  805: 	my $datatext = &Apache::lonxml::get_all_text("/data",$parser);
1.58      matthew   806: 	$datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
1.40      matthew   807: 	# Deal with cases where we're given an array...
                    808: 	if ($datatext =~ /^\@/) {
                    809: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
                    810: 					  $safeeval,1);
                    811: 	}
1.49      matthew   812: 	$datatext =~ s/\s+/ /g;
1.17      matthew   813: 	# Need to do some error checking on the @data array - 
                    814: 	# make sure it's all numbers and make sure each array 
                    815: 	# is of the same length.
                    816: 	my @data;
1.35      matthew   817: 	if ($datatext =~ /,/) { # comma deliminated
1.17      matthew   818: 	    @data = split /,/,$datatext;
1.95      www       819: 	} else { # Assume it's space separated.
1.17      matthew   820: 	    @data = split / /,$datatext;
                    821: 	}
                    822: 	for (my $i=0;$i<=$#data;$i++) {
                    823: 	    # Check that it's non-empty
1.19      matthew   824: 	    if (! defined($data[$i])) {
                    825: 		&Apache::lonxml::warning(
1.85      matthew   826: 		    'undefined curve data value.  Replacing with '.
1.19      matthew   827: 		    ' pi/e = 1.15572734979092');
                    828: 		$data[$i] = 1.15572734979092;
                    829: 	    }
1.17      matthew   830: 	    # Check that it's a number
1.19      matthew   831: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
                    832: 		&Apache::lonxml::warning(
1.85      matthew   833: 		    'Bad curve data value of '.$data[$i].'  Replacing with '.
1.19      matthew   834: 		    ' pi/e = 1.15572734979092');
                    835: 		$data[$i] = 1.15572734979092;
                    836: 	    }
1.17      matthew   837: 	}
1.35      matthew   838: 	# complain if the number of data points is not the same as
                    839: 	# in previous sets of data.
1.36      matthew   840: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
1.35      matthew   841: 	    &Apache::lonxml::warning
                    842: 		('Number of data points is not consistent with previous '.
                    843: 		 'number of data points');
                    844: 	}
1.17      matthew   845: 	push  @{$curves[-1]->{'data'}},\@data;
1.20      matthew   846:     } elsif ($target eq 'edit') {
1.37      matthew   847: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1.81      albertel  848: 	my $text = &Apache::lonxml::get_all_text("/data",$parser);
1.39      matthew   849: 	$result .= &Apache::edit::end_row().
                    850: 	    &Apache::edit::start_spanning_row().
1.63      albertel  851: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   852:     } elsif ($target eq 'modified') {
1.42      matthew   853: 	$result.=&Apache::edit::rebuild_tag($token);
1.93      albertel  854: 	$result.=&Apache::edit::modifiedfield("/data",$parser);
1.4       matthew   855:     }
1.1       matthew   856:     return $result;
                    857: }
                    858: 
                    859: sub end_data {
                    860:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    861:     my $result = '';
1.51      matthew   862:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   863:     } elsif ($target eq 'edit') {
1.26      matthew   864: 	$result .= &Apache::edit::end_table();
1.4       matthew   865:     }
1.1       matthew   866:     return $result;
                    867: }
                    868: 
                    869: ##------------------------------------------------------------------- axis
                    870: sub start_axis {
                    871:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    872:     my $result='';
1.51      matthew   873:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   874: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                    875: 			$tagstack->[-1]);
1.20      matthew   876:     } elsif ($target eq 'edit') {
1.25      matthew   877: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.65      matthew   878: 	$result .= &edit_attributes($target,$token,\%axis_defaults,
                    879: 				    \@axis_edit_order);
1.20      matthew   880:     } elsif ($target eq 'modified') {
1.29      matthew   881: 	my $constructtag=&Apache::edit::get_new_args
                    882: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
                    883: 	if ($constructtag) {
                    884: 	    $result = &Apache::edit::rebuild_tag($token);
                    885: 	}
1.4       matthew   886:     }
1.1       matthew   887:     return $result;
                    888: }
                    889: 
                    890: sub end_axis {
                    891:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    892:     my $result = '';
1.51      matthew   893:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   894:     } elsif ($target eq 'edit') {
1.21      matthew   895: 	$result.=&Apache::edit::tag_end($target,$token);
1.20      matthew   896:     } elsif ($target eq 'modified') {
1.4       matthew   897:     }
1.1       matthew   898:     return $result;
                    899: }
                    900: 
1.21      matthew   901: ###################################################################
                    902: ##                                                               ##
                    903: ##        Utility Functions                                      ##
                    904: ##                                                               ##
                    905: ###################################################################
                    906: 
1.13      matthew   907: ##----------------------------------------------------------- set_defaults
                    908: sub set_defaults {
1.21      matthew   909:     my ($var,$defaults) = @_;
1.13      matthew   910:     my $key;
1.24      matthew   911:     foreach $key (keys(%$defaults)) {
1.13      matthew   912: 	$var->{$key} = $defaults->{$key}->{'default'};
                    913:     }
                    914: }
                    915: 
1.1       matthew   916: ##------------------------------------------------------------------- misc
1.2       matthew   917: sub get_attributes{
1.21      matthew   918:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24      matthew   919:     foreach my $attr (keys(%{$defaults})) {
1.92      matthew   920: 	if ($attr eq 'texwidth' || $attr eq 'texfont') {
1.86      albertel  921: 	    $values->{$attr} = 
                    922: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval,undef,1);
                    923: 	} else {
                    924: 	    $values->{$attr} = 
                    925: 		&Apache::lonxml::get_param($attr,$parstack,$safeeval);
                    926: 	}
1.10      matthew   927: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew   928: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew   929: 	    next;
                    930: 	}
1.10      matthew   931: 	my $test = $defaults->{$attr}->{'test'};
                    932: 	if (! &$test($values->{$attr})) {
1.6       matthew   933: 	    &Apache::lonxml::warning
                    934: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew   935: 		 .$defaults->{$attr}->{'default'} );
                    936: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew   937: 	}
1.2       matthew   938:     }
1.11      matthew   939:     return ;
1.6       matthew   940: }
1.40      matthew   941: 
1.15      matthew   942: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew   943: sub write_gnuplot_file {
1.51      matthew   944:     my ($tmpdir,$filename,$target)= @_;
1.6       matthew   945:     my $gnuplot_input = '';
1.10      matthew   946:     my $curve;
1.92      matthew   947:     my $pt = $plot{'texfont'};
1.6       matthew   948:     # Collect all the colors
                    949:     my @Colors;
                    950:     push @Colors, $plot{'bgcolor'};
                    951:     push @Colors, $plot{'fgcolor'}; 
1.13      matthew   952:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9       matthew   953:     foreach $curve (@curves) {
                    954: 	push @Colors, ($curve->{'color'} ne '' ? 
                    955: 		       $curve->{'color'}       : 
1.13      matthew   956: 		       $plot{'fgcolor'}        );
1.6       matthew   957:     }
                    958:     # set term
1.51      matthew   959:     if ($target eq 'web') {
                    960: 	$gnuplot_input .= 'set term gif ';
                    961: 	$gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
                    962: 	$gnuplot_input .= $plot{'font'} . ' ';
                    963: 	$gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
                    964: 	$gnuplot_input .= "@Colors\n";
                    965: 	# set output
                    966: 	$gnuplot_input .= "set output\n";
                    967:     } elsif ($target eq 'tex') {
1.92      matthew   968: 	$gnuplot_input .= "set term postscript eps monochrome solid \"Helvetica\" $pt \n";
1.68      sakharuk  969: 	$gnuplot_input .= "set output \"/home/httpd/perl/tmp/".
                    970: 	    &Apache::lonnet::unescape($filename).".eps\"\n";
1.87      matthew   971:     }
                    972:     # cartesian or polar?
                    973:     if (lc($plot{'plottype'}) eq 'polar') {
                    974:         $gnuplot_input .= 'set polar'.$/;
                    975:     } else {
                    976:         # Assume Cartesian
1.51      matthew   977:     }
1.7       matthew   978:     # grid
1.10      matthew   979:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7       matthew   980:     # border
1.9       matthew   981:     $gnuplot_input .= ($plot{'border'} eq 'on'?
                    982: 		       'set border'.$/           :
1.67      matthew   983: 		       'set noborder'.$/         );
1.77      matthew   984:     # sampling rate for non-data curves
                    985:     $gnuplot_input .= "set samples $plot{'samples'}\n";
1.67      matthew   986:     # title, xlabel, ylabel
1.45      matthew   987:     # titles
1.89      matthew   988:     if ($target eq 'tex') {
1.92      matthew   989:         $gnuplot_input .= "set title  \"$title\" font \"Helvetica,".$pt."pt\"\n"  if (defined($title)) ;
                    990:         $gnuplot_input .= "set xlabel \"$xlabel\" font \"Helvetica,".$pt."pt\" \n" if (defined($xlabel));
                    991:         $gnuplot_input .= "set ylabel \"$ylabel\" font \"Helvetica,".$pt."pt\"\n" if (defined($ylabel));
1.89      matthew   992:     } else {
                    993:         $gnuplot_input .= "set title  \"$title\"  \n"  if (defined($title)) ;
                    994:         $gnuplot_input .= "set xlabel \"$xlabel\" \n" if (defined($xlabel));
                    995:         $gnuplot_input .= "set ylabel \"$ylabel\" \n" if (defined($ylabel));
                    996:     }
1.45      matthew   997:     # tics
                    998:     if (%xtics) {    
                    999: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
1.46      matthew  1000: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1001: 	$gnuplot_input .= "$xtics{'start'}, ";
                   1002: 	$gnuplot_input .= "$xtics{'increment'}, ";
                   1003: 	$gnuplot_input .= "$xtics{'end'}\n";
1.89      matthew  1004:         if ($xtics{'minorfreq'} != 0) {
                   1005:             $gnuplot_input .= "set mxtics ".$xtics{'minorfreq'}."\n";
                   1006:         } 
1.45      matthew  1007:     }
                   1008:     if (%ytics) {    
                   1009: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
1.46      matthew  1010: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1011: 	$gnuplot_input .= "$ytics{'start'}, ";
                   1012: 	$gnuplot_input .= "$ytics{'increment'}, ";
                   1013:         $gnuplot_input .= "$ytics{'end'}\n";
1.89      matthew  1014:         if ($ytics{'minorfreq'} != 0) {
                   1015:             $gnuplot_input .= "set mytics ".$ytics{'minorfreq'}."\n";
                   1016:         } 
1.45      matthew  1017:     }
                   1018:     # axis
1.23      matthew  1019:     if (%axis) {
1.13      matthew  1020: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                   1021: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6       matthew  1022:     }
                   1023:     # Key
1.23      matthew  1024:     if (%key) {
1.9       matthew  1025: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                   1026: 	if ($key{'title'} ne '') {
1.67      matthew  1027: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
1.11      matthew  1028: 	} 
                   1029: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew  1030:     } else {
1.9       matthew  1031: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew  1032:     }
1.6       matthew  1033:     # labels
1.10      matthew  1034:     my $label;
1.6       matthew  1035:     foreach $label (@labels) {
                   1036: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.92      matthew  1037: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.' font "Helvetica,'.$pt.'pt"'.$/ ;
1.6       matthew  1038:     }
1.74      matthew  1039:     if ($target eq 'tex') {
1.76      matthew  1040:         $gnuplot_input .="set size 1,".$plot{'height'}/$plot{'width'}*1.38;
1.74      matthew  1041:         $gnuplot_input .="\n";
                   1042:         }
1.6       matthew  1043:     # curves
                   1044:     $gnuplot_input .= 'plot ';
1.9       matthew  1045:     for (my $i = 0;$i<=$#curves;$i++) {
                   1046: 	$curve = $curves[$i];
                   1047: 	$gnuplot_input.= ', ' if ($i > 0);
1.6       matthew  1048: 	if (exists($curve->{'function'})) {
1.9       matthew  1049: 	    $gnuplot_input.= 
                   1050: 		$curve->{'function'}.' title "'.
                   1051: 		$curve->{'name'}.'" with '.
1.72      matthew  1052:                 $curve->{'linestyle'};
1.73      sakharuk 1053:             $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
1.60      matthew  1054:             if (($curve->{'linestyle'} eq 'points')      ||
                   1055:                 ($curve->{'linestyle'} eq 'linespoints') ||
                   1056:                 ($curve->{'linestyle'} eq 'errorbars')   ||
                   1057:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
                   1058:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
                   1059:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
1.62      matthew  1060:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
1.60      matthew  1061:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
                   1062:             }
1.6       matthew  1063: 	} elsif (exists($curve->{'data'})) {
1.40      matthew  1064: 	    # Store data values in $datatext
                   1065: 	    my $datatext = '';
                   1066: 	    #   get new filename
1.70      matthew  1067: 	    my $datafilename = "$tmpdir/$filename.data.$i";
1.40      matthew  1068: 	    my $fh=Apache::File->new(">$datafilename");
                   1069: 	    # Compile data
1.6       matthew  1070: 	    my @Data = @{$curve->{'data'}};
1.9       matthew  1071: 	    my @Data0 = @{$Data[0]};
                   1072: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew  1073: 		my $dataset;
1.6       matthew  1074: 		foreach $dataset (@Data) {
1.9       matthew  1075: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew  1076: 		}
1.9       matthew  1077: 		$datatext .= $/;
1.6       matthew  1078: 	    }
1.40      matthew  1079: 	    #   write file
                   1080: 	    print $fh $datatext;
                   1081: 	    close ($fh);
                   1082: 	    #   generate gnuplot text
                   1083: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
                   1084: 		$curve->{'name'}.'" with '.
                   1085: 		$curve->{'linestyle'};
1.73      sakharuk 1086:             $gnuplot_input.= ' linewidth 4 ' if ($target eq 'tex');
1.62      matthew  1087:             if (($curve->{'linestyle'} eq 'points')      ||
                   1088:                 ($curve->{'linestyle'} eq 'linespoints') ||
                   1089:                 ($curve->{'linestyle'} eq 'errorbars')   ||
                   1090:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
                   1091:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
                   1092:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
                   1093:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
                   1094:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
                   1095:             }
1.6       matthew  1096: 	}
                   1097:     }
1.40      matthew  1098:     # Write the output to a file.
1.70      matthew  1099:     my $fh=Apache::File->new(">$tmpdir$filename.data");
1.40      matthew  1100:     print $fh $gnuplot_input;
                   1101:     close($fh);
                   1102:     # That's all folks.
                   1103:     return ;
1.2       matthew  1104: }
1.21      matthew  1105: 
                   1106: #---------------------------------------------- check_inputs
                   1107: sub check_inputs {
                   1108:     ## Note: no inputs, no outputs - this acts only on global variables.
                   1109:     ## Make sure we have all the input we need:
1.47      matthew  1110:     if (! %plot) { &set_defaults(\%plot,\%gnuplot_defaults); }
1.23      matthew  1111:     if (! %key ) {} # No key for this plot, thats okay
1.34      matthew  1112: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21      matthew  1113:     if (! defined($title )) {} # No title for this plot, thats okay
                   1114:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
                   1115:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
                   1116:     if ($#labels < 0) { }      # No labels for this plot, thats okay
                   1117:     if ($#curves < 0) { 
                   1118: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
                   1119: 	return '';
                   1120:     }
                   1121:     my $curve;
                   1122:     foreach $curve (@curves) {
                   1123: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
1.85      matthew  1124: 	    &Apache::lonxml::warning("One of the curves specified did not contain any curve data or curve function declarations\n");
1.21      matthew  1125: 	    return '';
                   1126: 	}
                   1127:     }
                   1128: }
                   1129: 
1.20      matthew  1130: #------------------------------------------------ make_edit
                   1131: sub edit_attributes {
1.34      matthew  1132:     my ($target,$token,$defaults,$keys) = @_;
                   1133:     my ($result,@keys);
                   1134:     if ($keys && ref($keys) eq 'ARRAY') {
                   1135:         @keys = @$keys;
                   1136:     } else {
                   1137: 	@keys = sort(keys(%$defaults));
                   1138:     }
                   1139:     foreach my $attr (@keys) {
1.35      matthew  1140: 	# append a ' ' to the description if it doesn't have one already.
                   1141: 	my $description = $defaults->{$attr}->{'description'};
                   1142: 	$description .= ' ' if ($description !~ / $/);
1.20      matthew  1143: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1.35      matthew  1144: 	    $result .= &Apache::edit::text_arg
1.38      matthew  1145: 		($description,$attr,$token,
                   1146: 		 $defaults->{$attr}->{'size'});
1.20      matthew  1147: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1.35      matthew  1148: 	    $result .= &Apache::edit::select_arg
                   1149: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
1.45      matthew  1150: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1.35      matthew  1151: 	    $result .= &Apache::edit::select_arg
                   1152: 		($description,$attr,['on','off'],$token);
1.20      matthew  1153: 	}
1.25      matthew  1154: 	$result .= '<br />';
1.20      matthew  1155:     }
                   1156:     return $result;
                   1157: }
1.1       matthew  1158: 
1.21      matthew  1159: 
                   1160: ###################################################################
                   1161: ##                                                               ##
                   1162: ##           Insertion functions for editing plots               ##
                   1163: ##                                                               ##
                   1164: ###################################################################
                   1165: 
1.47      matthew  1166: sub insert_gnuplot {
1.29      matthew  1167:     my $result = '';
1.20      matthew  1168:     #  plot attributes
1.61      matthew  1169:     $result .= "\n<gnuplot ";
1.47      matthew  1170:     foreach my $attr (keys(%gnuplot_defaults)) {
1.61      matthew  1171: 	$result .= "\n     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
1.20      matthew  1172:     }
1.61      matthew  1173:     $result .= ">";
1.47      matthew  1174:     # Add the components (most are commented out for simplicity)
1.44      matthew  1175:     # $result .= &insert_key();
                   1176:     # $result .= &insert_axis();
                   1177:     # $result .= &insert_title();    
                   1178:     # $result .= &insert_xlabel();    
                   1179:     # $result .= &insert_ylabel();    
1.20      matthew  1180:     $result .= &insert_curve();
1.50      matthew  1181:     # close up the <gnuplot>
1.61      matthew  1182:     $result .= "\n</gnuplot>";
1.45      matthew  1183:     return $result;
                   1184: }
                   1185: 
1.46      matthew  1186: sub insert_tics {
                   1187:     my $result;
                   1188:     $result .= &insert_xtics() . &insert_ytics;
                   1189:     return $result;
                   1190: }
                   1191: 
1.45      matthew  1192: sub insert_xtics {
                   1193:     my $result;
1.46      matthew  1194:     $result .= "\n    <xtics ";
1.45      matthew  1195:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  1196: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1197:     }
1.61      matthew  1198:     $result .= "/>";
1.45      matthew  1199:     return $result;
                   1200: }
                   1201: 
                   1202: sub insert_ytics {
                   1203:     my $result;
1.46      matthew  1204:     $result .= "\n    <ytics ";
1.45      matthew  1205:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  1206: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1207:     }
1.61      matthew  1208:     $result .= "/>";
1.20      matthew  1209:     return $result;
                   1210: }
                   1211: 
                   1212: sub insert_key {
                   1213:     my $result;
1.61      matthew  1214:     $result .= "\n    <key ";
1.30      matthew  1215:     foreach my $attr (keys(%key_defaults)) {
1.61      matthew  1216: 	$result .= "\n         $attr=\"$key_defaults{$attr}->{'default'}\"";
1.20      matthew  1217:     }
1.61      matthew  1218:     $result .= " />";
1.20      matthew  1219:     return $result;
                   1220: }
                   1221: 
                   1222: sub insert_axis{
                   1223:     my $result;
1.46      matthew  1224:     $result .= "\n    <axis ";
1.30      matthew  1225:    foreach my $attr (keys(%axis_defaults)) {
1.61      matthew  1226: 	$result .= "\n         $attr=\"$axis_defaults{$attr}->{'default'}\"";
1.20      matthew  1227:     }
1.61      matthew  1228:     $result .= " />";
1.20      matthew  1229:     return $result;
                   1230: }
1.28      matthew  1231: 
1.61      matthew  1232: sub insert_title  { return "\n    <title></title>"; }
                   1233: sub insert_xlabel { return "\n    <xlabel></xlabel>"; }
                   1234: sub insert_ylabel { return "\n    <ylabel></ylabel>"; }
1.20      matthew  1235: 
                   1236: sub insert_label {
                   1237:     my $result;
1.46      matthew  1238:     $result .= "\n    <label ";
1.30      matthew  1239:     foreach my $attr (keys(%label_defaults)) {
1.61      matthew  1240: 	$result .= "\n         $attr=\"".
                   1241:             $label_defaults{$attr}->{'default'}."\"";
1.20      matthew  1242:     }
1.61      matthew  1243:     $result .= "></label>";
1.20      matthew  1244:     return $result;
                   1245: }
                   1246: 
                   1247: sub insert_curve {
                   1248:     my $result;
1.41      matthew  1249:     $result .= "\n    <curve ";
1.30      matthew  1250:     foreach my $attr (keys(%curve_defaults)) {
1.61      matthew  1251: 	$result .= "\n         $attr=\"".
                   1252: 	    $curve_defaults{$attr}->{'default'}."\"";
1.20      matthew  1253:     }
1.61      matthew  1254:     $result .= " >";
                   1255:     $result .= &insert_data().&insert_data()."\n    </curve>";
1.20      matthew  1256: }
1.4       matthew  1257: 
1.20      matthew  1258: sub insert_function {
                   1259:     my $result;
1.61      matthew  1260:     $result .= "\n        <function></function>";
1.20      matthew  1261:     return $result;
                   1262: }
1.4       matthew  1263: 
1.20      matthew  1264: sub insert_data {
                   1265:     my $result;
1.61      matthew  1266:     $result .= "\n        <data></data>";
1.20      matthew  1267:     return $result;
                   1268: }
1.4       matthew  1269: 
1.48      matthew  1270: ##----------------------------------------------------------------------
1.20      matthew  1271: 1;
                   1272: __END__
1.4       matthew  1273: 
                   1274: 

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