Annotation of loncom/xml/lonplot.pm, revision 1.3

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.3     ! matthew     4: # $Id: lonplot.pm,v 1.2 2001/12/18 15:33:47 matthew Exp $
1.1       matthew     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: #
1.3     ! matthew    28: # 12/15/01 Matthew
        !            29: # 12/18 Matthew
1.1       matthew    30: package Apache::lonplot;
                     31: use strict;
                     32: use Apache::response;
1.2       matthew    33: use Apache::lonxml;
                     34: use Digest::MD5  qw(md5 md5_hex md5_base64);
1.1       matthew    35: 
                     36: sub BEGIN {
                     37:   &Apache::lonxml::register('Apache::lonplot',('plot'));
                     38: }
                     39: 
                     40: 
                     41: ##
                     42: ## Tests used in checking the validitity of input
                     43: ##
                     44: my $int_test       = sub {$_[0]=~/^\d+$/};
                     45: my $real_test      = sub {$_[0]=~/^[+-]?\d*\.?\d*$/};
                     46: my $color_test     = sub {$_[0]=~/^x[\da-f]{6}$/};
                     47: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
                     48: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below)+$/};
                     49: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
                     50: my $linestyle_test = sub {$_[0]=~/^(lines|linespoints|dots|points|steps)$/};
                     51: 
                     52: ##
                     53: ## Default values for attributes of elements
                     54: ##
                     55: my %plot_defaults = 
                     56:     (
                     57:      height       => {default => 200,       test => $int_test  },
                     58:      width        => {default => 200,       test => $int_test  },
                     59:      bgcolor      => {default => "xffffff", test => $color_test},
                     60:      fgcolor      => {default => "x000000", test => $color_test},
                     61:      transparent  => {default => "off",     test => $onoff_test},
                     62:      grid         => {default => "off",     test => $onoff_test},
                     63:      border       => {default => "on" ,     test => $onoff_test},
                     64:      font         => {default => "medium",  test => $sml_test  }
                     65:      );
                     66: 
                     67: my %key_defaults = 
                     68:     (
                     69:      title => { default => "on" ,        test => $onoff_test  },
                     70:      box   => { default => "off" ,       test => $onoff_test  },
                     71:      pos   => { default => "top right" , test => $key_pos_test}
                     72:      );
                     73: 
                     74: my %label_defaults = 
                     75:     (
                     76:      xpos    => {default => 0,         test => $real_test                   },
                     77:      ypos    => {default => 0,         test => $real_test                   },
                     78:      color   => {default => "x000000", test => $color_test                  },
                     79:      justify => {default => "left",    
                     80:                  test => sub {$_[0]=~/^(left|right|center)$/}}
                     81:      );
                     82: 
                     83: my %axis_defaults = 
                     84:     (
                     85:      color     => {default => "x000000", test => $color_test},
                     86:      thickness => {default => 1,         test => $int_test  },
                     87:      xmin      => {default => -10.0,     test => $real_test },
                     88:      xmax      => {default =>  10.0,     test => $real_test },
                     89:      ymin      => {default => -10.0,     test => $real_test },
                     90:      ymax      => {default =>  10.0,     test => $real_test }
                     91:      );
                     92: 
                     93: my %curve_defaults = 
                     94:     (
                     95:      color     => {default => "x000000", test => $color_test      },
                     96:      name      => {default => "x000000", test => sub {$_[0]=~/^[\w ]*$/} },
                     97:      linestyle => {default => "lines",   test => $linestyle_test  }
                     98:      );
                     99: 
                    100: ##
                    101: ## End of defaults
                    102: ##
                    103: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
                    104: 
                    105: sub start_plot {
                    106:     %plot = '';   %key='';    %axis=''; 
                    107:     $title='';    $xlabel=''; $ylabel='';
                    108:     @labels = ''; @curves='';
1.2       matthew   109: 
1.1       matthew   110:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    111:     my $result='';
                    112:     &Apache::lonxml::register('Apache::plot',
                    113: 	     ('title','xlabel','ylabel','key','axis','label','curve'));
                    114:     push (@Apache::lonxml::namespace,'plot');
1.2       matthew   115:     ##-------------------------------------------------------
                    116:     ## How do I do this?  I need to "eval" and I need to keep the info
                    117:     ## available for the parser.
                    118:     ##
                    119:     my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
                    120:     my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
                    121:     if ($eval eq 'on') {
                    122: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    123: 	#&Apache::lonxml::debug("M is evaulated to:$inside:");
                    124:     }
                    125:     ##-------------------------------------------------------
                    126:     &get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,'plot');
1.1       matthew   127:     return '';
                    128: }
                    129: 
                    130: sub end_plot {
                    131:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    132:     pop @Apache::lonxml::namespace;
                    133:     my $result;
                    134:     ## Determine filename
1.2       matthew   135:     my $tmpdir = '/home/httpd/perl/tmp/';
                    136:     my $filename = $tmpdir.$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
                    137: 	'_plot.data';
                    138:     my $usersees=md5_base64($filename.'_'.$ENV{'REMOTE_ADDR'});
                    139:     
1.1       matthew   140:     ## Write the plot description to the file
1.2       matthew   141:     my $fh=&Apache::File->new('/home/httpd/perl/tmp/'.$realname);
                    142:     ## Ack! 
1.1       matthew   143:     ## return image tag for the plot
1.2       matthew   144:     $result = '<img src=\"/cgi-bin/plot.cgi?'.$usersees.'"';
1.1       matthew   145:     return $result;
                    146: }
