File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.28: download - view: text, annotated - select for diffs
Mon Dec 31 19:02:00 2001 UTC (22 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Removed axis{'linestyle'} data because it was unused.

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

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