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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.33    ! harris41    4: # $Id: lonplot.pm,v 1.32 2002/01/04 14:30:08 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.3       matthew    28: # 12/15/01 Matthew
1.31      matthew    29: # 12/17 12/18 12/19 12/20 12/21 12/27 12/28 12/30 12/31 Matthew
                     30: # 01/01/02 Matthew
                     31: # 01/02 01/03 Matthew
1.1       matthew    32: package Apache::lonplot;
1.10      matthew    33: 
1.1       matthew    34: use strict;
1.10      matthew    35: use Apache::File;
1.1       matthew    36: use Apache::response;
1.2       matthew    37: use Apache::lonxml;
1.20      matthew    38: use Apache::edit;
1.10      matthew    39: 
1.33    ! harris41   40: BEGIN {
1.1       matthew    41:   &Apache::lonxml::register('Apache::lonplot',('plot'));
                     42: }
                     43: 
1.10      matthew    44: ## 
                     45: ## Description of data structures:
                     46: ##
                     47: ##  %plot       %key    %axis
                     48: ## --------------------------
                     49: ##  height      title   color
                     50: ##  width       box     xmin
                     51: ##  bgcolor     pos     xmax
                     52: ##  fgcolor             ymin
                     53: ##  transparent         ymax
                     54: ##  grid
                     55: ##  border
                     56: ##  font
1.19      matthew    57: ##  align
1.10      matthew    58: ##
                     59: ##  @labels: $labels[$i] = \%label
                     60: ##           %label: text, xpos, ypos, justify
1.14      matthew    61: ##
1.10      matthew    62: ##  @curves: $curves[$i] = \%curve
1.14      matthew    63: ##           %curve: name, linestyle, ( function | data )
1.10      matthew    64: ##
                     65: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
                     66: ##                            [y1,y2,y3,y4] ]
                     67: ##
1.21      matthew    68: 
                     69: ###################################################################
                     70: ##                                                               ##
                     71: ##        Tests used in checking the validitity of input         ##
                     72: ##                                                               ##
                     73: ###################################################################
1.29      matthew    74: 
1.32      matthew    75: my $max_str_len = 50;    # if a label, title, xlabel, or ylabel text
                     76:                          # is longer than this, it will be truncated.
                     77: 
1.29      matthew    78: my %linestyles = 
                     79:     (
                     80:      lines          => 2,     # Maybe this will be used in the future
                     81:      linespoints    => 2,     # to check on whether or not they have 
                     82:      dots	    => 2,     # supplied enough <data></data> fields
                     83:      points         => 2,     # to use the given line style.  But for
                     84:      steps	    => 2,     # now there are more important things 
                     85:      fsteps	    => 2,     # for me to deal with.
                     86:      histeps        => 2,
                     87:      errorbars	    => 2,
                     88:      xerrorbars	    => 2,
                     89:      yerrorbars	    => 2,
                     90:      xyerrorbars    => 2,
                     91:      boxes          => 2,
                     92:      boxerrorbars   => 2,
                     93:      boxxyerrorbars => 2,
                     94:      financebars    => 2,
                     95:      candlesticks   => 2,
                     96:      vector	    => 2
                     97:     );		    
                     98: 
1.11      matthew    99: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19      matthew   100: my $real_test      = 
                    101:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.11      matthew   102: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
1.1       matthew   103: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
1.15      matthew   104: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.1       matthew   105: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
1.29      matthew   106: my $linestyle_test = sub {exists($linestyles{$_[0]})};
1.15      matthew   107: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
1.21      matthew   108: 
                    109: ###################################################################
                    110: ##                                                               ##
                    111: ##                      Attribute metadata                       ##
                    112: ##                                                               ##
                    113: ###################################################################
