File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.21: download - view: text, annotated - select for diffs
Fri Dec 28 15:49:38 2001 UTC (22 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Further $target eq 'edit' | 'modified' handling (still incomplete).
Some organizational changes in the code (mostly comments).

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

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