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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.47    ! matthew     4: # $Id: lonplot.pm,v 1.46 2002/01/30 13:27:51 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
1.35      matthew    31: # 01/02 01/03 01/04 01/07 01/08 01/09 Matthew
1.41      matthew    32: # 01/21 Matthew
1.35      matthew    33: 
1.1       matthew    34: package Apache::lonplot;
1.10      matthew    35: 
1.1       matthew    36: use strict;
1.10      matthew    37: use Apache::File;
1.1       matthew    38: use Apache::response;
1.2       matthew    39: use Apache::lonxml;
1.20      matthew    40: use Apache::edit;
1.10      matthew    41: 
1.33      harris41   42: BEGIN {
1.47    ! matthew    43:   &Apache::lonxml::register('Apache::lonplot',('gnuplot'));
1.1       matthew    44: }
                     45: 
1.10      matthew    46: ## 
                     47: ## Description of data structures:
                     48: ##
                     49: ##  %plot       %key    %axis
                     50: ## --------------------------
                     51: ##  height      title   color
                     52: ##  width       box     xmin
                     53: ##  bgcolor     pos     xmax
                     54: ##  fgcolor             ymin
                     55: ##  transparent         ymax
                     56: ##  grid
                     57: ##  border
                     58: ##  font
1.19      matthew    59: ##  align
1.10      matthew    60: ##
                     61: ##  @labels: $labels[$i] = \%label
                     62: ##           %label: text, xpos, ypos, justify
1.14      matthew    63: ##
1.10      matthew    64: ##  @curves: $curves[$i] = \%curve
1.14      matthew    65: ##           %curve: name, linestyle, ( function | data )
1.10      matthew    66: ##
                     67: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
                     68: ##                            [y1,y2,y3,y4] ]
                     69: ##
1.21      matthew    70: 
                     71: ###################################################################
                     72: ##                                                               ##
                     73: ##        Tests used in checking the validitity of input         ##
                     74: ##                                                               ##
                     75: ###################################################################
1.29      matthew    76: 
1.32      matthew    77: my $max_str_len = 50;    # if a label, title, xlabel, or ylabel text
                     78:                          # is longer than this, it will be truncated.
                     79: 
1.29      matthew    80: my %linestyles = 
                     81:     (
                     82:      lines          => 2,     # Maybe this will be used in the future
                     83:      linespoints    => 2,     # to check on whether or not they have 
                     84:      dots	    => 2,     # supplied enough <data></data> fields
                     85:      points         => 2,     # to use the given line style.  But for
                     86:      steps	    => 2,     # now there are more important things 
                     87:      fsteps	    => 2,     # for me to deal with.
                     88:      histeps        => 2,
1.34      matthew    89:      errorbars	    => 3,
                     90:      xerrorbars	    => [3,4],
                     91:      yerrorbars	    => [3,4],
1.35      matthew    92:      xyerrorbars    => [4,6],
1.34      matthew    93:      boxes          => 3,
1.35      matthew    94: #     boxerrorbars   => [3,4,5],
                     95: #     boxxyerrorbars => [4,6,7],
                     96: #     financebars    => 5,
                     97: #     candlesticks   => 5,
                     98:      vector	    => 4
1.29      matthew    99:     );		    
                    100: 
1.11      matthew   101: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19      matthew   102: my $real_test      = 
                    103:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.11      matthew   104: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
1.1       matthew   105: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
1.15      matthew   106: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.1       matthew   107: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
1.29      matthew   108: my $linestyle_test = sub {exists($linestyles{$_[0]})};
1.15      matthew   109: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
1.21      matthew   110: 
                    111: ###################################################################
                    112: ##                                                               ##
                    113: ##                      Attribute metadata                       ##
                    114: ##                                                               ##
                    115: ###################################################################