1.1       matthew   114: my %plot_defaults = 
                    115:     (
1.20      matthew   116:      height       => {
                    117: 	 default     => 200,
                    118: 	 test        => $int_test,
1.29      matthew   119: 	 description => 'height of image (pixels)',
1.20      matthew   120: 	 edit_type   => 'entry' 
                    121: 	 },
                    122:      width        => {
                    123: 	 default     => 200,
                    124: 	 test        => $int_test,
1.29      matthew   125: 	 description => 'width of image (pixels)',
1.20      matthew   126: 	 edit_type   => 'entry'
                    127: 	 },
                    128:      bgcolor      => {
                    129: 	 default     => 'xffffff',
                    130: 	 test        => $color_test, 
                    131: 	 description => 'background color of image (xffffff)',
                    132: 	 edit_type   => 'entry'
                    133: 	 },
                    134:      fgcolor      => {
                    135: 	 default     => 'x000000',
                    136: 	 test        => $color_test,
                    137: 	 description => 'foreground color of image (x000000)',
                    138: 	 edit_type   => 'entry' 
                    139: 	 },
                    140:      transparent  => {
                    141: 	 default     => 'off',
                    142: 	 test        => $onoff_test, 
                    143: 	 description => '',
                    144: 	 edit_type   => 'on_off'
                    145: 	 },
                    146:      grid         => {
                    147: 	 default     => 'off',
                    148: 	 test        => $onoff_test, 
                    149: 	 description => '',
                    150: 	 edit_type   => 'on_off'
                    151: 	 },
                    152:      border       => {
                    153: 	 default     => 'on',
                    154: 	 test        => $onoff_test, 
                    155: 	 description => '',
                    156: 	 edit_type   => 'on_off'
                    157: 	 },
                    158:      font         => {
                    159: 	 default     => 'medium',
                    160: 	 test        => $sml_test,
                    161: 	 description => 'Size of font to use',
                    162: 	 edit_type   => 'choice',
                    163: 	 choices     => ['small','medium','large']
                    164: 	 },
                    165:      align        => {
                    166: 	 default     => 'left',
                    167: 	 test        => sub {$_[0]=~/^(left|right|center)$/},
                    168: 	 description => 'alignment for image in html',
                    169: 	 edit_type   => 'choice',
                    170: 	 choices     => ['left','right','center']
                    171: 	 } 
1.1       matthew   172:      );
                    173: 
                    174: my %key_defaults = 
                    175:     (
1.20      matthew   176:      title => { 
                    177: 	 default => '',
                    178: 	 test => $words_test,
                    179: 	 description => 'Title of key',
                    180: 	 edit_type   => 'entry'
                    181: 	 },
                    182:      box   => { 
                    183: 	 default => 'off',
                    184: 	 test => $onoff_test,
                    185: 	 description => 'Draw a box around the key?',
                    186: 	 edit_type   => 'on_off'
                    187: 	 },
                    188:      pos   => { 
                    189: 	 default => 'top right', 
                    190: 	 test => $key_pos_test, 
                    191: 	 description => 'position of the key on the plot',
                    192: 	 edit_type   => 'choice',
                    193: 	 choices     => ['top left','top right','bottom left','bottom right',
                    194: 			 'outside','below']
                    195: 	 }
1.1       matthew   196:      );
                    197: 
                    198: my %label_defaults = 
                    199:     (
1.20      matthew   200:      xpos    => {
                    201: 	 default => 0,
                    202: 	 test => $real_test,
                    203: 	 description => 'x position of label (graph coordinates)',
                    204: 	 edit_type   => 'entry'
                    205: 	 },
                    206:      ypos    => {
                    207: 	 default => 0, 
                    208: 	 test => $real_test,
                    209: 	 description => 'y position of label (graph coordinates)',
                    210: 	 edit_type   => 'entry'
                    211: 	 },
                    212:      justify => {
                    213: 	 default => 'left',    
                    214: 	 test => sub {$_[0]=~/^(left|right|center)$/},
                    215: 	 description => 'justification of the label text on the plot',
                    216: 	 edit_type   => 'choice',
                    217: 	 choices     => ['left','right','center']
                    218:      }
1.1       matthew   219:      );
                    220: 
                    221: my %axis_defaults = 
                    222:     (
1.28      matthew   223:      color   => {
1.20      matthew   224: 	 default => 'x000000', 
                    225: 	 test => $color_test,
                    226: 	 description => 'color of axes (x000000)',
                    227: 	 edit_type   => 'entry'
                    228: 	 },
                    229:      xmin      => {
                    230: 	 default => '-10.0',
                    231: 	 test => $real_test,
                    232: 	 description => 'minimum x-value shown in plot',
                    233: 	 edit_type   => 'entry'
                    234: 	 },
                    235:      xmax      => {
                    236: 	 default => ' 10.0',
                    237: 	 test => $real_test,
                    238: 	 description => 'maximum x-value shown in plot',	 
                    239: 	 edit_type   => 'entry'
                    240: 	 },
                    241:      ymin      => {
                    242: 	 default => '-10.0',
                    243: 	 test => $real_test,
                    244: 	 description => 'minimum y-value shown in plot',	 
                    245: 	 edit_type   => 'entry'
                    246: 	 },
                    247:      ymax      => {
                    248: 	 default => ' 10.0',
                    249: 	 test => $real_test,
                    250: 	 description => 'maximum y-value shown in plot',	 
                    251: 	 edit_type   => 'entry'
                    252: 	 }
1.1       matthew   253:      );
                    254: 
                    255: my %curve_defaults = 
                    256:     (
1.20      matthew   257:      color     => {
                    258: 	 default => 'x000000',
                    259: 	 test => $color_test,
                    260: 	 description => 'color of curve (x000000)',
                    261: 	 edit_type   => 'entry'
                    262: 	 },
                    263:      name      => {
                    264: 	 default => '',
                    265: 	 test => $words_test,
                    266: 	 description => 'name of curve to appear in key',
                    267: 	 edit_type   => 'entry'
                    268: 	 },
                    269:      linestyle => {
                    270: 	 default => 'lines',
                    271: 	 test => $linestyle_test,
                    272: 	 description => 'Style of the axis lines',
                    273: 	 edit_type   => 'choice',
1.29      matthew   274: 	 choices     => ['lines','linespoints','dots','points','steps',
                    275: 			 'fsteps','histeps','errorbars','xerrorbars',
                    276: 			 'yerrorbars','xyerrorbars','boxes','boxerrorbars',
                    277: 			 'boxxyerrorbars','financebars','candlesticks',
                    278: 			 'vector']
1.20      matthew   279: 	 }
1.1       matthew   280:      );
                    281: 
