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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.21    ! matthew     4: # $Id: lonplot.pm,v 1.20 2001/12/27 22:30:01 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.20      matthew    29: # 12/17 12/18 12/19 12/20 12/21 12/27 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.20      matthew   196:      color     => {
                    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: 	 },
                    226:      linestyle => {
                    227: 	 default => 'points',
                    228: 	 test => $linestyle_test,
                    229: 	 description => 'Style of the axis lines',
                    230: 	 edit_type   => 'choice',
                    231: 	 choices     => ['lines','linespoints','dots','points']
                    232: 	 }
1.1       matthew   233:      );
                    234: 
                    235: my %curve_defaults = 
                    236:     (
1.20      matthew   237:      color     => {
                    238: 	 default => 'x000000',
                    239: 	 test => $color_test,
                    240: 	 description => 'color of curve (x000000)',
                    241: 	 edit_type   => 'entry'
                    242: 	 },
                    243:      name      => {
                    244: 	 default => '',
                    245: 	 test => $words_test,
                    246: 	 description => 'name of curve to appear in key',
                    247: 	 edit_type   => 'entry'
                    248: 	 },
                    249:      linestyle => {
                    250: 	 default => 'lines',
                    251: 	 test => $linestyle_test,
                    252: 	 description => 'Style of the axis lines',
                    253: 	 edit_type   => 'choice',
                    254: 	 choices     => ['lines','linespoints','dots','points','steps']
                    255: 	 }
1.1       matthew   256:      );
                    257: 
1.21    ! matthew   258: ###################################################################
        !           259: ##                                                               ##
        !           260: ##                    parsing and edit rendering                 ##
        !           261: ##                                                               ##
        !           262: ###################################################################
1.1       matthew   263: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
                    264: 
                    265: sub start_plot {
1.10      matthew   266:     %plot    = undef;   %key     = undef;   %axis   = undef; 
                    267:     $title   = undef;   $xlabel  = undef;   $ylabel = undef;
                    268:     $#labels = -1;      $#curves = -1;
1.6       matthew   269:     #
1.1       matthew   270:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    271:     my $result='';
1.4       matthew   272:     if ($target eq 'web') {
1.17      matthew   273: 	&Apache::lonxml::register('Apache::lonplot',
                    274: 	      ('title','xlabel','ylabel','key','axis','label','curve'));
                    275: 	push (@Apache::lonxml::namespace,'plot');
                    276: 	## Always evaluate the insides of the <plot></plot> tags
                    277: 	my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
                    278: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    279: 	&Apache::lonxml::newparser($parser,\$inside);
                    280: 	##-------------------------------------------------------
                    281: 	&get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,
                    282: 			$tagstack->[-1]);
1.20      matthew   283:     } elsif ($target eq 'edit') {
1.21    ! matthew   284: 	$result .= &Apache::edit::tag_start($target,$token);
        !           285: 	$result .= &edit_attributes($target,$token,\%plot_defaults);
1.20      matthew   286:     } elsif ($target eq 'modified') {
                    287: 	my $constructtag=&Apache::edit::get_new_args
                    288: 	    ($token,$parstack,$safeeval,keys %plot_defaults);
                    289: 	if ($constructtag) {
                    290: 	    $result = &Apache::edit::rebuild_tag($token);
                    291: 	    $result.= &Apache::edit::handle_insert();
                    292: 	}
1.4       matthew   293:     }
1.21    ! matthew   294:     return $result;
1.1       matthew   295: }
                    296: 
                    297: sub end_plot {
                    298:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    299:     pop @Apache::lonxml::namespace;
1.4       matthew   300:     &Apache::lonxml::deregister('Apache::lonplot',
                    301: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    302:     my $result = '';
                    303:     if ($target eq 'web') {
1.21    ! matthew   304: 	&check_inputs(); # Make sure we have all the data we need
1.13      matthew   305: 	##
                    306: 	## Determine filename
1.4       matthew   307: 	my $tmpdir = '/home/httpd/perl/tmp/';
1.12      matthew   308: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.13      matthew   309: 	    '_'.time.'_'.$$.'_plot.data';
1.4       matthew   310: 	## Write the plot description to the file
1.12      matthew   311: 	my $fh=Apache::File->new(">$tmpdir$filename");
                    312: 	print $fh &write_gnuplot_file();
1.14      matthew   313: 	close($fh);
1.4       matthew   314: 	## return image tag for the plot
1.12      matthew   315: 	$result .= <<"ENDIMAGE";
1.16      matthew   316: <img src    = "/cgi-bin/plot.gif?$filename" 
                    317:      width  = "$plot{'width'}" 
                    318:      height = "$plot{'height'}"
                    319:      align  = "$plot{'align'}"
                    320:      alt    = "/cgi-bin/plot.gif?$filename" />
1.12      matthew   321: ENDIMAGE
1.20      matthew   322:     } elsif ($target eq 'edit') {
1.21    ! matthew   323: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   324:     }
1.1       matthew   325:     return $result;
                    326: }
1.2       matthew   327: 
1.1       matthew   328: ##----------------------------------------------------------------- key
                    329: sub start_key {
                    330:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    331:     my $result='';
1.17      matthew   332:     if ($target eq 'web') {
                    333: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   334: 		    $tagstack->[-1]);
1.20      matthew   335:     } elsif ($target eq 'edit') {
1.21    ! matthew   336: 	$result .= &Apache::edit::tag_start($target,$token);
        !           337: 	$result .= &edit_attributes($target,$token,\%key_defaults);
1.20      matthew   338:     } elsif ($target eq 'modified') {
                    339: 	my $constructtag=&Apache::edit::get_new_args
                    340: 	    ($token,$parstack,$safeeval,keys %key_defaults);
                    341: 	if ($constructtag) {
                    342: 	    $result = &Apache::edit::rebuild_tag($token);
                    343: 	    $result.= &Apache::edit::handle_insert();
                    344: 	}
1.4       matthew   345:     }
1.1       matthew   346:     return $result;
                    347: }
                    348: 
                    349: sub end_key {
                    350:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    351:     my $result = '';
1.4       matthew   352:     if ($target eq 'web') {
1.20      matthew   353:     } elsif ($target eq 'edit') {
1.21    ! matthew   354: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   355:     }
1.1       matthew   356:     return $result;
                    357: }