1.47    ! matthew   116: my @gnuplot_edit_order = 
1.37      matthew   117:     qw/bgcolor fgcolor height width font transparent grid border align/;
1.47    ! matthew   118: my %gnuplot_defaults = 
1.1       matthew   119:     (
1.20      matthew   120:      height       => {
                    121: 	 default     => 200,
                    122: 	 test        => $int_test,
1.29      matthew   123: 	 description => 'height of image (pixels)',
1.38      matthew   124:       	 edit_type   => 'entry',
                    125: 	 size        => '10'
1.20      matthew   126: 	 },
                    127:      width        => {
                    128: 	 default     => 200,
                    129: 	 test        => $int_test,
1.29      matthew   130: 	 description => 'width of image (pixels)',
1.38      matthew   131: 	 edit_type   => 'entry',
                    132: 	 size        => '10'
1.20      matthew   133: 	 },
                    134:      bgcolor      => {
                    135: 	 default     => 'xffffff',
                    136: 	 test        => $color_test, 
                    137: 	 description => 'background color of image (xffffff)',
1.38      matthew   138: 	 edit_type   => 'entry',
                    139: 	 size        => '10'
1.20      matthew   140: 	 },
                    141:      fgcolor      => {
                    142: 	 default     => 'x000000',
                    143: 	 test        => $color_test,
                    144: 	 description => 'foreground color of image (x000000)',
1.38      matthew   145: 	 edit_type   => 'entry',
                    146: 	 size        => '10'
1.20      matthew   147: 	 },
                    148:      transparent  => {
                    149: 	 default     => 'off',
                    150: 	 test        => $onoff_test, 
1.34      matthew   151: 	 description => 'Transparent image',
1.45      matthew   152: 	 edit_type   => 'onoff'
1.20      matthew   153: 	 },
                    154:      grid         => {
                    155: 	 default     => 'off',
                    156: 	 test        => $onoff_test, 
1.34      matthew   157: 	 description => 'Display grid',
1.45      matthew   158: 	 edit_type   => 'onoff'
1.20      matthew   159: 	 },
                    160:      border       => {
                    161: 	 default     => 'on',
                    162: 	 test        => $onoff_test, 
1.34      matthew   163: 	 description => 'Draw border around plot',
1.45      matthew   164: 	 edit_type   => 'onoff'
1.20      matthew   165: 	 },
                    166:      font         => {
                    167: 	 default     => 'medium',
                    168: 	 test        => $sml_test,
                    169: 	 description => 'Size of font to use',
                    170: 	 edit_type   => 'choice',
                    171: 	 choices     => ['small','medium','large']
                    172: 	 },
                    173:      align        => {
                    174: 	 default     => 'left',
                    175: 	 test        => sub {$_[0]=~/^(left|right|center)$/},
                    176: 	 description => 'alignment for image in html',
                    177: 	 edit_type   => 'choice',
                    178: 	 choices     => ['left','right','center']
                    179: 	 } 
1.1       matthew   180:      );
                    181: 
                    182: my %key_defaults = 
                    183:     (
1.20      matthew   184:      title => { 
                    185: 	 default => '',
                    186: 	 test => $words_test,
                    187: 	 description => 'Title of key',
1.38      matthew   188: 	 edit_type   => 'entry',
                    189: 	 size        => '40'
1.20      matthew   190: 	 },
                    191:      box   => { 
                    192: 	 default => 'off',
                    193: 	 test => $onoff_test,
                    194: 	 description => 'Draw a box around the key?',
1.45      matthew   195: 	 edit_type   => 'onoff'
1.20      matthew   196: 	 },
                    197:      pos   => { 
                    198: 	 default => 'top right', 
                    199: 	 test => $key_pos_test, 
                    200: 	 description => 'position of the key on the plot',
                    201: 	 edit_type   => 'choice',
                    202: 	 choices     => ['top left','top right','bottom left','bottom right',
                    203: 			 'outside','below']
                    204: 	 }
1.1       matthew   205:      );
                    206: 
                    207: my %label_defaults = 
                    208:     (
1.20      matthew   209:      xpos    => {
                    210: 	 default => 0,
                    211: 	 test => $real_test,
                    212: 	 description => 'x position of label (graph coordinates)',
1.38      matthew   213: 	 edit_type   => 'entry',
                    214: 	 size        => '10'
1.20      matthew   215: 	 },
                    216:      ypos    => {
                    217: 	 default => 0, 
                    218: 	 test => $real_test,
                    219: 	 description => 'y position of label (graph coordinates)',
1.38      matthew   220: 	 edit_type   => 'entry',
                    221: 	 size        => '10'
1.20      matthew   222: 	 },
                    223:      justify => {
                    224: 	 default => 'left',    
                    225: 	 test => sub {$_[0]=~/^(left|right|center)$/},
                    226: 	 description => 'justification of the label text on the plot',
                    227: 	 edit_type   => 'choice',
                    228: 	 choices     => ['left','right','center']
                    229:      }
1.1       matthew   230:      );
                    231: 
1.45      matthew   232: my @tic_edit_order = ('location','mirror','start','increment','end');
                    233: my %tic_defaults =
                    234:     (
                    235:      location => {
                    236: 	 default => 'border', 
                    237: 	 test => sub {$_[0]=~/^(border|axis)$/},
                    238: 	 description => 'Location of tick marks',
                    239: 	 edit_type   => 'choice',
                    240: 	 choices     => ['border','axis']
                    241: 	 },
                    242:      mirror => {
                    243: 	 default => 'on', 
                    244: 	 test => $onoff_test,
                    245: 	 description => 'mirror ticks on opposite axis?',
                    246: 	 edit_type   => 'onoff'
                    247: 	 },
                    248:      start => {
                    249: 	 default => '-10.0',
                    250: 	 test => $real_test,
                    251: 	 description => 'Start ticks at',
                    252: 	 edit_type   => 'entry',
                    253: 	 size        => '10'
                    254: 	 },
                    255:      increment => {
                    256: 	 default => '1.0',
                    257: 	 test => $real_test,
                    258: 	 description => 'Place a tick every',
                    259: 	 edit_type   => 'entry',
                    260: 	 size        => '10'
                    261: 	 },
                    262:      end => {
                    263: 	 default => ' 10.0',
                    264: 	 test => $real_test,
                    265: 	 description => 'Stop ticks at ',
                    266: 	 edit_type   => 'entry',
                    267: 	 size        => '10'
                    268: 	 },
                    269:      );
                    270: 
1.1       matthew   271: my %axis_defaults = 
                    272:     (
1.28      matthew   273:      color   => {
1.20      matthew   274: 	 default => 'x000000', 
                    275: 	 test => $color_test,
                    276: 	 description => 'color of axes (x000000)',
1.38      matthew   277: 	 edit_type   => 'entry',
                    278: 	 size        => '10'
1.20      matthew   279: 	 },
                    280:      xmin      => {
                    281: 	 default => '-10.0',
                    282: 	 test => $real_test,
                    283: 	 description => 'minimum x-value shown in plot',
1.38      matthew   284: 	 edit_type   => 'entry',
                    285: 	 size        => '10'
1.20      matthew   286: 	 },
                    287:      xmax      => {
                    288: 	 default => ' 10.0',
                    289: 	 test => $real_test,
                    290: 	 description => 'maximum x-value shown in plot',	 
1.38      matthew   291: 	 edit_type   => 'entry',
                    292: 	 size        => '10'
1.20      matthew   293: 	 },
                    294:      ymin      => {
                    295: 	 default => '-10.0',
                    296: 	 test => $real_test,
                    297: 	 description => 'minimum y-value shown in plot',	 
1.38      matthew   298: 	 edit_type   => 'entry',
                    299: 	 size        => '10'
1.20      matthew   300: 	 },
                    301:      ymax      => {
                    302: 	 default => ' 10.0',
                    303: 	 test => $real_test,
                    304: 	 description => 'maximum y-value shown in plot',	 
1.38      matthew   305: 	 edit_type   => 'entry',
                    306: 	 size        => '10'
1.20      matthew   307: 	 }
1.1       matthew   308:      );
                    309: 
                    310: my %curve_defaults = 
                    311:     (
1.20      matthew   312:      color     => {
                    313: 	 default => 'x000000',
                    314: 	 test => $color_test,
                    315: 	 description => 'color of curve (x000000)',
1.38      matthew   316: 	 edit_type   => 'entry',
                    317: 	 size        => '10'
1.20      matthew   318: 	 },
                    319:      name      => {
                    320: 	 default => '',
                    321: 	 test => $words_test,
                    322: 	 description => 'name of curve to appear in key',
1.38      matthew   323: 	 edit_type   => 'entry',
                    324: 	 size        => '20'
1.20      matthew   325: 	 },
                    326:      linestyle => {
                    327: 	 default => 'lines',
                    328: 	 test => $linestyle_test,
1.35      matthew   329: 	 description => 'Line style',
1.20      matthew   330: 	 edit_type   => 'choice',
1.38      matthew   331: 	 choices     => [keys(%linestyles)]
1.20      matthew   332: 	 }
1.1       matthew   333:      );
                    334: 