1.21      matthew   282: ###################################################################
                    283: ##                                                               ##
                    284: ##                    parsing and edit rendering                 ##
                    285: ##                                                               ##
                    286: ###################################################################
1.1       matthew   287: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
                    288: 
                    289: sub start_plot {
1.23      matthew   290:     %plot    = ();      %key     = ();      %axis   = (); 
1.10      matthew   291:     $title   = undef;   $xlabel  = undef;   $ylabel = undef;
                    292:     $#labels = -1;      $#curves = -1;
1.6       matthew   293:     #
1.1       matthew   294:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    295:     my $result='';
1.25      matthew   296:     &Apache::lonxml::register('Apache::lonplot',
                    297: 	     ('title','xlabel','ylabel','key','axis','label','curve'));
1.29      matthew   298:     push (@Apache::lonxml::namespace,'lonplot');
1.4       matthew   299:     if ($target eq 'web') {
1.29      matthew   300: 	my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
1.17      matthew   301: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
1.29      matthew   302: 	&Apache::lonxml::newparser($parser,\$inside);
1.17      matthew   303: 	&get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,
                    304: 			$tagstack->[-1]);
1.20      matthew   305:     } elsif ($target eq 'edit') {
1.25      matthew   306: 	$result .= &Apache::edit::tag_start($target,$token,'Plot');
1.21      matthew   307: 	$result .= &edit_attributes($target,$token,\%plot_defaults);
1.20      matthew   308:     } elsif ($target eq 'modified') {
                    309: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   310: 	    ($token,$parstack,$safeeval,keys(%plot_defaults));
1.20      matthew   311: 	if ($constructtag) {
                    312: 	    $result = &Apache::edit::rebuild_tag($token);
1.26      matthew   313: #	    $result.= &Apache::edit::handle_insert();
1.20      matthew   314: 	}
1.4       matthew   315:     }
1.21      matthew   316:     return $result;
1.1       matthew   317: }
                    318: 
                    319: sub end_plot {
                    320:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.25      matthew   321: 
1.1       matthew   322:     pop @Apache::lonxml::namespace;
1.4       matthew   323:     &Apache::lonxml::deregister('Apache::lonplot',
                    324: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    325:     my $result = '';
                    326:     if ($target eq 'web') {
1.21      matthew   327: 	&check_inputs(); # Make sure we have all the data we need
1.13      matthew   328: 	##
                    329: 	## Determine filename
1.4       matthew   330: 	my $tmpdir = '/home/httpd/perl/tmp/';
1.12      matthew   331: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.29      matthew   332: 	    '_'.time.'_'.$$.int(rand(1000)).'_plot.data';
1.4       matthew   333: 	## Write the plot description to the file
1.12      matthew   334: 	my $fh=Apache::File->new(">$tmpdir$filename");
                    335: 	print $fh &write_gnuplot_file();
1.14      matthew   336: 	close($fh);
1.4       matthew   337: 	## return image tag for the plot
1.12      matthew   338: 	$result .= <<"ENDIMAGE";
1.16      matthew   339: <img src    = "/cgi-bin/plot.gif?$filename" 
                    340:      width  = "$plot{'width'}" 
                    341:      height = "$plot{'height'}"
                    342:      align  = "$plot{'align'}"
                    343:      alt    = "/cgi-bin/plot.gif?$filename" />
1.12      matthew   344: ENDIMAGE
1.20      matthew   345:     } elsif ($target eq 'edit') {
1.21      matthew   346: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   347:     }
1.1       matthew   348:     return $result;
                    349: }
1.2       matthew   350: 
1.1       matthew   351: ##----------------------------------------------------------------- key
                    352: sub start_key {
                    353:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    354:     my $result='';
1.17      matthew   355:     if ($target eq 'web') {
                    356: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   357: 		    $tagstack->[-1]);
1.20      matthew   358:     } elsif ($target eq 'edit') {
1.25      matthew   359: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21      matthew   360: 	$result .= &edit_attributes($target,$token,\%key_defaults);
1.20      matthew   361:     } elsif ($target eq 'modified') {
                    362: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   363: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
1.20      matthew   364: 	if ($constructtag) {
                    365: 	    $result = &Apache::edit::rebuild_tag($token);
                    366: 	    $result.= &Apache::edit::handle_insert();
                    367: 	}
1.4       matthew   368:     }
1.1       matthew   369:     return $result;
                    370: }
                    371: 
                    372: sub end_key {
                    373:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    374:     my $result = '';
1.4       matthew   375:     if ($target eq 'web') {
1.20      matthew   376:     } elsif ($target eq 'edit') {
1.21      matthew   377: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   378:     }
1.1       matthew   379:     return $result;
                    380: }
