File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.22: download - view: text, annotated - select for diffs
Fri Dec 28 19:04:56 2001 UTC (22 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Further $target eq 'edit' handling code added.  Still untested and likely
broken.

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

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