1.21    ! matthew   358: 
1.1       matthew   359: ##------------------------------------------------------------------- title
                    360: sub start_title {
                    361:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    362:     my $result='';
1.4       matthew   363:     if ($target eq 'web') {
1.17      matthew   364: 	$title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.20      matthew   365:     } elsif ($target eq 'edit') {
                    366:     } elsif ($target eq 'modified') {
1.21    ! matthew   367: 	my $text=$$parser[-1]->get_text("/function");
        !           368: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   369:     }
1.1       matthew   370:     return $result;
                    371: }
                    372: 
                    373: sub end_title {
                    374:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    375:     my $result = '';
1.4       matthew   376:     if ($target eq 'web') {
1.20      matthew   377:     } elsif ($target eq 'edit') {
1.4       matthew   378:     }
1.1       matthew   379:     return $result;
                    380: }
                    381: ##------------------------------------------------------------------- xlabel
                    382: sub start_xlabel {
                    383:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    384:     my $result='';
1.4       matthew   385:     if ($target eq 'web') {
1.17      matthew   386: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.20      matthew   387:     } elsif ($target eq 'edit') {
                    388:     } elsif ($target eq 'modified') {
1.21    ! matthew   389: 	my $text=$$parser[-1]->get_text("/function");
        !           390: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   391:     }
1.1       matthew   392:     return $result;
                    393: }
                    394: 
                    395: sub end_xlabel {
                    396:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    397:     my $result = '';
1.4       matthew   398:     if ($target eq 'web') {
1.20      matthew   399:     } elsif ($target eq 'edit') {
1.4       matthew   400:     }
1.1       matthew   401:     return $result;
                    402: }
