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

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

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