1.21      matthew   381: 
1.1       matthew   382: ##------------------------------------------------------------------- title
                    383: sub start_title {
                    384:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    385:     my $result='';
1.4       matthew   386:     if ($target eq 'web') {
1.17      matthew   387: 	$title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.32      matthew   388: 	if (length($title) > $max_str_len) {
                    389: 	    $title = substr($title,0,$max_str_len);
                    390: 	}
1.20      matthew   391:     } elsif ($target eq 'edit') {
1.25      matthew   392: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.22      matthew   393: 	my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
                    394: 	$result.='</td></tr><tr><td colspan="3">'.
1.30      matthew   395: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   396:     } elsif ($target eq 'modified') {
1.29      matthew   397: 	my $text=$$parser[-1]->get_text("/title");
1.21      matthew   398: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   399:     }
1.1       matthew   400:     return $result;
                    401: }
                    402: 
                    403: sub end_title {
                    404:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    405:     my $result = '';
1.4       matthew   406:     if ($target eq 'web') {
1.20      matthew   407:     } elsif ($target eq 'edit') {
1.27      matthew   408: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   409:     }
1.1       matthew   410:     return $result;
                    411: }
                    412: ##------------------------------------------------------------------- xlabel
                    413: sub start_xlabel {
                    414:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    415:     my $result='';
1.4       matthew   416:     if ($target eq 'web') {
1.17      matthew   417: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.32      matthew   418: 	if (length($xlabel) > $max_str_len) {
                    419: 	    $xlabel = substr($xlabel,0,$max_str_len);
                    420: 	}
1.20      matthew   421:     } elsif ($target eq 'edit') {
1.25      matthew   422: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.22      matthew   423: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
                    424: 	$result.='</td></tr><tr><td colspan="3">'.
1.30      matthew   425: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   426:     } elsif ($target eq 'modified') {
1.29      matthew   427: 	my $text=$$parser[-1]->get_text("/xlabel");
1.21      matthew   428: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   429:     }
1.1       matthew   430:     return $result;
                    431: }
                    432: 
                    433: sub end_xlabel {
                    434:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    435:     my $result = '';
1.4       matthew   436:     if ($target eq 'web') {
1.20      matthew   437:     } elsif ($target eq 'edit') {
1.27      matthew   438: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   439:     }
1.1       matthew   440:     return $result;
                    441: }
1.21      matthew   442: 
1.1       matthew   443: ##------------------------------------------------------------------- ylabel
                    444: sub start_ylabel {
                    445:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    446:     my $result='';
1.4       matthew   447:     if ($target eq 'web') {
1.17      matthew   448: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.32      matthew   449: 	if (length($ylabel) > $max_str_len) {
                    450: 	    $ylabel = substr($ylabel,0,$max_str_len);
                    451: 	}
1.20      matthew   452:     } elsif ($target eq 'edit') {
1.25      matthew   453: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.22      matthew   454: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
                    455: 	$result .= '</td></tr><tr><td colspan="3">'.
1.30      matthew   456: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   457:     } elsif ($target eq 'modified') {
1.29      matthew   458: 	my $text=$$parser[-1]->get_text("/ylabel");
1.21      matthew   459: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   460:     }
1.1       matthew   461:     return $result;
                    462: }
                    463: 
                    464: sub end_ylabel {
                    465:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    466:     my $result = '';
1.4       matthew   467:     if ($target eq 'web') {
1.20      matthew   468:     } elsif ($target eq 'edit') {
1.27      matthew   469: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   470:     }
1.1       matthew   471:     return $result;
                    472: }