1.21    ! matthew   403: 
1.1       matthew   404: ##------------------------------------------------------------------- ylabel
                    405: sub start_ylabel {
                    406:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    407:     my $result='';
1.4       matthew   408:     if ($target eq 'web') {
1.17      matthew   409: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.20      matthew   410:     } elsif ($target eq 'edit') {
                    411:     } elsif ($target eq 'modified') {
1.21    ! matthew   412: 	my $text=$$parser[-1]->get_text("/function");
        !           413: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   414:     }
1.1       matthew   415:     return $result;
                    416: }
                    417: 
                    418: sub end_ylabel {
                    419:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    420:     my $result = '';
1.4       matthew   421:     if ($target eq 'web') {
1.20      matthew   422:     } elsif ($target eq 'edit') {
1.4       matthew   423:     }
1.1       matthew   424:     return $result;
                    425: }
1.21    ! matthew   426: 
1.1       matthew   427: ##------------------------------------------------------------------- label
                    428: sub start_label {
                    429:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    430:     my $result='';
1.17      matthew   431:     if ($target eq 'web') {
                    432: 	my %label;
                    433: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew   434: 		    $tagstack->[-1]);
1.17      matthew   435: 	$label{'text'} = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
                    436: 	if (! &$words_test($label{'text'})) {
                    437: 	    # I should probably warn about it, too.
                    438: 	    $label{'text'} = 'Illegal text';
                    439: 	}
                    440: 	push(@labels,\%label);
1.20      matthew   441:     } elsif ($target eq 'edit') {
1.21    ! matthew   442: 	$result .= &Apache::edit::tag_start($target,$token);
        !           443: 	$result .= &edit_attributes($target,$token,\%label_defaults);
1.20      matthew   444:     } elsif ($target eq 'modified') {
                    445: 	my $constructtag=&Apache::edit::get_new_args
                    446: 	    ($token,$parstack,$safeeval,keys %label_defaults);
                    447: 	if ($constructtag) {
                    448: 	    $result = &Apache::edit::rebuild_tag($token);
                    449: 	    $result.= &Apache::edit::handle_insert();
                    450: 	}
1.21    ! matthew   451: 	my $text=$$parser[-1]->get_text("/function");
        !           452: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   453:     }
1.1       matthew   454:     return $result;
                    455: }
                    456: 
                    457: sub end_label {
                    458:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    459:     my $result = '';
1.4       matthew   460:     if ($target eq 'web') {
1.20      matthew   461:     } elsif ($target eq 'edit') {
1.21    ! matthew   462: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   463:     }
1.1       matthew   464:     return $result;
                    465: }
                    466: 
                    467: ##------------------------------------------------------------------- curve
                    468: sub start_curve {
                    469:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    470:     my $result='';
1.17      matthew   471:     if ($target eq 'web') {
                    472: 	my %curve;
                    473: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew   474: 		    $tagstack->[-1]);
1.17      matthew   475: 	push (@curves,\%curve);
                    476: 	&Apache::lonxml::register('Apache::lonplot',('function','data'));
                    477: 	push (@Apache::lonxml::namespace,'curve');
1.20      matthew   478:     } elsif ($target eq 'edit') {
1.21    ! matthew   479: 	$result .= &Apache::edit::tag_start($target,$token);
        !           480: 	$result .= &edit_attributes($target,$token,\%curve_defaults);
1.20      matthew   481:     } elsif ($target eq 'modified') {
                    482: 	my $constructtag=&Apache::edit::get_new_args
                    483: 	    ($token,$parstack,$safeeval,keys %label_defaults);
                    484: 	if ($constructtag) {
                    485: 	    $result = &Apache::edit::rebuild_tag($token);
                    486: 	    $result.= &Apache::edit::handle_insert();
                    487: 	}
1.4       matthew   488:     }
1.1       matthew   489:     return $result;
                    490: }
                    491: 
                    492: sub end_curve {
                    493:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    494:     my $result = '';
1.4       matthew   495:     if ($target eq 'web') {
1.17      matthew   496: 	pop @Apache::lonxml::namespace;
                    497: 	&Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.20      matthew   498:     } elsif ($target eq 'edit') {
1.21    ! matthew   499: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   500:     }
1.1       matthew   501:     return $result;
                    502: }