1.21      matthew   335: ###################################################################
                    336: ##                                                               ##
                    337: ##                    parsing and edit rendering                 ##
                    338: ##                                                               ##
                    339: ###################################################################
1.45      matthew   340: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
1.1       matthew   341: 
1.47    ! matthew   342: sub start_gnuplot {
1.23      matthew   343:     %plot    = ();      %key     = ();      %axis   = (); 
1.10      matthew   344:     $title   = undef;   $xlabel  = undef;   $ylabel = undef;
                    345:     $#labels = -1;      $#curves = -1;
1.45      matthew   346:     %xtics    = ();      %ytics    = ();
1.6       matthew   347:     #
1.1       matthew   348:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    349:     my $result='';
1.25      matthew   350:     &Apache::lonxml::register('Apache::lonplot',
1.45      matthew   351: 	     ('title','xlabel','ylabel','key','axis','label','curve',
                    352: 	      'xtics','ytics'));
1.29      matthew   353:     push (@Apache::lonxml::namespace,'lonplot');
1.4       matthew   354:     if ($target eq 'web') {
1.47    ! matthew   355: 	my $inside = &Apache::lonxml::get_all_text("/gnuplot",$$parser[-1]);
1.17      matthew   356: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
1.29      matthew   357: 	&Apache::lonxml::newparser($parser,\$inside);
1.47    ! matthew   358: 	&get_attributes(\%plot,\%gnuplot_defaults,$parstack,$safeeval,
1.17      matthew   359: 			$tagstack->[-1]);
1.20      matthew   360:     } elsif ($target eq 'edit') {
1.47    ! matthew   361: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
        !           362: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
        !           363: 				    \@gnuplot_edit_order);
1.20      matthew   364:     } elsif ($target eq 'modified') {
                    365: 	my $constructtag=&Apache::edit::get_new_args
1.47    ! matthew   366: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
1.20      matthew   367: 	if ($constructtag) {
                    368: 	    $result = &Apache::edit::rebuild_tag($token);
                    369: 	}
1.4       matthew   370:     }
1.21      matthew   371:     return $result;
1.1       matthew   372: }
                    373: 
1.47    ! matthew   374: sub end_gnuplot {
1.1       matthew   375:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    376:     pop @Apache::lonxml::namespace;
1.4       matthew   377:     &Apache::lonxml::deregister('Apache::lonplot',
                    378: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    379:     my $result = '';
                    380:     if ($target eq 'web') {
1.21      matthew   381: 	&check_inputs(); # Make sure we have all the data we need
1.13      matthew   382: 	##
                    383: 	## Determine filename
1.4       matthew   384: 	my $tmpdir = '/home/httpd/perl/tmp/';
1.12      matthew   385: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.29      matthew   386: 	    '_'.time.'_'.$$.int(rand(1000)).'_plot.data';
1.4       matthew   387: 	## Write the plot description to the file
1.40      matthew   388: 	&write_gnuplot_file($tmpdir,$filename);
1.4       matthew   389: 	## return image tag for the plot
1.12      matthew   390: 	$result .= <<"ENDIMAGE";
1.16      matthew   391: <img src    = "/cgi-bin/plot.gif?$filename" 
                    392:      width  = "$plot{'width'}" 
                    393:      height = "$plot{'height'}"
                    394:      align  = "$plot{'align'}"
1.47    ! matthew   395:      alt    = "image should be /cgi-bin/plot.gif?$filename" />
1.12      matthew   396: ENDIMAGE
1.20      matthew   397:     } elsif ($target eq 'edit') {
1.21      matthew   398: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   399:     }
1.1       matthew   400:     return $result;
                    401: }
1.2       matthew   402: 
1.45      matthew   403: 
                    404: ##--------------------------------------------------------------- xtics
                    405: sub start_xtics {
                    406:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    407:     my $result='';
                    408:     if ($target eq 'web') {
                    409: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
                    410: 		    $tagstack->[-1]);
                    411:     } elsif ($target eq 'edit') {
                    412: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
                    413: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    414: 				    \@tic_edit_order);
                    415:     } elsif ($target eq 'modified') {
                    416: 	my $constructtag=&Apache::edit::get_new_args
                    417: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    418: 	if ($constructtag) {
                    419: 	    $result = &Apache::edit::rebuild_tag($token);
                    420: 	}
                    421:     }
                    422:     return $result;
                    423: }
                    424: 
                    425: sub end_xtics {
                    426:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    427:     my $result = '';
                    428:     if ($target eq 'web') {
                    429:     } elsif ($target eq 'edit') {
                    430: 	$result.=&Apache::edit::tag_end($target,$token);
                    431:     }
                    432:     return $result;
                    433: }
                    434: 
                    435: ##--------------------------------------------------------------- ytics
                    436: sub start_ytics {
                    437:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    438:     my $result='';
                    439:     if ($target eq 'web') {
                    440: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
                    441: 		    $tagstack->[-1]);
                    442:     } elsif ($target eq 'edit') {
                    443: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
                    444: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    445: 				    \@tic_edit_order);
                    446:     } elsif ($target eq 'modified') {
                    447: 	my $constructtag=&Apache::edit::get_new_args
                    448: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    449: 	if ($constructtag) {
                    450: 	    $result = &Apache::edit::rebuild_tag($token);
                    451: 	}
                    452:     }
                    453:     return $result;
                    454: }
                    455: 
                    456: sub end_ytics {
                    457:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    458:     my $result = '';
                    459:     if ($target eq 'web') {
                    460:     } elsif ($target eq 'edit') {
                    461: 	$result.=&Apache::edit::tag_end($target,$token);
                    462:     }
                    463:     return $result;
                    464: }
                    465: 
                    466: 
