File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.29: download - view: text, annotated - select for diffs
Tue Jan 1 19:34:25 2002 UTC (22 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Multiple changes to enable target eq 'modified' and track down insert
errors

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

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