1.21    ! matthew   503: 
1.1       matthew   504: ##------------------------------------------------------------ curve function
                    505: sub start_function {
                    506:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    507:     my $result='';
1.4       matthew   508:     if ($target eq 'web') {
1.17      matthew   509: 	if (exists($curves[-1]->{'data'})) {
                    510: 	    &Apache::lonxml::warning('Use of <function> precludes use of <data>.  The <data> will be omitted in favor of the <function> declaration.');
                    511: 	    delete $curves[-1]->{'data'} ;
                    512: 	}
                    513: 	$curves[-1]->{'function'} = 
                    514: 	    &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.20      matthew   515:     } elsif ($target eq 'edit') {
                    516: 	$result.=&Apache::edit::tag_start($target,$token);
                    517: 	my $text=&Apache::lonxml::get_all_text("/function",$$parser[-1]);
                    518: 	$result.='</td></tr><tr><td colspan="3">'.
                    519: 	    &Apache::edit::editfield('',$text,'',20,1).
                    520: 	    &Apache::edit::end_table();
                    521:     } elsif ($target eq 'modified') {
                    522: 	# Why do I do this?
                    523: 	my $text=$$parser[-1]->get_text("/function");
                    524: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   525:     }
1.1       matthew   526:     return $result;
                    527: }
                    528: 
                    529: sub end_function {
                    530:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    531:     my $result = '';
1.4       matthew   532:     if ($target eq 'web') {
1.20      matthew   533:     } elsif ($target eq 'edit') {
1.4       matthew   534:     }
1.1       matthew   535:     return $result;
                    536: }
1.21    ! matthew   537: 
1.1       matthew   538: ##------------------------------------------------------------ curve  data
                    539: sub start_data {
                    540:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    541:     my $result='';
1.4       matthew   542:     if ($target eq 'web') {
1.17      matthew   543: 	if (exists($curves[-1]->{'function'})) {
                    544: 	    &Apache::lonxml::warning('Use of <data> precludes use of .'.
                    545: 	    '<function>.  The <function> will be omitted in favor of '.
                    546:             'the <data> declaration.');
                    547: 	    delete($curves[-1]->{'function'});
                    548: 	}
                    549: 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
                    550: 	$datatext =~ s/\s+/ /g;  
                    551: 	# Need to do some error checking on the @data array - 
                    552: 	# make sure it's all numbers and make sure each array 
                    553: 	# is of the same length.
                    554: 	my @data;
                    555: 	if ($datatext =~ /,/) {
                    556: 	    @data = split /,/,$datatext;
                    557: 	} else { # Assume it's space seperated.
                    558: 	    @data = split / /,$datatext;
                    559: 	}
                    560: 	for (my $i=0;$i<=$#data;$i++) {
                    561: 	    # Check that it's non-empty
1.19      matthew   562: 	    if (! defined($data[$i])) {
                    563: 		&Apache::lonxml::warning(
                    564: 		    'undefined <data> value.  Replacing with '.
                    565: 		    ' pi/e = 1.15572734979092');
                    566: 		$data[$i] = 1.15572734979092;
                    567: 	    }
1.17      matthew   568: 	    # Check that it's a number
1.19      matthew   569: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
                    570: 		&Apache::lonxml::warning(
                    571: 		    'Bad <data> value of '.$data[$i].'  Replacing with '.
                    572: 		    ' pi/e = 1.15572734979092');
                    573: 		$data[$i] = 1.15572734979092;
                    574: 	    }
1.17      matthew   575: 	}
                    576: 	push  @{$curves[-1]->{'data'}},\@data;
1.20      matthew   577:     } elsif ($target eq 'edit') {
                    578:     } elsif ($target eq 'modified') {
1.21    ! matthew   579: 	my $text=$$parser[-1]->get_text("/data");
        !           580: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   581:     }