1.1       matthew   467: ##----------------------------------------------------------------- key
                    468: sub start_key {
                    469:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    470:     my $result='';
1.17      matthew   471:     if ($target eq 'web') {
                    472: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   473: 		    $tagstack->[-1]);
1.20      matthew   474:     } elsif ($target eq 'edit') {
1.25      matthew   475: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21      matthew   476: 	$result .= &edit_attributes($target,$token,\%key_defaults);
1.20      matthew   477:     } elsif ($target eq 'modified') {
                    478: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   479: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
1.20      matthew   480: 	if ($constructtag) {
                    481: 	    $result = &Apache::edit::rebuild_tag($token);
                    482: 	}
1.4       matthew   483:     }
1.1       matthew   484:     return $result;
                    485: }
                    486: 
                    487: sub end_key {
                    488:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    489:     my $result = '';
1.4       matthew   490:     if ($target eq 'web') {
1.20      matthew   491:     } elsif ($target eq 'edit') {
1.21      matthew   492: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   493:     }
1.1       matthew   494:     return $result;
                    495: }
1.21      matthew   496: 
1.1       matthew   497: ##------------------------------------------------------------------- title
                    498: sub start_title {
                    499:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    500:     my $result='';
1.4       matthew   501:     if ($target eq 'web') {
1.17      matthew   502: 	$title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.32      matthew   503: 	if (length($title) > $max_str_len) {
                    504: 	    $title = substr($title,0,$max_str_len);
                    505: 	}
1.20      matthew   506:     } elsif ($target eq 'edit') {
1.25      matthew   507: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.22      matthew   508: 	my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.39      matthew   509: 	$result.=&Apache::edit::end_row().
                    510: 	    &Apache::edit::start_spanning_row().
1.30      matthew   511: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   512:     } elsif ($target eq 'modified') {
1.29      matthew   513: 	my $text=$$parser[-1]->get_text("/title");
1.42      matthew   514: 	$result.=&Apache::edit::rebuild_tag($token);
1.21      matthew   515: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   516:     }
1.1       matthew   517:     return $result;
                    518: }
                    519: 
                    520: sub end_title {
                    521:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    522:     my $result = '';
1.4       matthew   523:     if ($target eq 'web') {
1.20      matthew   524:     } elsif ($target eq 'edit') {
1.27      matthew   525: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   526:     }
1.1       matthew   527:     return $result;
                    528: }
                    529: ##------------------------------------------------------------------- xlabel
                    530: sub start_xlabel {
                    531:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    532:     my $result='';
1.4       matthew   533:     if ($target eq 'web') {
1.17      matthew   534: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.32      matthew   535: 	if (length($xlabel) > $max_str_len) {
                    536: 	    $xlabel = substr($xlabel,0,$max_str_len);
                    537: 	}
1.20      matthew   538:     } elsif ($target eq 'edit') {
1.25      matthew   539: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.22      matthew   540: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.39      matthew   541: 	$result.=&Apache::edit::end_row().
                    542: 	    &Apache::edit::start_spanning_row().
1.30      matthew   543: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   544:     } elsif ($target eq 'modified') {
1.29      matthew   545: 	my $text=$$parser[-1]->get_text("/xlabel");
1.42      matthew   546: 	$result.=&Apache::edit::rebuild_tag($token);	
1.21      matthew   547: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   548:     }
1.1       matthew   549:     return $result;
                    550: }
                    551: 
                    552: sub end_xlabel {
                    553:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    554:     my $result = '';
1.4       matthew   555:     if ($target eq 'web') {
1.20      matthew   556:     } elsif ($target eq 'edit') {
1.27      matthew   557: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   558:     }
1.1       matthew   559:     return $result;
                    560: }
1.21      matthew   561: 
1.1       matthew   562: ##------------------------------------------------------------------- ylabel
                    563: sub start_ylabel {
                    564:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    565:     my $result='';
1.4       matthew   566:     if ($target eq 'web') {
1.17      matthew   567: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.32      matthew   568: 	if (length($ylabel) > $max_str_len) {
                    569: 	    $ylabel = substr($ylabel,0,$max_str_len);
                    570: 	}
1.20      matthew   571:     } elsif ($target eq 'edit') {
1.25      matthew   572: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.22      matthew   573: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.39      matthew   574: 	$result .= &Apache::edit::end_row().
                    575: 	    &Apache::edit::start_spanning_row().
1.30      matthew   576: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   577:     } elsif ($target eq 'modified') {
1.29      matthew   578: 	my $text=$$parser[-1]->get_text("/ylabel");
1.42      matthew   579: 	$result.=&Apache::edit::rebuild_tag($token);
1.21      matthew   580: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   581:     }
1.1       matthew   582:     return $result;
                    583: }
                    584: 
                    585: sub end_ylabel {
                    586:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    587:     my $result = '';
1.4       matthew   588:     if ($target eq 'web') {
1.20      matthew   589:     } elsif ($target eq 'edit') {
1.27      matthew   590: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   591:     }
1.1       matthew   592:     return $result;
                    593: }