1.21      matthew   473: 
1.1       matthew   474: ##------------------------------------------------------------------- label
                    475: sub start_label {
                    476:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    477:     my $result='';
1.17      matthew   478:     if ($target eq 'web') {
                    479: 	my %label;
                    480: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew   481: 		    $tagstack->[-1]);
1.32      matthew   482: 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
                    483: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
                    484: 	$label{'text'} = $text;
1.17      matthew   485: 	push(@labels,\%label);
1.20      matthew   486:     } elsif ($target eq 'edit') {
1.25      matthew   487: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21      matthew   488: 	$result .= &edit_attributes($target,$token,\%label_defaults);
1.22      matthew   489: 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
                    490: 	$result .= '</td></tr><tr><td colspan="3">'.
1.30      matthew   491: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   492:     } elsif ($target eq 'modified') {
                    493: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   494: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
1.20      matthew   495: 	if ($constructtag) {
                    496: 	    $result = &Apache::edit::rebuild_tag($token);
                    497: 	    $result.= &Apache::edit::handle_insert();
                    498: 	}
1.22      matthew   499: 	my $text=$$parser[-1]->get_text("/label");
1.21      matthew   500: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   501:     }
1.1       matthew   502:     return $result;
                    503: }
                    504: 
                    505: sub end_label {
                    506:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    507:     my $result = '';
1.4       matthew   508:     if ($target eq 'web') {
1.20      matthew   509:     } elsif ($target eq 'edit') {
1.21      matthew   510: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   511:     }
1.1       matthew   512:     return $result;
                    513: }
                    514: 
                    515: ##------------------------------------------------------------------- curve
                    516: sub start_curve {
                    517:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    518:     my $result='';
1.25      matthew   519:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
                    520:     push (@Apache::lonxml::namespace,'curve');
1.17      matthew   521:     if ($target eq 'web') {
                    522: 	my %curve;
                    523: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew   524: 		    $tagstack->[-1]);
1.17      matthew   525: 	push (@curves,\%curve);
1.20      matthew   526:     } elsif ($target eq 'edit') {
1.26      matthew   527: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
1.21      matthew   528: 	$result .= &edit_attributes($target,$token,\%curve_defaults);
1.20      matthew   529:     } elsif ($target eq 'modified') {
                    530: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   531: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
1.20      matthew   532: 	if ($constructtag) {
                    533: 	    $result = &Apache::edit::rebuild_tag($token);
                    534: 	    $result.= &Apache::edit::handle_insert();
                    535: 	}
1.4       matthew   536:     }
1.1       matthew   537:     return $result;
                    538: }
                    539: 
                    540: sub end_curve {
                    541:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    542:     my $result = '';
1.25      matthew   543:     pop @Apache::lonxml::namespace;
                    544:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.4       matthew   545:     if ($target eq 'web') {
1.20      matthew   546:     } elsif ($target eq 'edit') {
1.21      matthew   547: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   548:     }
1.1       matthew   549:     return $result;
                    550: }
1.21      matthew   551: 
1.1       matthew   552: ##------------------------------------------------------------ curve function
                    553: sub start_function {
                    554:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    555:     my $result='';
1.4       matthew   556:     if ($target eq 'web') {
1.17      matthew   557: 	if (exists($curves[-1]->{'data'})) {
                    558: 	    &Apache::lonxml::warning('Use of <function> precludes use of <data>.  The <data> will be omitted in favor of the <function> declaration.');
                    559: 	    delete $curves[-1]->{'data'} ;
                    560: 	}
                    561: 	$curves[-1]->{'function'} = 
                    562: 	    &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.20      matthew   563:     } elsif ($target eq 'edit') {
1.25      matthew   564: 	$result .= &Apache::edit::tag_start($target,$token,'Curve Function');
1.22      matthew   565: 	my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
                    566: 	$result .= '</td></tr><tr><td colspan="3">'.
1.30      matthew   567: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   568:     } elsif ($target eq 'modified') {
                    569: 	# Why do I do this?
                    570: 	my $text=$$parser[-1]->get_text("/function");
                    571: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   572:     }
1.1       matthew   573:     return $result;
                    574: }
                    575: 
                    576: sub end_function {
                    577:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    578:     my $result = '';
1.4       matthew   579:     if ($target eq 'web') {
1.20      matthew   580:     } elsif ($target eq 'edit') {
1.26      matthew   581: 	$result .= &Apache::edit::end_table();
1.4       matthew   582:     }
1.1       matthew   583:     return $result;
                    584: }
