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

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

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