1.21      matthew   594: 
1.1       matthew   595: ##------------------------------------------------------------------- label
                    596: sub start_label {
                    597:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    598:     my $result='';
1.17      matthew   599:     if ($target eq 'web') {
                    600: 	my %label;
                    601: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew   602: 		    $tagstack->[-1]);
1.32      matthew   603: 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
                    604: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
                    605: 	$label{'text'} = $text;
1.17      matthew   606: 	push(@labels,\%label);
1.20      matthew   607:     } elsif ($target eq 'edit') {
1.25      matthew   608: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21      matthew   609: 	$result .= &edit_attributes($target,$token,\%label_defaults);
1.22      matthew   610: 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
1.39      matthew   611: 	$result .= &Apache::edit::end_row().
                    612: 	    &Apache::edit::start_spanning_row().
1.30      matthew   613: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   614:     } elsif ($target eq 'modified') {
1.42      matthew   615: 	&Apache::edit::get_new_args
1.24      matthew   616: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
1.42      matthew   617: 	$result.=&Apache::edit::rebuild_tag($token);
1.22      matthew   618: 	my $text=$$parser[-1]->get_text("/label");
1.21      matthew   619: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   620:     }
1.1       matthew   621:     return $result;
                    622: }
                    623: 
                    624: sub end_label {
                    625:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    626:     my $result = '';
1.4       matthew   627:     if ($target eq 'web') {
1.20      matthew   628:     } elsif ($target eq 'edit') {
1.21      matthew   629: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   630:     }
1.1       matthew   631:     return $result;
                    632: }
                    633: 
                    634: ##------------------------------------------------------------------- curve
                    635: sub start_curve {
                    636:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    637:     my $result='';
1.25      matthew   638:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
                    639:     push (@Apache::lonxml::namespace,'curve');
1.17      matthew   640:     if ($target eq 'web') {
                    641: 	my %curve;
                    642: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew   643: 		    $tagstack->[-1]);
1.17      matthew   644: 	push (@curves,\%curve);
1.20      matthew   645:     } elsif ($target eq 'edit') {
1.26      matthew   646: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
1.21      matthew   647: 	$result .= &edit_attributes($target,$token,\%curve_defaults);
1.20      matthew   648:     } elsif ($target eq 'modified') {
                    649: 	my $constructtag=&Apache::edit::get_new_args
1.35      matthew   650: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
1.20      matthew   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_curve {
                    660:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    661:     my $result = '';
1.25      matthew   662:     pop @Apache::lonxml::namespace;
                    663:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.4       matthew   664:     if ($target eq 'web') {
1.20      matthew   665:     } elsif ($target eq 'edit') {
1.21      matthew   666: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   667:     }
1.1       matthew   668:     return $result;
                    669: }
1.21      matthew   670: 
1.1       matthew   671: ##------------------------------------------------------------ curve function
                    672: sub start_function {
                    673:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    674:     my $result='';
1.4       matthew   675:     if ($target eq 'web') {
1.17      matthew   676: 	if (exists($curves[-1]->{'data'})) {
                    677: 	    &Apache::lonxml::warning('Use of <function> precludes use of <data>.  The <data> will be omitted in favor of the <function> declaration.');
                    678: 	    delete $curves[-1]->{'data'} ;
                    679: 	}
                    680: 	$curves[-1]->{'function'} = 
                    681: 	    &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.20      matthew   682:     } elsif ($target eq 'edit') {
1.37      matthew   683: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1.22      matthew   684: 	my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.39      matthew   685: 	$result .= &Apache::edit::end_row().
                    686: 	    &Apache::edit::start_spanning_row().
1.30      matthew   687: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   688:     } elsif ($target eq 'modified') {
1.42      matthew   689: 	$result.=&Apache::edit::rebuild_tag($token);
1.20      matthew   690: 	my $text=$$parser[-1]->get_text("/function");
                    691: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   692:     }
1.1       matthew   693:     return $result;
                    694: }
                    695: 
                    696: sub end_function {
                    697:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    698:     my $result = '';
1.4       matthew   699:     if ($target eq 'web') {
1.20      matthew   700:     } elsif ($target eq 'edit') {
1.26      matthew   701: 	$result .= &Apache::edit::end_table();
1.4       matthew   702:     }
1.1       matthew   703:     return $result;
                    704: }
1.21      matthew   705: 
1.1       matthew   706: ##------------------------------------------------------------ curve  data
                    707: sub start_data {
                    708:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    709:     my $result='';
1.4       matthew   710:     if ($target eq 'web') {
1.17      matthew   711: 	if (exists($curves[-1]->{'function'})) {
                    712: 	    &Apache::lonxml::warning('Use of <data> precludes use of .'.
                    713: 	    '<function>.  The <function> will be omitted in favor of '.
                    714:             'the <data> declaration.');
                    715: 	    delete($curves[-1]->{'function'});
                    716: 	}
                    717: 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
1.40      matthew   718: 	# Deal with cases where we're given an array...
                    719: 	if ($datatext =~ /^\@/) {
                    720: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
                    721: 					  $safeeval,1);
                    722: 	}
1.17      matthew   723: 	$datatext =~ s/\s+/ /g;  
                    724: 	# Need to do some error checking on the @data array - 
                    725: 	# make sure it's all numbers and make sure each array 
                    726: 	# is of the same length.
                    727: 	my @data;
1.35      matthew   728: 	if ($datatext =~ /,/) { # comma deliminated
1.17      matthew   729: 	    @data = split /,/,$datatext;
                    730: 	} else { # Assume it's space seperated.
                    731: 	    @data = split / /,$datatext;
                    732: 	}
                    733: 	for (my $i=0;$i<=$#data;$i++) {
                    734: 	    # Check that it's non-empty
1.19      matthew   735: 	    if (! defined($data[$i])) {
                    736: 		&Apache::lonxml::warning(
                    737: 		    'undefined <data> value.  Replacing with '.
                    738: 		    ' pi/e = 1.15572734979092');
                    739: 		$data[$i] = 1.15572734979092;
                    740: 	    }
1.17      matthew   741: 	    # Check that it's a number
1.19      matthew   742: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
                    743: 		&Apache::lonxml::warning(
                    744: 		    'Bad <data> value of '.$data[$i].'  Replacing with '.
                    745: 		    ' pi/e = 1.15572734979092');
                    746: 		$data[$i] = 1.15572734979092;
                    747: 	    }
1.17      matthew   748: 	}
1.35      matthew   749: 	# complain if the number of data points is not the same as
                    750: 	# in previous sets of data.
1.36      matthew   751: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
1.35      matthew   752: 	    &Apache::lonxml::warning
                    753: 		('Number of data points is not consistent with previous '.
                    754: 		 'number of data points');
                    755: 	}