1.1       matthew   582:     return $result;
                    583: }
                    584: 
                    585: sub end_data {
                    586:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    587:     my $result = '';
1.4       matthew   588:     if ($target eq 'web') {
1.20      matthew   589:     } elsif ($target eq 'edit') {
1.4       matthew   590:     }
1.1       matthew   591:     return $result;
                    592: }
                    593: 
                    594: ##------------------------------------------------------------------- axis
                    595: sub start_axis {
                    596:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    597:     my $result='';
1.4       matthew   598:     if ($target eq 'web') {
1.17      matthew   599: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                    600: 			$tagstack->[-1]);
1.20      matthew   601:     } elsif ($target eq 'edit') {
1.21    ! matthew   602: 	$result .= &Apache::edit::tag_start($target,$token);
        !           603: 	$result .= &edit_attributes($target,$token,\%axis_defaults);
1.20      matthew   604:     } elsif ($target eq 'modified') {
1.4       matthew   605:     }
1.1       matthew   606:     return $result;
                    607: }
                    608: 
                    609: sub end_axis {
                    610:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    611:     my $result = '';
1.4       matthew   612:     if ($target eq 'web') {
1.20      matthew   613:     } elsif ($target eq 'edit') {
1.21    ! matthew   614: 	$result.=&Apache::edit::tag_end($target,$token);
1.20      matthew   615:     } elsif ($target eq 'modified') {
                    616: 	my $constructtag=&Apache::edit::get_new_args
                    617: 	    ($token,$parstack,$safeeval,keys %axis_defaults);
                    618: 	if ($constructtag) {
                    619: 	    $result = &Apache::edit::rebuild_tag($token);
                    620: 	    $result.= &Apache::edit::handle_insert();
                    621: 	}
1.4       matthew   622:     }
1.1       matthew   623:     return $result;
                    624: }
                    625: 
1.21    ! matthew   626: ###################################################################
        !           627: ##                                                               ##
        !           628: ##        Utility Functions                                      ##
        !           629: ##                                                               ##
        !           630: ###################################################################
        !           631: 
1.13      matthew   632: ##----------------------------------------------------------- set_defaults
                    633: sub set_defaults {
1.21    ! matthew   634:     my ($var,$defaults) = @_;
1.13      matthew   635:     my $key;
                    636:     foreach $key (keys %$defaults) {
                    637: 	$var->{$key} = $defaults->{$key}->{'default'};
                    638:     }
                    639: }
                    640: 
1.1       matthew   641: ##------------------------------------------------------------------- misc
1.2       matthew   642: sub get_attributes{
1.21    ! matthew   643:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.19      matthew   644:     foreach my $attr (keys %{$defaults}) {
1.10      matthew   645: 	$values->{$attr} = 
1.15      matthew   646: 	    &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1.10      matthew   647: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew   648: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew   649: 	    next;
                    650: 	}
1.10      matthew   651: 	my $test = $defaults->{$attr}->{'test'};
                    652: 	if (! &$test($values->{$attr})) {
1.6       matthew   653: 	    &Apache::lonxml::warning
                    654: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew   655: 		 .$defaults->{$attr}->{'default'} );
                    656: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew   657: 	}
