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

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

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