1.17      matthew   756: 	push  @{$curves[-1]->{'data'}},\@data;
1.20      matthew   757:     } elsif ($target eq 'edit') {
1.37      matthew   758: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1.22      matthew   759: 	my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
1.39      matthew   760: 	$result .= &Apache::edit::end_row().
                    761: 	    &Apache::edit::start_spanning_row().
1.30      matthew   762: 	    &Apache::edit::editfield('',$text,'',60,1);
1.20      matthew   763:     } elsif ($target eq 'modified') {
1.42      matthew   764: 	$result.=&Apache::edit::rebuild_tag($token);
1.21      matthew   765: 	my $text=$$parser[-1]->get_text("/data");
                    766: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   767:     }
1.1       matthew   768:     return $result;
                    769: }
                    770: 
                    771: sub end_data {
                    772:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    773:     my $result = '';
1.4       matthew   774:     if ($target eq 'web') {
1.20      matthew   775:     } elsif ($target eq 'edit') {
1.26      matthew   776: 	$result .= &Apache::edit::end_table();
1.4       matthew   777:     }
1.1       matthew   778:     return $result;
                    779: }
                    780: 
                    781: ##------------------------------------------------------------------- axis
                    782: sub start_axis {
                    783:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    784:     my $result='';
1.4       matthew   785:     if ($target eq 'web') {
1.17      matthew   786: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                    787: 			$tagstack->[-1]);
1.20      matthew   788:     } elsif ($target eq 'edit') {
1.25      matthew   789: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.21      matthew   790: 	$result .= &edit_attributes($target,$token,\%axis_defaults);
1.20      matthew   791:     } elsif ($target eq 'modified') {
1.29      matthew   792: 	my $constructtag=&Apache::edit::get_new_args
                    793: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
                    794: 	if ($constructtag) {
                    795: 	    $result = &Apache::edit::rebuild_tag($token);
                    796: 	}
1.4       matthew   797:     }
1.1       matthew   798:     return $result;
                    799: }
                    800: 
                    801: sub end_axis {
                    802:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    803:     my $result = '';
1.4       matthew   804:     if ($target eq 'web') {
1.20      matthew   805:     } elsif ($target eq 'edit') {
1.21      matthew   806: 	$result.=&Apache::edit::tag_end($target,$token);
1.20      matthew   807:     } elsif ($target eq 'modified') {
1.4       matthew   808:     }
1.1       matthew   809:     return $result;
                    810: }
                    811: 
1.21      matthew   812: ###################################################################
                    813: ##                                                               ##
                    814: ##        Utility Functions                                      ##
                    815: ##                                                               ##
                    816: ###################################################################
                    817: 
1.13      matthew   818: ##----------------------------------------------------------- set_defaults
                    819: sub set_defaults {
1.21      matthew   820:     my ($var,$defaults) = @_;
1.13      matthew   821:     my $key;
1.24      matthew   822:     foreach $key (keys(%$defaults)) {
1.13      matthew   823: 	$var->{$key} = $defaults->{$key}->{'default'};
                    824:     }
                    825: }
                    826: 
1.1       matthew   827: ##------------------------------------------------------------------- misc
1.2       matthew   828: sub get_attributes{
1.21      matthew   829:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24      matthew   830:     foreach my $attr (keys(%{$defaults})) {
1.10      matthew   831: 	$values->{$attr} = 
1.15      matthew   832: 	    &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1.10      matthew   833: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew   834: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew   835: 	    next;
                    836: 	}
1.10      matthew   837: 	my $test = $defaults->{$attr}->{'test'};
                    838: 	if (! &$test($values->{$attr})) {
1.6       matthew   839: 	    &Apache::lonxml::warning
                    840: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew   841: 		 .$defaults->{$attr}->{'default'} );
                    842: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew   843: 	}