1.2       matthew   147: 
1.1       matthew   148: ##----------------------------------------------------------------- key
                    149: sub start_key {
                    150:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    151:     my $result='';
1.3     ! matthew   152:     &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,$tagstack);
1.1       matthew   153:     return $result;
                    154: }
                    155: 
                    156: sub end_key {
                    157:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    158:     my $result = '';
                    159:     return $result;
                    160: }
                    161: ##------------------------------------------------------------------- title
                    162: sub start_title {
                    163:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    164:     $title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
                    165:     my $result='';
                    166:     return $result;
                    167: }
                    168: 
                    169: sub end_title {
                    170:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    171:     my $result = '';
                    172:     return $result;
                    173: }
                    174: ##------------------------------------------------------------------- xlabel
                    175: sub start_xlabel {
                    176:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    177:     my $result='';
                    178:     $xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
                    179:     return $result;
                    180: }
                    181: 
                    182: sub end_xlabel {
                    183:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    184:     my $result = '';
                    185:     return $result;
                    186: }
                    187: ##------------------------------------------------------------------- ylabel
                    188: sub start_ylabel {
                    189:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    190:     my $result='';
                    191:     $ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
                    192:     return $result;
                    193: }
                    194: 
                    195: sub end_ylabel {
                    196:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    197:     my $result = '';
                    198:     return $result;
                    199: }
                    200: ##------------------------------------------------------------------- label
                    201: sub start_label {
                    202:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    203:     my $result='';
1.3     ! matthew   204:     my %label;
        !           205:     &get_attributes($label,\%label_defaults,$parstack,$safeeval,$tagstack);
1.1       matthew   206:     $label->{'text'} = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
1.3     ! matthew   207:     push(@labels,\%label);
1.1       matthew   208:     return $result;
                    209: }
                    210: 
                    211: sub end_label {
                    212:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    213:     my $result = '';
                    214:     return $result;
                    215: }
                    216: 
                    217: ##------------------------------------------------------------------- curve
                    218: sub start_curve {
                    219:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    220:     my $result='';
1.3     ! matthew   221:     my %curve;
        !           222:     &get_attributes($curve,\%curve_defaults,$parstack,$safeeval,$tagstack);
1.1       matthew   223:     push (@curves,$curve);
                    224:     
                    225:     &Apache::lonxml::register('Apache::plot',('function','data'));
                    226:     push (@Apache::lonxml::namespace,'curve');
                    227:     
                    228:     return $result;
                    229: }
                    230: 
                    231: sub end_curve {
                    232:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    233:     my $result = '';
                    234:     return $result;
                    235: }
                    236: 
                    237: ##------------------------------------------------------------ curve function
                    238: sub start_function {
                    239:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    240:     my $result='';
                    241: 
                    242:     $curves[-1]->{'function'} = 
                    243: 	&Apache::lonxml::get_all_text("/function",$$parser[-1]);
                    244:     return $result;
                    245: }
                    246: 
                    247: sub end_function {
                    248:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    249:     my $result = '';
                    250:     return $result;
                    251: }
                    252: 
                    253: ##------------------------------------------------------------ curve  data
                    254: sub start_data {
                    255:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    256:     my $result='';
                    257:     push( @{$curves[-1]->{'data'}},
                    258: 	  &Apache::lonxml::get_all_text("/data",$$parser[-1]));
                    259:     return $result;
                    260: }
                    261: 
                    262: sub end_data {
                    263:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    264:     my $result = '';
                    265:     return $result;
                    266: }
                    267: 
                    268: ##------------------------------------------------------------------- axis
                    269: sub start_axis {
                    270:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    271:     my $result='';
1.3     ! matthew   272:     &get_attributes(\%axis,\%label_defaults,$parstack,$safeeval,$tagstack);
1.1       matthew   273:     return $result;
                    274: }
                    275: 
                    276: sub end_axis {
                    277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    278:     my $result = '';
                    279:     return $result;
                    280: }
                    281: 
                    282: ##------------------------------------------------------------------- misc
1.2       matthew   283: sub get_attributes{
                    284:     %values   = %{shift};
                    285:     %defaults = %{shift};
                    286:     $parstack = shift;
                    287:     $safeeval = shift;
                    288:     $tag      = shift;
                    289:     my $attr;
                    290:     foreach $attr (keys %defaults) {
                    291: 	$values{$attr} = &Apache::lonxml::get_param($attr,$parstack,$safeeval);
                    292: 	my $test = $defaults{$attr}->{'test'};
                    293: 	if (! &$test($values{$attr})) {
                    294: 	    &Apache::lonxml::warning($tag.':'.$attr.': Bad value.  Replacing your value with : '.$defaults{$attr});
                    295: 	    $values{$attr} = $defaults{$attr};
                    296:     }
                    297:     return ;
                    298: }
1.1       matthew   299: 
                    300: 1;
                    301: __END__

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