1.2       matthew   658:     }
1.11      matthew   659:     return ;
1.6       matthew   660: }
1.15      matthew   661: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew   662: sub write_gnuplot_file {
                    663:     my $gnuplot_input = '';
1.10      matthew   664:     my $curve;
1.6       matthew   665:     # Collect all the colors
                    666:     my @Colors;
                    667:     push @Colors, $plot{'bgcolor'};
                    668:     push @Colors, $plot{'fgcolor'}; 
1.13      matthew   669:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9       matthew   670:     foreach $curve (@curves) {
                    671: 	push @Colors, ($curve->{'color'} ne '' ? 
                    672: 		       $curve->{'color'}       : 
1.13      matthew   673: 		       $plot{'fgcolor'}        );
1.6       matthew   674:     }
                    675:     # set term
                    676:     $gnuplot_input .= 'set term gif ';
                    677:     $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
                    678:     $gnuplot_input .= $plot{'font'} . ' ';
1.10      matthew   679:     $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
1.6       matthew   680:     $gnuplot_input .= "@Colors\n";
1.7       matthew   681:     # grid
1.10      matthew   682:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7       matthew   683:     # border
1.9       matthew   684:     $gnuplot_input .= ($plot{'border'} eq 'on'?
                    685: 		       'set border'.$/           :
                    686: 		       'set noborder'.$/         );    # title, xlabel, ylabel
1.13      matthew   687:     $gnuplot_input .= "set output\n";
                    688:     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;
                    689:     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
                    690:     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
                    691:     if (defined(%axis)) {
                    692: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                    693: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6       matthew   694:     }
                    695:     # Key
1.13      matthew   696:     if (defined(%key)) {
1.9       matthew   697: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                    698: 	if ($key{'title'} ne '') {
1.11      matthew   699: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
                    700: 	} 
                    701: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew   702:     } else {
1.9       matthew   703: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew   704:     }
1.6       matthew   705:     # labels
1.10      matthew   706:     my $label;
1.6       matthew   707:     foreach $label (@labels) {
                    708: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.9       matthew   709: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
1.6       matthew   710:     }
                    711:     # curves
                    712:     $gnuplot_input .= 'plot ';
                    713:     my $datatext = '';
1.9       matthew   714:     for (my $i = 0;$i<=$#curves;$i++) {
                    715: 	$curve = $curves[$i];
                    716: 	$gnuplot_input.= ', ' if ($i > 0);
1.6       matthew   717: 	if (exists($curve->{'function'})) {
1.9       matthew   718: 	    $gnuplot_input.= 
                    719: 		$curve->{'function'}.' title "'.
                    720: 		$curve->{'name'}.'" with '.
                    721: 		$curve->{'linestyle'};
1.6       matthew   722: 	} elsif (exists($curve->{'data'})) {
1.9       matthew   723: 	    $gnuplot_input.= '\'-\' title "'.
                    724: 		$curve->{'name'}.'" with '.
                    725: 		$curve->{'linestyle'};
1.6       matthew   726: 	    my @Data = @{$curve->{'data'}};
1.9       matthew   727: 	    my @Data0 = @{$Data[0]};
                    728: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew   729: 		my $dataset;
1.6       matthew   730: 		foreach $dataset (@Data) {
1.9       matthew   731: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew   732: 		}
1.9       matthew   733: 		$datatext .= $/;
1.6       matthew   734: 	    }
1.9       matthew   735: 	    $datatext .=$/;
1.6       matthew   736: 	}
                    737:     }
1.9       matthew   738:     $gnuplot_input .= $/.$datatext;
1.10      matthew   739:     return $gnuplot_input;
1.2       matthew   740: }
1.21    ! matthew   741: 
        !           742: #---------------------------------------------- check_inputs
        !           743: sub check_inputs {
        !           744:     ## Note: no inputs, no outputs - this acts only on global variables.
        !           745:     ## Make sure we have all the input we need:
        !           746:     if (! defined(%plot  )) { &set_defaults(\%plot,\%plot_defaults); }
        !           747:     if (! defined(%key   )) {} # No key for this plot, thats okay
        !           748:     if (! defined(%axis  )) { &set_defaults(\%axis,\%axis_defaults); }
        !           749:     if (! defined($title )) {} # No title for this plot, thats okay
        !           750:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
        !           751:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
        !           752:     if ($#labels < 0) { }      # No labels for this plot, thats okay
        !           753:     if ($#curves < 0) { 
        !           754: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
        !           755: 	return '';
        !           756:     }
        !           757:     my $curve;
        !           758:     foreach $curve (@curves) {
        !           759: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
        !           760: 	    &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
        !           761: 	    return '';
        !           762: 	}
        !           763:     }
        !           764: }
        !           765: 