1.2       matthew   844:     }
1.11      matthew   845:     return ;
1.6       matthew   846: }
1.40      matthew   847: 
1.15      matthew   848: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew   849: sub write_gnuplot_file {
1.40      matthew   850:     my ($tmpdir,$filename)= @_;
1.6       matthew   851:     my $gnuplot_input = '';
1.10      matthew   852:     my $curve;
1.6       matthew   853:     # Collect all the colors
                    854:     my @Colors;
                    855:     push @Colors, $plot{'bgcolor'};
                    856:     push @Colors, $plot{'fgcolor'}; 
1.13      matthew   857:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9       matthew   858:     foreach $curve (@curves) {
                    859: 	push @Colors, ($curve->{'color'} ne '' ? 
                    860: 		       $curve->{'color'}       : 
1.13      matthew   861: 		       $plot{'fgcolor'}        );
1.6       matthew   862:     }
                    863:     # set term
                    864:     $gnuplot_input .= 'set term gif ';
                    865:     $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
                    866:     $gnuplot_input .= $plot{'font'} . ' ';
1.10      matthew   867:     $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
1.6       matthew   868:     $gnuplot_input .= "@Colors\n";
1.45      matthew   869:     # set output
                    870:     $gnuplot_input .= "set output\n";
1.7       matthew   871:     # grid
1.10      matthew   872:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7       matthew   873:     # border
1.9       matthew   874:     $gnuplot_input .= ($plot{'border'} eq 'on'?
                    875: 		       'set border'.$/           :
                    876: 		       'set noborder'.$/         );    # title, xlabel, ylabel
1.45      matthew   877:     # titles
1.13      matthew   878:     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;
                    879:     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
                    880:     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
1.45      matthew   881:     # tics
                    882:     if (%xtics) {    
                    883: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
1.46      matthew   884: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew   885: 	$gnuplot_input .= "$xtics{'start'}, ";
                    886: 	$gnuplot_input .= "$xtics{'increment'}, ";
                    887: 	$gnuplot_input .= "$xtics{'end'}\n";
                    888:     }
                    889:     if (%ytics) {    
                    890: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
1.46      matthew   891: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew   892: 	$gnuplot_input .= "$ytics{'start'}, ";
                    893: 	$gnuplot_input .= "$ytics{'increment'}, ";
                    894:         $gnuplot_input .= "$ytics{'end'}\n";
                    895:     }
                    896:     # axis
1.23      matthew   897:     if (%axis) {
1.13      matthew   898: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                    899: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6       matthew   900:     }
                    901:     # Key
1.23      matthew   902:     if (%key) {
1.9       matthew   903: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                    904: 	if ($key{'title'} ne '') {
1.43      matthew   905: 	    $gnuplot_input .= 'title " '.$key{'title'}.'" ';
1.11      matthew   906: 	} 
                    907: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew   908:     } else {
1.9       matthew   909: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew   910:     }
1.6       matthew   911:     # labels
1.10      matthew   912:     my $label;
1.6       matthew   913:     foreach $label (@labels) {
                    914: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.9       matthew   915: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
1.6       matthew   916:     }
                    917:     # curves
                    918:     $gnuplot_input .= 'plot ';
1.9       matthew   919:     for (my $i = 0;$i<=$#curves;$i++) {
                    920: 	$curve = $curves[$i];
                    921: 	$gnuplot_input.= ', ' if ($i > 0);
1.6       matthew   922: 	if (exists($curve->{'function'})) {
1.9       matthew   923: 	    $gnuplot_input.= 
                    924: 		$curve->{'function'}.' title "'.
                    925: 		$curve->{'name'}.'" with '.
                    926: 		$curve->{'linestyle'};
1.6       matthew   927: 	} elsif (exists($curve->{'data'})) {
1.40      matthew   928: 	    # Store data values in $datatext
                    929: 	    my $datatext = '';
                    930: 	    #   get new filename
                    931: 	    my $datafilename = "$tmpdir/$filename.$i";
                    932: 	    my $fh=Apache::File->new(">$datafilename");
                    933: 	    # Compile data
1.6       matthew   934: 	    my @Data = @{$curve->{'data'}};
1.9       matthew   935: 	    my @Data0 = @{$Data[0]};
                    936: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew   937: 		my $dataset;
1.6       matthew   938: 		foreach $dataset (@Data) {
1.9       matthew   939: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew   940: 		}
1.9       matthew   941: 		$datatext .= $/;
1.6       matthew   942: 	    }
1.40      matthew   943: 	    #   write file
                    944: 	    print $fh $datatext;
                    945: 	    close ($fh);
                    946: 	    #   generate gnuplot text
                    947: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
                    948: 		$curve->{'name'}.'" with '.
                    949: 		$curve->{'linestyle'};
1.6       matthew   950: 	}
                    951:     }
1.40      matthew   952:     # Write the output to a file.
                    953:     my $fh=Apache::File->new(">$tmpdir$filename");
                    954:     print $fh $gnuplot_input;
                    955:     close($fh);
                    956:     # That's all folks.
                    957:     return ;
1.2       matthew   958: }
1.21      matthew   959: 
                    960: #---------------------------------------------- check_inputs
                    961: sub check_inputs {
                    962:     ## Note: no inputs, no outputs - this acts only on global variables.
                    963:     ## Make sure we have all the input we need:
1.47    ! matthew   964:     if (! %plot) { &set_defaults(\%plot,\%gnuplot_defaults); }
1.23      matthew   965:     if (! %key ) {} # No key for this plot, thats okay
1.34      matthew   966: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21      matthew   967:     if (! defined($title )) {} # No title for this plot, thats okay
                    968:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
                    969:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
                    970:     if ($#labels < 0) { }      # No labels for this plot, thats okay
                    971:     if ($#curves < 0) { 
                    972: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
                    973: 	return '';
                    974:     }
                    975:     my $curve;
                    976:     foreach $curve (@curves) {
                    977: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
                    978: 	    &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
                    979: 	    return '';
                    980: 	}
                    981:     }
                    982: }
                    983: 
1.20      matthew   984: #------------------------------------------------ make_edit
                    985: sub edit_attributes {
1.34      matthew   986:     my ($target,$token,$defaults,$keys) = @_;
                    987:     my ($result,@keys);
                    988:     if ($keys && ref($keys) eq 'ARRAY') {
                    989:         @keys = @$keys;
                    990:     } else {
                    991: 	@keys = sort(keys(%$defaults));
                    992:     }
                    993:     foreach my $attr (@keys) {
1.35      matthew   994: 	# append a ' ' to the description if it doesn't have one already.
                    995: 	my $description = $defaults->{$attr}->{'description'};
                    996: 	$description .= ' ' if ($description !~ / $/);
1.20      matthew   997: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1.35      matthew   998: 	    $result .= &Apache::edit::text_arg
1.38      matthew   999: 		($description,$attr,$token,
                   1000: 		 $defaults->{$attr}->{'size'});
1.20      matthew  1001: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1.35      matthew  1002: 	    $result .= &Apache::edit::select_arg
                   1003: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
1.45      matthew  1004: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1.35      matthew  1005: 	    $result .= &Apache::edit::select_arg
                   1006: 		($description,$attr,['on','off'],$token);
1.20      matthew  1007: 	}
1.25      matthew  1008: 	$result .= '<br />';
1.20      matthew  1009:     }
                   1010:     return $result;
                   1011: }