1.21      matthew   585: 
1.1       matthew   586: ##------------------------------------------------------------ curve  data
                    587: sub start_data {
                    588:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    589:     my $result='';
1.4       matthew   590:     if ($target eq 'web') {
1.17      matthew   591: 	if (exists($curves[-1]->{'function'})) {
                    592: 	    &Apache::lonxml::warning('Use of <data> precludes use of .'.
                    593: 	    '<function>.  The <function> will be omitted in favor of '.
                    594:             'the <data> declaration.');
                    595: 	    delete($curves[-1]->{'function'});
                    596: 	}
                    597: 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
                    598: 	$datatext =~ s/\s+/ /g;  
                    599: 	# Need to do some error checking on the @data array - 
                    600: 	# make sure it's all numbers and make sure each array 
                    601: 	# is of the same length.
                    602: 	my @data;
                    603: 	if ($datatext =~ /,/) {
                    604: 	    @data = split /,/,$datatext;
                    605: 	} else { # Assume it's space seperated.
                    606: 	    @data = split / /,$datatext;
                    607: 	}
                    608: 	for (my $i=0;$i<=$#data;$i++) {
                    609: 	    # Check that it's non-empty
1.19      matthew   610: 	    if (! defined($data[$i])) {
                    611: 		&Apache::lonxml::warning(
                    612: 		    'undefined <data> value.  Replacing with '.
                    613: 		    ' pi/e = 1.15572734979092');
                    614: 		$data[$i] = 1.15572734979092;
                    615: 	    }
1.17      matthew   616: 	    # Check that it's a number
1.19      matthew   617: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
                    618: 		&Apache::lonxml::warning(
                    619: 		    'Bad <data> value of '.$data[$i].'  Replacing with '.
                    620: 		    ' pi/e = 1.15572734979092');
                    621: 		$data[$i] = 1.15572734979092;
                    622: 	    }
1.17      matthew   623: 	}
                    624: 	push  @{$curves[-1]->{'data'}},\@data;
1.20      matthew   625:     } elsif ($target eq 'edit') {
1.25      matthew   626: 	$result .= &Apache::edit::tag_start($target,$token,'Curve Data');
1.22      matthew   627: 	my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
                    628: 	$result .= '</td></tr><tr><td colspan="3">'.
1.30      matthew   629: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   630:     } elsif ($target eq 'modified') {
1.21      matthew   631: 	my $text=$$parser[-1]->get_text("/data");
                    632: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   633:     }
1.1       matthew   634:     return $result;
                    635: }
                    636: 
                    637: sub end_data {
                    638:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    639:     my $result = '';
1.4       matthew   640:     if ($target eq 'web') {
1.20      matthew   641:     } elsif ($target eq 'edit') {
1.26      matthew   642: 	$result .= &Apache::edit::end_table();
1.4       matthew   643:     }
1.1       matthew   644:     return $result;
                    645: }
                    646: 
                    647: ##------------------------------------------------------------------- axis
                    648: sub start_axis {
                    649:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    650:     my $result='';
1.4       matthew   651:     if ($target eq 'web') {
1.17      matthew   652: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                    653: 			$tagstack->[-1]);
1.20      matthew   654:     } elsif ($target eq 'edit') {
1.25      matthew   655: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.21      matthew   656: 	$result .= &edit_attributes($target,$token,\%axis_defaults);
1.20      matthew   657:     } elsif ($target eq 'modified') {
1.29      matthew   658: 	my $constructtag=&Apache::edit::get_new_args
                    659: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
                    660: 	if ($constructtag) {
                    661: 	    $result = &Apache::edit::rebuild_tag($token);
                    662: 	    $result.= &Apache::edit::handle_insert();
                    663: 	}
1.4       matthew   664:     }
1.1       matthew   665:     return $result;
                    666: }
                    667: 
                    668: sub end_axis {
                    669:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    670:     my $result = '';
1.4       matthew   671:     if ($target eq 'web') {
1.20      matthew   672:     } elsif ($target eq 'edit') {
1.21      matthew   673: 	$result.=&Apache::edit::tag_end($target,$token);
1.20      matthew   674:     } elsif ($target eq 'modified') {
1.4       matthew   675:     }
1.1       matthew   676:     return $result;
                    677: }
                    678: 
1.21      matthew   679: ###################################################################
                    680: ##                                                               ##
                    681: ##        Utility Functions                                      ##
                    682: ##                                                               ##
                    683: ###################################################################
                    684: 
1.13      matthew   685: ##----------------------------------------------------------- set_defaults
                    686: sub set_defaults {
1.21      matthew   687:     my ($var,$defaults) = @_;
1.13      matthew   688:     my $key;
1.24      matthew   689:     foreach $key (keys(%$defaults)) {
1.13      matthew   690: 	$var->{$key} = $defaults->{$key}->{'default'};
                    691:     }
                    692: }
                    693: 
1.1       matthew   694: ##------------------------------------------------------------------- misc
1.2       matthew   695: sub get_attributes{
1.21      matthew   696:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24      matthew   697:     foreach my $attr (keys(%{$defaults})) {
1.10      matthew   698: 	$values->{$attr} = 
1.15      matthew   699: 	    &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1.10      matthew   700: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew   701: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew   702: 	    next;
                    703: 	}
1.10      matthew   704: 	my $test = $defaults->{$attr}->{'test'};
                    705: 	if (! &$test($values->{$attr})) {
1.6       matthew   706: 	    &Apache::lonxml::warning
                    707: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew   708: 		 .$defaults->{$attr}->{'default'} );
                    709: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew   710: 	}
