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

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

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