1.1       matthew  1012: 
1.21      matthew  1013: 
                   1014: ###################################################################
                   1015: ##                                                               ##
                   1016: ##           Insertion functions for editing plots               ##
                   1017: ##                                                               ##
                   1018: ###################################################################
                   1019: 
1.47    ! matthew  1020: sub insert_gnuplot {
1.29      matthew  1021:     my $result = '';
1.20      matthew  1022:     #  plot attributes
1.29      matthew  1023:     $result .= "<plot \n";
1.47    ! matthew  1024:     foreach my $attr (keys(%gnuplot_defaults)) {
        !          1025: 	$result .= "     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"\n";
1.20      matthew  1026:     }
                   1027:     $result .= ">\n";
1.47    ! matthew  1028:     # Add the components (most are commented out for simplicity)
1.44      matthew  1029:     # $result .= &insert_key();
                   1030:     # $result .= &insert_axis();
                   1031:     # $result .= &insert_title();    
                   1032:     # $result .= &insert_xlabel();    
                   1033:     # $result .= &insert_ylabel();    
1.20      matthew  1034:     $result .= &insert_curve();
                   1035:     # close up the <plot>
                   1036:     $result .= "</plot>\n";
1.45      matthew  1037:     return $result;
                   1038: }
                   1039: 
1.46      matthew  1040: sub insert_tics {
                   1041:     my $result;
                   1042:     $result .= &insert_xtics() . &insert_ytics;
                   1043:     return $result;
                   1044: }
                   1045: 
1.45      matthew  1046: sub insert_xtics {
                   1047:     my $result;
1.46      matthew  1048:     $result .= "\n    <xtics ";
1.45      matthew  1049:     foreach my $attr (keys(%tic_defaults)) {
1.46      matthew  1050: 	$result .= "$attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1051:     }
1.46      matthew  1052:     $result .= "/>\n";
1.45      matthew  1053:     return $result;
                   1054: }
                   1055: 
                   1056: sub insert_ytics {
                   1057:     my $result;
1.46      matthew  1058:     $result .= "\n    <ytics ";
1.45      matthew  1059:     foreach my $attr (keys(%tic_defaults)) {
1.46      matthew  1060: 	$result .= "$attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1061:     }
1.46      matthew  1062:     $result .= "/>\n";
1.20      matthew  1063:     return $result;
                   1064: }
                   1065: 
                   1066: sub insert_key {
                   1067:     my $result;
1.46      matthew  1068:     $result .= "\n    <key \n";
1.30      matthew  1069:     foreach my $attr (keys(%key_defaults)) {
1.29      matthew  1070: 	$result .= "         $attr=\"$key_defaults{$attr}->{'default'}\"\n";
1.20      matthew  1071:     }
                   1072:     $result .= "   />\n";
                   1073:     return $result;
                   1074: }
                   1075: 
                   1076: sub insert_axis{
                   1077:     my $result;
1.46      matthew  1078:     $result .= "\n    <axis ";
1.30      matthew  1079:    foreach my $attr (keys(%axis_defaults)) {
1.29      matthew  1080: 	$result .= "         $attr=\"$axis_defaults{$attr}->{'default'}\"\n";
1.20      matthew  1081:     }
                   1082:     $result .= "   />\n";
                   1083:     return $result;
                   1084: }
1.28      matthew  1085: 
1.46      matthew  1086: sub insert_title { return "\n    <title></title>\n"; }
                   1087: sub insert_xlabel { return "\n    <xlabel></xlabel>\n"; }
                   1088: sub insert_ylabel { return "\n    <ylabel></ylabel>\n"; }
1.20      matthew  1089: 
                   1090: sub insert_label {
                   1091:     my $result;
1.46      matthew  1092:     $result .= "\n    <label ";
1.30      matthew  1093:     foreach my $attr (keys(%label_defaults)) {
1.27      matthew  1094: 	$result .= '         '.$attr.'="'.
1.20      matthew  1095: 	    $label_defaults{$attr}->{'default'}."\"\n";
                   1096:     }
                   1097:     $result .= "   ></label>\n";
                   1098:     return $result;
                   1099: }
                   1100: 
                   1101: sub insert_curve {
                   1102:     my $result;
1.41      matthew  1103:     $result .= "\n    <curve ";
1.30      matthew  1104:     foreach my $attr (keys(%curve_defaults)) {
1.27      matthew  1105: 	$result .= '         '.$attr.'="'.
1.20      matthew  1106: 	    $curve_defaults{$attr}->{'default'}."\"\n";
                   1107:     }
1.41      matthew  1108:     $result .= "    >\n";
                   1109:     $result .= &insert_data().&insert_data()."</curve>\n";
1.20      matthew  1110: }
1.4       matthew  1111: 
1.20      matthew  1112: sub insert_function {
                   1113:     my $result;
                   1114:     $result .= "<function></function>\n";
                   1115:     return $result;
                   1116: }
1.4       matthew  1117: 
1.20      matthew  1118: sub insert_data {
                   1119:     my $result;
                   1120:     $result .= "     <data></data>\n";
                   1121:     return $result;
                   1122: }
1.4       matthew  1123: 
1.21      matthew  1124: ##----------------------------------------------------------------------
1.20      matthew  1125: 1;
                   1126: __END__
1.4       matthew  1127: 
                   1128: 

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