1.2       matthew   711:     }
1.11      matthew   712:     return ;
1.6       matthew   713: }
1.15      matthew   714: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew   715: sub write_gnuplot_file {
                    716:     my $gnuplot_input = '';
1.10      matthew   717:     my $curve;
1.6       matthew   718:     # Collect all the colors
                    719:     my @Colors;
                    720:     push @Colors, $plot{'bgcolor'};
                    721:     push @Colors, $plot{'fgcolor'}; 
1.13      matthew   722:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9       matthew   723:     foreach $curve (@curves) {
                    724: 	push @Colors, ($curve->{'color'} ne '' ? 
                    725: 		       $curve->{'color'}       : 
1.13      matthew   726: 		       $plot{'fgcolor'}        );
1.6       matthew   727:     }
                    728:     # set term
                    729:     $gnuplot_input .= 'set term gif ';
                    730:     $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
                    731:     $gnuplot_input .= $plot{'font'} . ' ';
1.10      matthew   732:     $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
1.6       matthew   733:     $gnuplot_input .= "@Colors\n";
1.7       matthew   734:     # grid
1.10      matthew   735:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7       matthew   736:     # border
1.9       matthew   737:     $gnuplot_input .= ($plot{'border'} eq 'on'?
                    738: 		       'set border'.$/           :
                    739: 		       'set noborder'.$/         );    # title, xlabel, ylabel
1.13      matthew   740:     $gnuplot_input .= "set output\n";
                    741:     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;
                    742:     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
                    743:     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
1.23      matthew   744:     if (%axis) {
1.13      matthew   745: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                    746: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6       matthew   747:     }
                    748:     # Key
1.23      matthew   749:     if (%key) {
1.9       matthew   750: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                    751: 	if ($key{'title'} ne '') {
1.11      matthew   752: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
                    753: 	} 
                    754: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew   755:     } else {
1.9       matthew   756: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew   757:     }
1.6       matthew   758:     # labels
1.10      matthew   759:     my $label;
1.6       matthew   760:     foreach $label (@labels) {
                    761: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.9       matthew   762: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
1.6       matthew   763:     }
                    764:     # curves
                    765:     $gnuplot_input .= 'plot ';
                    766:     my $datatext = '';
1.9       matthew   767:     for (my $i = 0;$i<=$#curves;$i++) {
                    768: 	$curve = $curves[$i];
                    769: 	$gnuplot_input.= ', ' if ($i > 0);
1.6       matthew   770: 	if (exists($curve->{'function'})) {
1.9       matthew   771: 	    $gnuplot_input.= 
                    772: 		$curve->{'function'}.' title "'.
                    773: 		$curve->{'name'}.'" with '.
                    774: 		$curve->{'linestyle'};
1.6       matthew   775: 	} elsif (exists($curve->{'data'})) {
1.9       matthew   776: 	    $gnuplot_input.= '\'-\' title "'.
                    777: 		$curve->{'name'}.'" with '.
                    778: 		$curve->{'linestyle'};
1.6       matthew   779: 	    my @Data = @{$curve->{'data'}};
1.9       matthew   780: 	    my @Data0 = @{$Data[0]};
                    781: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew   782: 		my $dataset;
1.6       matthew   783: 		foreach $dataset (@Data) {
1.9       matthew   784: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew   785: 		}
1.9       matthew   786: 		$datatext .= $/;
1.6       matthew   787: 	    }
1.9       matthew   788: 	    $datatext .=$/;
1.6       matthew   789: 	}
                    790:     }
1.9       matthew   791:     $gnuplot_input .= $/.$datatext;
1.10      matthew   792:     return $gnuplot_input;
1.2       matthew   793: }
1.21      matthew   794: 
                    795: #---------------------------------------------- check_inputs
                    796: sub check_inputs {
                    797:     ## Note: no inputs, no outputs - this acts only on global variables.
                    798:     ## Make sure we have all the input we need:
1.23      matthew   799:     if (! %plot) { &set_defaults(\%plot,\%plot_defaults); }
                    800:     if (! %key ) {} # No key for this plot, thats okay
                    801:     if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21      matthew   802:     if (! defined($title )) {} # No title for this plot, thats okay
                    803:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
                    804:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
                    805:     if ($#labels < 0) { }      # No labels for this plot, thats okay
                    806:     if ($#curves < 0) { 
                    807: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
                    808: 	return '';
                    809:     }
                    810:     my $curve;
                    811:     foreach $curve (@curves) {
                    812: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
                    813: 	    &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
                    814: 	    return '';
                    815: 	}
                    816:     }
                    817: }
                    818: 
