File:  [LON-CAPA] / loncom / xml / lonplot.pm
Revision 1.20: download - view: text, annotated - select for diffs
Thu Dec 27 22:30:01 2001 UTC (22 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Added more metadata for the tags, began adding 'edit' and 'modified' handling.

    1: # The LearningOnline Network with CAPA
    2: # Dynamic plot
    3: #
    4: # $Id: lonplot.pm,v 1.20 2001/12/27 22:30:01 matthew Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 12/15/01 Matthew
   29: # 12/17 12/18 12/19 12/20 12/21 12/27 Matthew
   30: package Apache::lonplot;
   31: 
   32: use strict;
   33: use Apache::File;
   34: use Apache::response;
   35: use Apache::lonxml;
   36: use Apache::edit;
   37: 
   38: sub BEGIN {
   39:   &Apache::lonxml::register('Apache::lonplot',('plot'));
   40: }
   41: 
   42: ## 
   43: ## Description of data structures:
   44: ##
   45: ##  %plot       %key    %axis
   46: ## --------------------------
   47: ##  height      title   color
   48: ##  width       box     xmin
   49: ##  bgcolor     pos     xmax
   50: ##  fgcolor             ymin
   51: ##  transparent         ymax
   52: ##  grid
   53: ##  border
   54: ##  font
   55: ##  align
   56: ##
   57: ##  @labels: $labels[$i] = \%label
   58: ##           %label: text, xpos, ypos, justify
   59: ##
   60: ##  @curves: $curves[$i] = \%curve
   61: ##           %curve: name, linestyle, ( function | data )
   62: ##
   63: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
   64: ##                            [y1,y2,y3,y4] ]
   65: ##
   66: ##------------------------------------------------------------
   67: ##
   68: ## Tests used in checking the validitity of input
   69: ##
   70: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
   71: my $real_test      = 
   72:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
   73: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
   74: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
   75: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
   76: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
   77: my $linestyle_test = sub {$_[0]=~/^(lines|linespoints|dots|points|steps)$/};
   78: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
   79: ##
   80: ## Attribute metadata
   81: ##
   82: my %plot_defaults = 
   83:     (
   84:      height       => {
   85: 	 default     => 200,
   86: 	 test        => $int_test,
   87: 	 description => 'vertical size of image (pixels)',
   88: 	 edit_type   => 'entry' 
   89: 	 },
   90:      width        => {
   91: 	 default     => 200,
   92: 	 test        => $int_test,
   93: 	 description => 'horizontal size of image (pixels)',
   94: 	 edit_type   => 'entry'
   95: 	 },
   96:      bgcolor      => {
   97: 	 default     => 'xffffff',
   98: 	 test        => $color_test, 
   99: 	 description => 'background color of image (xffffff)',
  100: 	 edit_type   => 'entry'
  101: 	 },
  102:      fgcolor      => {
  103: 	 default     => 'x000000',
  104: 	 test        => $color_test,
  105: 	 description => 'foreground color of image (x000000)',
  106: 	 edit_type   => 'entry' 
  107: 	 },
  108:      transparent  => {
  109: 	 default     => 'off',
  110: 	 test        => $onoff_test, 
  111: 	 description => '',
  112: 	 edit_type   => 'on_off'
  113: 	 },
  114:      grid         => {
  115: 	 default     => 'off',
  116: 	 test        => $onoff_test, 
  117: 	 description => '',
  118: 	 edit_type   => 'on_off'
  119: 	 },
  120:      border       => {
  121: 	 default     => 'on',
  122: 	 test        => $onoff_test, 
  123: 	 description => '',
  124: 	 edit_type   => 'on_off'
  125: 	 },
  126:      font         => {
  127: 	 default     => 'medium',
  128: 	 test        => $sml_test,
  129: 	 description => 'Size of font to use',
  130: 	 edit_type   => 'choice',
  131: 	 choices     => ['small','medium','large']
  132: 	 },
  133:      align        => {
  134: 	 default     => 'left',
  135: 	 test        => sub {$_[0]=~/^(left|right|center)$/},
  136: 	 description => 'alignment for image in html',
  137: 	 edit_type   => 'choice',
  138: 	 choices     => ['left','right','center']
  139: 	 } 
  140:      );
  141: 
  142: my %key_defaults = 
  143:     (
  144:      title => { 
  145: 	 default => '',
  146: 	 test => $words_test,
  147: 	 description => 'Title of key',
  148: 	 edit_type   => 'entry'
  149: 	 },
  150:      box   => { 
  151: 	 default => 'off',
  152: 	 test => $onoff_test,
  153: 	 description => 'Draw a box around the key?',
  154: 	 edit_type   => 'on_off'
  155: 	 },
  156:      pos   => { 
  157: 	 default => 'top right', 
  158: 	 test => $key_pos_test, 
  159: 	 description => 'position of the key on the plot',
  160: 	 edit_type   => 'choice',
  161: 	 choices     => ['top left','top right','bottom left','bottom right',
  162: 			 'outside','below']
  163: 	 }
  164:      );
  165: 
  166: my %label_defaults = 
  167:     (
  168:      xpos    => {
  169: 	 default => 0,
  170: 	 test => $real_test,
  171: 	 description => 'x position of label (graph coordinates)',
  172: 	 edit_type   => 'entry'
  173: 	 },
  174:      ypos    => {
  175: 	 default => 0, 
  176: 	 test => $real_test,
  177: 	 description => 'y position of label (graph coordinates)',
  178: 	 edit_type   => 'entry'
  179: 	 },
  180:      justify => {
  181: 	 default => 'left',    
  182: 	 test => sub {$_[0]=~/^(left|right|center)$/},
  183: 	 description => 'justification of the label text on the plot',
  184: 	 edit_type   => 'choice',
  185: 	 choices     => ['left','right','center']
  186:      }
  187:      );
  188: 
  189: my %axis_defaults = 
  190:     (
  191:      color     => {
  192: 	 default => 'x000000', 
  193: 	 test => $color_test,
  194: 	 description => 'color of axes (x000000)',
  195: 	 edit_type   => 'entry'
  196: 	 },
  197:      xmin      => {
  198: 	 default => '-10.0',
  199: 	 test => $real_test,
  200: 	 description => 'minimum x-value shown in plot',
  201: 	 edit_type   => 'entry'
  202: 	 },
  203:      xmax      => {
  204: 	 default => ' 10.0',
  205: 	 test => $real_test,
  206: 	 description => 'maximum x-value shown in plot',	 
  207: 	 edit_type   => 'entry'
  208: 	 },
  209:      ymin      => {
  210: 	 default => '-10.0',
  211: 	 test => $real_test,
  212: 	 description => 'minimum y-value shown in plot',	 
  213: 	 edit_type   => 'entry'
  214: 	 },
  215:      ymax      => {
  216: 	 default => ' 10.0',
  217: 	 test => $real_test,
  218: 	 description => 'maximum y-value shown in plot',	 
  219: 	 edit_type   => 'entry'
  220: 	 },
  221:      linestyle => {
  222: 	 default => 'points',
  223: 	 test => $linestyle_test,
  224: 	 description => 'Style of the axis lines',
  225: 	 edit_type   => 'choice',
  226: 	 choices     => ['lines','linespoints','dots','points']
  227: 	 }
  228:      );
  229: 
  230: my %curve_defaults = 
  231:     (
  232:      color     => {
  233: 	 default => 'x000000',
  234: 	 test => $color_test,
  235: 	 description => 'color of curve (x000000)',
  236: 	 edit_type   => 'entry'
  237: 	 },
  238:      name      => {
  239: 	 default => '',
  240: 	 test => $words_test,
  241: 	 description => 'name of curve to appear in key',
  242: 	 edit_type   => 'entry'
  243: 	 },
  244:      linestyle => {
  245: 	 default => 'lines',
  246: 	 test => $linestyle_test,
  247: 	 description => 'Style of the axis lines',
  248: 	 edit_type   => 'choice',
  249: 	 choices     => ['lines','linespoints','dots','points','steps']
  250: 	 }
  251:      );
  252: 
  253: ##
  254: ## End of defaults
  255: ##
  256: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
  257: 
  258: sub start_plot {
  259:     %plot    = undef;   %key     = undef;   %axis   = undef; 
  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:     if ($target eq 'web') {
  266: 	&Apache::lonxml::register('Apache::lonplot',
  267: 	      ('title','xlabel','ylabel','key','axis','label','curve'));
  268: 	push (@Apache::lonxml::namespace,'plot');
  269: 	## Always evaluate the insides of the <plot></plot> tags
  270: 	my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
  271: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
  272: 	&Apache::lonxml::newparser($parser,\$inside);
  273: 	##-------------------------------------------------------
  274: 	&get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,
  275: 			$tagstack->[-1]);
  276:     } elsif ($target eq 'edit') {
  277:     } elsif ($target eq 'modified') {
  278: 	my $constructtag=&Apache::edit::get_new_args
  279: 	    ($token,$parstack,$safeeval,keys %plot_defaults);
  280: 	if ($constructtag) {
  281: 	    $result = &Apache::edit::rebuild_tag($token);
  282: 	    $result.= &Apache::edit::handle_insert();
  283: 	}
  284:     }
  285:     return '';
  286: }
  287: 
  288: sub end_plot {
  289:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  290:     pop @Apache::lonxml::namespace;
  291:     &Apache::lonxml::deregister('Apache::lonplot',
  292: 	('title','xlabel','ylabel','key','axis','label','curve'));
  293:     my $result = '';
  294:     if ($target eq 'web') {
  295: 	##
  296: 	## Make sure we have all the input we need:
  297: 	if (! defined(%plot  )) { &set_defaults(\%plot,\%plot_defaults); }
  298: 	if (! defined(%key   )) {} # No key for this plot
  299: 	if (! defined(%axis  )) { &set_defaults(\%axis,\%axis_defaults); }
  300: 	if (! defined($title )) {} # No title for this plot
  301: 	if (! defined($xlabel)) {} # No xlabel for this plot
  302: 	if (! defined($ylabel)) {} # No ylabel for this plot
  303: 	if ($#labels < 0) { } # No labels for this plot
  304: 	if ($#curves < 0) { 
  305: 	    &Apache::lonxml::warning("No curves specified for plot!!!!");
  306: 	    return '';
  307: 	}
  308: 	my $curve;
  309: 	foreach $curve (@curves) {
  310: 	    if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
  311: 		&Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
  312: 		return '';
  313: 	    }
  314: 	}
  315: 	##
  316: 	## Determine filename
  317: 	my $tmpdir = '/home/httpd/perl/tmp/';
  318: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
  319: 	    '_'.time.'_'.$$.'_plot.data';
  320: 	## Write the plot description to the file
  321: 	my $fh=Apache::File->new(">$tmpdir$filename");
  322: 	print $fh &write_gnuplot_file();
  323: 	close($fh);
  324: 	## return image tag for the plot
  325: 	$result .= <<"ENDIMAGE";
  326: <img src    = "/cgi-bin/plot.gif?$filename" 
  327:      width  = "$plot{'width'}" 
  328:      height = "$plot{'height'}"
  329:      align  = "$plot{'align'}"
  330:      alt    = "/cgi-bin/plot.gif?$filename" />
  331: ENDIMAGE
  332:     } elsif ($target eq 'edit') {
  333:     } elsif ($target eq 'modified') {
  334:     }
  335:     return $result;
  336: }
  337: 
  338: ##----------------------------------------------------------------- key
  339: sub start_key {
  340:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  341:     my $result='';
  342:     if ($target eq 'web') {
  343: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
  344: 		    $tagstack->[-1]);
  345:     } elsif ($target eq 'edit') {
  346:     } elsif ($target eq 'modified') {
  347: 	my $constructtag=&Apache::edit::get_new_args
  348: 	    ($token,$parstack,$safeeval,keys %key_defaults);
  349: 	if ($constructtag) {
  350: 	    $result = &Apache::edit::rebuild_tag($token);
  351: 	    $result.= &Apache::edit::handle_insert();
  352: 	}
  353:     }
  354:     return $result;
  355: }
  356: 
  357: sub end_key {
  358:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  359:     my $result = '';
  360:     if ($target eq 'web') {
  361:     } elsif ($target eq 'edit') {
  362:     } elsif ($target eq 'modified') {
  363:     }
  364:     return $result;
  365: }
  366: ##------------------------------------------------------------------- title
  367: sub start_title {
  368:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  369:     my $result='';
  370:     if ($target eq 'web') {
  371: 	$title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
  372:     } elsif ($target eq 'edit') {
  373:     } elsif ($target eq 'modified') {
  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:     } elsif ($target eq 'modified') {
  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:     } elsif ($target eq 'modified') {
  395:     }
  396:     return $result;
  397: }
  398: 
  399: sub end_xlabel {
  400:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  401:     my $result = '';
  402:     if ($target eq 'web') {
  403:     } elsif ($target eq 'edit') {
  404:     } elsif ($target eq 'modified') {
  405:     }
  406:     return $result;
  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:     } elsif ($target eq 'modified') {
  416:     }
  417:     return $result;
  418: }
  419: 
  420: sub end_ylabel {
  421:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  422:     my $result = '';
  423:     if ($target eq 'web') {
  424:     } elsif ($target eq 'edit') {
  425:     } elsif ($target eq 'modified') {
  426:     }
  427:     return $result;
  428: }
  429: ##------------------------------------------------------------------- label
  430: sub start_label {
  431:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  432:     my $result='';
  433:     if ($target eq 'web') {
  434: 	my %label;
  435: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
  436: 		    $tagstack->[-1]);
  437: 	$label{'text'} = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
  438: 	if (! &$words_test($label{'text'})) {
  439: 	    # I should probably warn about it, too.
  440: 	    $label{'text'} = 'Illegal text';
  441: 	}
  442: 	push(@labels,\%label);
  443:     } elsif ($target eq 'edit') {
  444:     } elsif ($target eq 'modified') {
  445: 	my $constructtag=&Apache::edit::get_new_args
  446: 	    ($token,$parstack,$safeeval,keys %label_defaults);
  447: 	if ($constructtag) {
  448: 	    $result = &Apache::edit::rebuild_tag($token);
  449: 	    $result.= &Apache::edit::handle_insert();
  450: 	}
  451:     }
  452:     return $result;
  453: }
  454: 
  455: sub end_label {
  456:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  457:     my $result = '';
  458:     if ($target eq 'web') {
  459:     } elsif ($target eq 'edit') {
  460:     } elsif ($target eq 'modified') {
  461:     }
  462:     return $result;
  463: }
  464: 
  465: ##------------------------------------------------------------------- curve
  466: sub start_curve {
  467:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  468:     my $result='';
  469:     if ($target eq 'web') {
  470: 	my %curve;
  471: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
  472: 		    $tagstack->[-1]);
  473: 	push (@curves,\%curve);
  474: 	&Apache::lonxml::register('Apache::lonplot',('function','data'));
  475: 	push (@Apache::lonxml::namespace,'curve');
  476:     } elsif ($target eq 'edit') {
  477:     } elsif ($target eq 'modified') {
  478: 	my $constructtag=&Apache::edit::get_new_args
  479: 	    ($token,$parstack,$safeeval,keys %label_defaults);
  480: 	if ($constructtag) {
  481: 	    $result = &Apache::edit::rebuild_tag($token);
  482: 	    $result.= &Apache::edit::handle_insert();
  483: 	}
  484:     }
  485:     return $result;
  486: }
  487: 
  488: sub end_curve {
  489:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  490:     my $result = '';
  491:     if ($target eq 'web') {
  492: 	pop @Apache::lonxml::namespace;
  493: 	&Apache::lonxml::deregister('Apache::lonplot',('function','data'));
  494:     } elsif ($target eq 'edit') {
  495:     } elsif ($target eq 'modified') {
  496:     }
  497:     return $result;
  498: }
  499: ##------------------------------------------------------------ curve function
  500: sub start_function {
  501:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  502:     my $result='';
  503:     if ($target eq 'web') {
  504: 	if (exists($curves[-1]->{'data'})) {
  505: 	    &Apache::lonxml::warning('Use of <function> precludes use of <data>.  The <data> will be omitted in favor of the <function> declaration.');
  506: 	    delete $curves[-1]->{'data'} ;
  507: 	}
  508: 	$curves[-1]->{'function'} = 
  509: 	    &Apache::lonxml::get_all_text("/function",$$parser[-1]);
  510:     } elsif ($target eq 'edit') {
  511: 	$result.=&Apache::edit::tag_start($target,$token);
  512: 	my $text=&Apache::lonxml::get_all_text("/function",$$parser[-1]);
  513: 	$result.='</td></tr><tr><td colspan="3">'.
  514: 	    &Apache::edit::editfield('',$text,'',20,1).
  515: 	    &Apache::edit::end_table();
  516:     } elsif ($target eq 'modified') {
  517: 	# Why do I do this?
  518: 	my $text=$$parser[-1]->get_text("/function");
  519: 	$result.=&Apache::edit::modifiedfield($token);
  520:     }
  521:     return $result;
  522: }
  523: 
  524: sub end_function {
  525:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  526:     my $result = '';
  527:     if ($target eq 'web') {
  528:     } elsif ($target eq 'edit') {
  529:     } elsif ($target eq 'modified') {
  530:     }
  531:     return $result;
  532: }
  533: ##------------------------------------------------------------ curve  data
  534: sub start_data {
  535:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  536:     my $result='';
  537:     if ($target eq 'web') {
  538: 	if (exists($curves[-1]->{'function'})) {
  539: 	    &Apache::lonxml::warning('Use of <data> precludes use of .'.
  540: 	    '<function>.  The <function> will be omitted in favor of '.
  541:             'the <data> declaration.');
  542: 	    delete($curves[-1]->{'function'});
  543: 	}
  544: 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
  545: 	$datatext =~ s/\s+/ /g;  
  546: 	# Need to do some error checking on the @data array - 
  547: 	# make sure it's all numbers and make sure each array 
  548: 	# is of the same length.
  549: 	my @data;
  550: 	if ($datatext =~ /,/) {
  551: 	    @data = split /,/,$datatext;
  552: 	} else { # Assume it's space seperated.
  553: 	    @data = split / /,$datatext;
  554: 	}
  555: 	for (my $i=0;$i<=$#data;$i++) {
  556: 	    # Check that it's non-empty
  557: 	    if (! defined($data[$i])) {
  558: 		&Apache::lonxml::warning(
  559: 		    'undefined <data> value.  Replacing with '.
  560: 		    ' pi/e = 1.15572734979092');
  561: 		$data[$i] = 1.15572734979092;
  562: 	    }
  563: 	    # Check that it's a number
  564: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
  565: 		&Apache::lonxml::warning(
  566: 		    'Bad <data> value of '.$data[$i].'  Replacing with '.
  567: 		    ' pi/e = 1.15572734979092');
  568: 		$data[$i] = 1.15572734979092;
  569: 	    }
  570: 	}
  571: 	push  @{$curves[-1]->{'data'}},\@data;
  572:     } elsif ($target eq 'edit') {
  573:     } elsif ($target eq 'modified') {
  574:     }
  575:     return $result;
  576: }
  577: 
  578: sub end_data {
  579:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  580:     my $result = '';
  581:     if ($target eq 'web') {
  582:     } elsif ($target eq 'edit') {
  583:     } elsif ($target eq 'modified') {
  584:     }
  585:     return $result;
  586: }
  587: 
  588: ##------------------------------------------------------------------- axis
  589: sub start_axis {
  590:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  591:     my $result='';
  592:     if ($target eq 'web') {
  593: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
  594: 			$tagstack->[-1]);
  595:     } elsif ($target eq 'edit') {
  596:     } elsif ($target eq 'modified') {
  597:     }
  598:     return $result;
  599: }
  600: 
  601: sub end_axis {
  602:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  603:     my $result = '';
  604:     if ($target eq 'web') {
  605:     } elsif ($target eq 'edit') {
  606:     } elsif ($target eq 'modified') {
  607: 	my $constructtag=&Apache::edit::get_new_args
  608: 	    ($token,$parstack,$safeeval,keys %axis_defaults);
  609: 	if ($constructtag) {
  610: 	    $result = &Apache::edit::rebuild_tag($token);
  611: 	    $result.= &Apache::edit::handle_insert();
  612: 	}
  613:     }
  614:     return $result;
  615: }
  616: 
  617: ##----------------------------------------------------------- set_defaults
  618: sub set_defaults {
  619:     my $var      = shift;
  620:     my $defaults = shift;
  621:     my $key;
  622:     foreach $key (keys %$defaults) {
  623: 	$var->{$key} = $defaults->{$key}->{'default'};
  624:     }
  625: }
  626: 
  627: ##------------------------------------------------------------------- misc
  628: sub get_attributes{
  629:     my $values   = shift;
  630:     my $defaults = shift;
  631:     my $parstack = shift;
  632:     my $safeeval = shift;
  633:     my $tag      = shift;
  634:     foreach my $attr (keys %{$defaults}) {
  635: 	$values->{$attr} = 
  636: 	    &Apache::lonxml::get_param($attr,$parstack,$safeeval);
  637: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
  638: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
  639: 	    next;
  640: 	}
  641: 	my $test = $defaults->{$attr}->{'test'};
  642: 	if (! &$test($values->{$attr})) {
  643: 	    &Apache::lonxml::warning
  644: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
  645: 		 .$defaults->{$attr}->{'default'} );
  646: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
  647: 	}
  648:     }
  649:     return ;
  650: }
  651: ##------------------------------------------------------- write_gnuplot_file
  652: sub write_gnuplot_file {
  653:     my $gnuplot_input = '';
  654:     my $curve;
  655:     # Collect all the colors
  656:     my @Colors;
  657:     push @Colors, $plot{'bgcolor'};
  658:     push @Colors, $plot{'fgcolor'}; 
  659:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
  660:     foreach $curve (@curves) {
  661: 	push @Colors, ($curve->{'color'} ne '' ? 
  662: 		       $curve->{'color'}       : 
  663: 		       $plot{'fgcolor'}        );
  664:     }
  665:     # set term
  666:     $gnuplot_input .= 'set term gif ';
  667:     $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
  668:     $gnuplot_input .= $plot{'font'} . ' ';
  669:     $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
  670:     $gnuplot_input .= "@Colors\n";
  671:     # grid
  672:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
  673:     # border
  674:     $gnuplot_input .= ($plot{'border'} eq 'on'?
  675: 		       'set border'.$/           :
  676: 		       'set noborder'.$/         );    # title, xlabel, ylabel
  677:     $gnuplot_input .= "set output\n";
  678:     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;
  679:     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
  680:     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
  681:     if (defined(%axis)) {
  682: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
  683: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
  684:     }
  685:     # Key
  686:     if (defined(%key)) {
  687: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
  688: 	if ($key{'title'} ne '') {
  689: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
  690: 	} 
  691: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
  692:     } else {
  693: 	$gnuplot_input .= 'set nokey'.$/;
  694:     }
  695:     # labels
  696:     my $label;
  697:     foreach $label (@labels) {
  698: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
  699: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
  700:     }
  701:     # curves
  702:     $gnuplot_input .= 'plot ';
  703:     my $datatext = '';
  704:     for (my $i = 0;$i<=$#curves;$i++) {
  705: 	$curve = $curves[$i];
  706: 	$gnuplot_input.= ', ' if ($i > 0);
  707: 	if (exists($curve->{'function'})) {
  708: 	    $gnuplot_input.= 
  709: 		$curve->{'function'}.' title "'.
  710: 		$curve->{'name'}.'" with '.
  711: 		$curve->{'linestyle'};
  712: 	} elsif (exists($curve->{'data'})) {
  713: 	    $gnuplot_input.= '\'-\' title "'.
  714: 		$curve->{'name'}.'" with '.
  715: 		$curve->{'linestyle'};
  716: 	    my @Data = @{$curve->{'data'}};
  717: 	    my @Data0 = @{$Data[0]};
  718: 	    for (my $i =0; $i<=$#Data0; $i++) {
  719: 		my $dataset;
  720: 		foreach $dataset (@Data) {
  721: 		    $datatext .= $dataset->[$i] . ' ';
  722: 		}
  723: 		$datatext .= $/;
  724: 	    }
  725: 	    $datatext .=$/;
  726: 	}
  727:     }
  728:     $gnuplot_input .= $/.$datatext;
  729:     return $gnuplot_input;
  730: }
  731: #------------------------------------------------ make_edit
  732: sub edit_attributes {
  733:     my $target    = shift;
  734:     my $token     = shift;
  735:     my $defaults  = shift;
  736:     my $result;
  737:     foreach my $attr (%{$token->[2]}) {
  738: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
  739: 	    $result .= &Apache::edit::text_arg(
  740:                  $defaults->{$attr}->{'description'},
  741: 		 $attr,
  742: 		 $token);
  743: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
  744: 	    $result .= &Apache::edit::select_arg(
  745: 		 $defaults->{$attr}->{'description'},
  746: 		 $attr,
  747: 		 $defaults->{$attr}->{'choices'},
  748: 		 $token);
  749: 	}
  750:     }
  751:     return $result;
  752: }
  753: 
  754: #------------------------------------------------ insert_xxxxxxx
  755: sub insert_plot {
  756:     my $result;
  757:     #  plot attributes
  758:     $result .= '<plot ';
  759:     foreach my $attr (%plot_defaults) {
  760: 	$result .= '     '.$attr.' "'.$plot_defaults{$attr}->{'default'}.
  761: 	    "\"\n";
  762:     }
  763:     $result .= ">\n";
  764:     # Add the components
  765:     $result .= &insert_key();
  766:     $result .= &insert_axis();
  767:     $result .= &insert_label();    
  768:     $result .= &insert_curve();
  769:     $result .= &insert_function();    
  770:     $result .= "</curve>\n";
  771:     $result .= &insert_curve();
  772:     $result .= &insert_data();    
  773:     $result .= "</curve>\n";
  774:     # close up the <plot>
  775:     $result .= "</plot>\n";
  776:     return $result;
  777: }
  778: 
  779: sub insert_key {
  780:     my $result;
  781:     $result .= '    <key ';
  782:     foreach my $attr (%key_defaults) {
  783: 	$result .= '         '.$attr.' "'.$key_defaults{$attr}->{'default'}.
  784: 	    "\"\n";
  785:     }
  786:     $result .= "   />\n";
  787:     return $result;
  788: }
  789: 
  790: sub insert_axis{
  791:     my $result;
  792:     $result .= '    <axis ';
  793:     foreach my $attr (%axis_defaults) {
  794: 	$result .= '         '.$attr.' "'.$axis_defaults{$attr}->{'default'}.
  795: 	    "\"\n";
  796:     }
  797:     $result .= "   />\n";
  798:     return $result;
  799: }
  800: 
  801: sub insert_label {
  802:     my $result;
  803:     $result .= '    <label ';
  804:     foreach my $attr (%label_defaults) {
  805: 	$result .= '         '.$attr.' "'.
  806: 	    $label_defaults{$attr}->{'default'}."\"\n";
  807:     }
  808:     $result .= "   ></label>\n";
  809:     return $result;
  810: }
  811: 
  812: sub insert_curve {
  813:     my $result;
  814:     $result .= '    <curve ';
  815:     foreach my $attr (%curve_defaults) {
  816: 	$result .= '         '.$attr.' "'.
  817: 	    $curve_defaults{$attr}->{'default'}."\"\n";
  818:     }
  819:     $result .= "    >\n";
  820: }
  821: 
  822: sub insert_function {
  823:     my $result;
  824:     $result .= "<function></function>\n";
  825:     return $result;
  826: }
  827: 
  828: sub insert_data {
  829:     my $result;
  830:     $result .= "     <data></data>\n";
  831:     $result .= "     <data></data>\n";
  832:     return $result;
  833: }
  834: 
  835: 1;
  836: __END__
  837: 
  838: 

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