1.20      matthew   766: #------------------------------------------------ make_edit
                    767: sub edit_attributes {
1.21    ! matthew   768:     my ($target,$token,$defaults) = @_;
1.20      matthew   769:     my $result;
1.21    ! matthew   770:     foreach my $attr (%$defaults) {
1.20      matthew   771: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
                    772: 	    $result .= &Apache::edit::text_arg(
                    773:                  $defaults->{$attr}->{'description'},
                    774: 		 $attr,
                    775: 		 $token);
                    776: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
                    777: 	    $result .= &Apache::edit::select_arg(
                    778: 		 $defaults->{$attr}->{'description'},
                    779: 		 $attr,
                    780: 		 $defaults->{$attr}->{'choices'},
                    781: 		 $token);
                    782: 	}
                    783:     }
                    784:     return $result;
                    785: }
1.1       matthew   786: 
1.21    ! matthew   787: 
        !           788: ###################################################################
        !           789: ##                                                               ##
        !           790: ##           Insertion functions for editing plots               ##
        !           791: ##                                                               ##
        !           792: ###################################################################
        !           793: 
1.20      matthew   794: #------------------------------------------------ insert_xxxxxxx
                    795: sub insert_plot {
                    796:     my $result;
                    797:     #  plot attributes
                    798:     $result .= '<plot ';
                    799:     foreach my $attr (%plot_defaults) {
                    800: 	$result .= '     '.$attr.' "'.$plot_defaults{$attr}->{'default'}.
                    801: 	    "\"\n";
                    802:     }
                    803:     $result .= ">\n";
                    804:     # Add the components
                    805:     $result .= &insert_key();
                    806:     $result .= &insert_axis();
                    807:     $result .= &insert_label();    
                    808:     $result .= &insert_curve();
                    809:     $result .= &insert_function();    
                    810:     $result .= "</curve>\n";
                    811:     $result .= &insert_curve();
                    812:     $result .= &insert_data();    
                    813:     $result .= "</curve>\n";
                    814:     # close up the <plot>
                    815:     $result .= "</plot>\n";
                    816:     return $result;
                    817: }
                    818: 
                    819: sub insert_key {
                    820:     my $result;
                    821:     $result .= '    <key ';
                    822:     foreach my $attr (%key_defaults) {
                    823: 	$result .= '         '.$attr.' "'.$key_defaults{$attr}->{'default'}.
                    824: 	    "\"\n";
                    825:     }
                    826:     $result .= "   />\n";
                    827:     return $result;
                    828: }
                    829: 
                    830: sub insert_axis{
                    831:     my $result;
                    832:     $result .= '    <axis ';
                    833:     foreach my $attr (%axis_defaults) {
                    834: 	$result .= '         '.$attr.' "'.$axis_defaults{$attr}->{'default'}.
                    835: 	    "\"\n";
                    836:     }
                    837:     $result .= "   />\n";
                    838:     return $result;
                    839: }
                    840: 
                    841: sub insert_label {
                    842:     my $result;
                    843:     $result .= '    <label ';
                    844:     foreach my $attr (%label_defaults) {
                    845: 	$result .= '         '.$attr.' "'.
                    846: 	    $label_defaults{$attr}->{'default'}."\"\n";
                    847:     }
                    848:     $result .= "   ></label>\n";
                    849:     return $result;
                    850: }
                    851: 
                    852: sub insert_curve {
                    853:     my $result;
                    854:     $result .= '    <curve ';
                    855:     foreach my $attr (%curve_defaults) {
                    856: 	$result .= '         '.$attr.' "'.
                    857: 	    $curve_defaults{$attr}->{'default'}."\"\n";
                    858:     }
                    859:     $result .= "    >\n";
                    860: }
1.4       matthew   861: 
1.20      matthew   862: sub insert_function {
                    863:     my $result;
                    864:     $result .= "<function></function>\n";
                    865:     return $result;
                    866: }
1.4       matthew   867: 
1.20      matthew   868: sub insert_data {
                    869:     my $result;
                    870:     $result .= "     <data></data>\n";
                    871:     $result .= "     <data></data>\n";
                    872:     return $result;
                    873: }
1.4       matthew   874: 
1.21    ! matthew   875: ##----------------------------------------------------------------------
1.20      matthew   876: 1;
                    877: __END__
1.4       matthew   878: 
                    879: 

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