1.20      matthew   819: #------------------------------------------------ make_edit
                    820: sub edit_attributes {
1.21      matthew   821:     my ($target,$token,$defaults) = @_;
1.20      matthew   822:     my $result;
1.25      matthew   823:     foreach my $attr (sort keys(%$defaults)) {
1.20      matthew   824: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
                    825: 	    $result .= &Apache::edit::text_arg(
                    826:                  $defaults->{$attr}->{'description'},
                    827: 		 $attr,
                    828: 		 $token);
                    829: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
                    830: 	    $result .= &Apache::edit::select_arg(
                    831: 		 $defaults->{$attr}->{'description'},
                    832: 		 $attr,
                    833: 		 $defaults->{$attr}->{'choices'},
                    834: 		 $token);
                    835: 	}
1.25      matthew   836: 	$result .= '<br />';
1.20      matthew   837:     }
                    838:     return $result;
                    839: }
1.1       matthew   840: 
1.21      matthew   841: 
                    842: ###################################################################
                    843: ##                                                               ##
                    844: ##           Insertion functions for editing plots               ##
                    845: ##                                                               ##
                    846: ###################################################################
                    847: 
1.20      matthew   848: #------------------------------------------------ insert_xxxxxxx
                    849: sub insert_plot {
1.29      matthew   850:     my $result = '';
1.20      matthew   851:     #  plot attributes
1.29      matthew   852:     $result .= "<plot \n";
1.30      matthew   853:     foreach my $attr (keys(%plot_defaults)) {
1.29      matthew   854: 	$result .= "     $attr=\"$plot_defaults{$attr}->{'default'}\"\n";
1.20      matthew   855:     }
                    856:     $result .= ">\n";
                    857:     # Add the components
                    858:     $result .= &insert_key();
                    859:     $result .= &insert_axis();
1.29      matthew   860:     $result .= &insert_title();    
                    861:     $result .= &insert_xlabel();    
                    862:     $result .= &insert_ylabel();    
1.20      matthew   863:     $result .= &insert_curve();
                    864:     # close up the <plot>
                    865:     $result .= "</plot>\n";
                    866:     return $result;
                    867: }
                    868: 
                    869: sub insert_key {
                    870:     my $result;
1.29      matthew   871:     $result .= "    <key \n";
1.30      matthew   872:     foreach my $attr (keys(%key_defaults)) {
1.29      matthew   873: 	$result .= "         $attr=\"$key_defaults{$attr}->{'default'}\"\n";
1.20      matthew   874:     }
                    875:     $result .= "   />\n";
                    876:     return $result;
                    877: }
                    878: 
                    879: sub insert_axis{
                    880:     my $result;
                    881:     $result .= '    <axis ';
1.30      matthew   882:    foreach my $attr (keys(%axis_defaults)) {
1.29      matthew   883: 	$result .= "         $attr=\"$axis_defaults{$attr}->{'default'}\"\n";
1.20      matthew   884:     }
                    885:     $result .= "   />\n";
                    886:     return $result;
                    887: }
1.28      matthew   888: 
                    889: sub insert_title { return "    <title></title>\n"; }
1.29      matthew   890: sub insert_xlabel { return "    <xlabel></xlabel>\n"; }
                    891: sub insert_ylabel { return "    <ylabel></ylabel>\n"; }
1.20      matthew   892: 
                    893: sub insert_label {
                    894:     my $result;
                    895:     $result .= '    <label ';
1.30      matthew   896:     foreach my $attr (keys(%label_defaults)) {
1.27      matthew   897: 	$result .= '         '.$attr.'="'.
1.20      matthew   898: 	    $label_defaults{$attr}->{'default'}."\"\n";
                    899:     }
                    900:     $result .= "   ></label>\n";
                    901:     return $result;
                    902: }
                    903: 
                    904: sub insert_curve {
                    905:     my $result;
                    906:     $result .= '    <curve ';
1.30      matthew   907:     foreach my $attr (keys(%curve_defaults)) {
1.27      matthew   908: 	$result .= '         '.$attr.'="'.
1.20      matthew   909: 	    $curve_defaults{$attr}->{'default'}."\"\n";
                    910:     }
1.29      matthew   911:     $result .= "    ></curve>\n";
1.20      matthew   912: }
1.4       matthew   913: 
1.20      matthew   914: sub insert_function {
                    915:     my $result;
                    916:     $result .= "<function></function>\n";
                    917:     return $result;
                    918: }
1.4       matthew   919: 
1.20      matthew   920: sub insert_data {
                    921:     my $result;
                    922:     $result .= "     <data></data>\n";
                    923:     return $result;
                    924: }
1.4       matthew   925: 
1.21      matthew   926: ##----------------------------------------------------------------------
1.20      matthew   927: 1;
                    928: __END__
1.4       matthew   929: 
                    930: 

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