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

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

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