File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.33: download - view: text, annotated - select for diffs
Sun Jan 6 02:19:25 2002 UTC (22 years, 4 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
sub BEGIN should be BEGIN

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

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