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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.63    ! albertel    4: # $Id: lonplot.pm,v 1.62 2002/03/22 16:56:37 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.52      matthew    32: # 01/21 02/05 02/06 2/28Matthew
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:      vector	    => 4
1.29      matthew    95:     );		    
                     96: 
1.11      matthew    97: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19      matthew    98: my $real_test      = 
                     99:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.62      matthew   100: my $pos_real_test  =
                    101:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+]?\d*\.?\d*([eE][+-]\d+)?$/};
1.11      matthew   102: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
1.1       matthew   103: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
1.15      matthew   104: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.1       matthew   105: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
1.29      matthew   106: my $linestyle_test = sub {exists($linestyles{$_[0]})};
1.15      matthew   107: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
1.21      matthew   108: 
                    109: ###################################################################
                    110: ##                                                               ##
                    111: ##                      Attribute metadata                       ##
                    112: ##                                                               ##
                    113: ###################################################################
1.47      matthew   114: my @gnuplot_edit_order = 
1.37      matthew   115:     qw/bgcolor fgcolor height width font transparent grid border align/;
1.48      matthew   116: 
                    117: my $gnuplot_help_text = <<"ENDPLOTHELP";
                    118: <p>
                    119: The <b>gnuplot</b> tag allows an author to design a plot which can
                    120: be created on the fly.  This is intended for use in homework problems
                    121: where each student needs to see a distinct plot.  It can be used in
                    122: conjunction with a <b>script</b> tag to generate random plots.
                    123: </p><p>
                    124: A <b>gnuplot</b> tag can contain the following sub-tags:
                    125: </p>
                    126: <dl>
                    127: <dt> Plot Label
                    128:     <dd> Allows you to place text at a given (x,y) coordinate on the plot.
                    129: <dt> Plot Title
                    130:     <dd> The title of the plot
                    131: <dt> Plot Xlabel
                    132:     <dd> The label on the horizontal axis of the plot
                    133: <dt> Plot Ylabel
                    134:     <dd> The label on the vertical axis of the plot
                    135: <dt> Plot Axes
                    136:     <dd> allows specification of the x and y ranges displayed in the plot
                    137: <dt> Plot Key
                    138:     <dd> Lists the functions displayed in the plot.
                    139: <dt> Plot Curve
                    140:     <dd> Sets the data used in the plot.
                    141: <dt> Plot Tics
                    142:     <dd> Allows specification of the x and y coordinate 'tics' on the axes.
                    143: This is mostly used to adjust the grid lines when a grid is displayed.
                    144: </dl>
1.55      matthew   145: If you are having trouble with your plot, please read the help
                    146: available on Plot Curve.
1.48      matthew   147: ENDPLOTHELP
                    148: 
1.47      matthew   149: my %gnuplot_defaults = 
1.1       matthew   150:     (
1.20      matthew   151:      height       => {
                    152: 	 default     => 200,
                    153: 	 test        => $int_test,
1.29      matthew   154: 	 description => 'height of image (pixels)',
1.38      matthew   155:       	 edit_type   => 'entry',
                    156: 	 size        => '10'
1.20      matthew   157: 	 },
                    158:      width        => {
                    159: 	 default     => 200,
                    160: 	 test        => $int_test,
1.29      matthew   161: 	 description => 'width of image (pixels)',
1.38      matthew   162: 	 edit_type   => 'entry',
                    163: 	 size        => '10'
1.20      matthew   164: 	 },
                    165:      bgcolor      => {
                    166: 	 default     => 'xffffff',
                    167: 	 test        => $color_test, 
                    168: 	 description => 'background color of image (xffffff)',
1.38      matthew   169: 	 edit_type   => 'entry',
                    170: 	 size        => '10'
1.20      matthew   171: 	 },
                    172:      fgcolor      => {
                    173: 	 default     => 'x000000',
                    174: 	 test        => $color_test,
                    175: 	 description => 'foreground color of image (x000000)',
1.38      matthew   176: 	 edit_type   => 'entry',
                    177: 	 size        => '10'
1.20      matthew   178: 	 },
                    179:      transparent  => {
                    180: 	 default     => 'off',
                    181: 	 test        => $onoff_test, 
1.34      matthew   182: 	 description => 'Transparent image',
1.45      matthew   183: 	 edit_type   => 'onoff'
1.20      matthew   184: 	 },
                    185:      grid         => {
                    186: 	 default     => 'off',
                    187: 	 test        => $onoff_test, 
1.34      matthew   188: 	 description => 'Display grid',
1.45      matthew   189: 	 edit_type   => 'onoff'
1.20      matthew   190: 	 },
                    191:      border       => {
                    192: 	 default     => 'on',
                    193: 	 test        => $onoff_test, 
1.34      matthew   194: 	 description => 'Draw border around plot',
1.45      matthew   195: 	 edit_type   => 'onoff'
1.20      matthew   196: 	 },
                    197:      font         => {
                    198: 	 default     => 'medium',
                    199: 	 test        => $sml_test,
                    200: 	 description => 'Size of font to use',
                    201: 	 edit_type   => 'choice',
                    202: 	 choices     => ['small','medium','large']
                    203: 	 },
                    204:      align        => {
                    205: 	 default     => 'left',
                    206: 	 test        => sub {$_[0]=~/^(left|right|center)$/},
                    207: 	 description => 'alignment for image in html',
                    208: 	 edit_type   => 'choice',
                    209: 	 choices     => ['left','right','center']
                    210: 	 } 
1.1       matthew   211:      );
                    212: 
                    213: my %key_defaults = 
                    214:     (
1.20      matthew   215:      title => { 
                    216: 	 default => '',
                    217: 	 test => $words_test,
                    218: 	 description => 'Title of key',
1.38      matthew   219: 	 edit_type   => 'entry',
                    220: 	 size        => '40'
1.20      matthew   221: 	 },
                    222:      box   => { 
                    223: 	 default => 'off',
                    224: 	 test => $onoff_test,
                    225: 	 description => 'Draw a box around the key?',
1.45      matthew   226: 	 edit_type   => 'onoff'
1.20      matthew   227: 	 },
                    228:      pos   => { 
                    229: 	 default => 'top right', 
                    230: 	 test => $key_pos_test, 
                    231: 	 description => 'position of the key on the plot',
                    232: 	 edit_type   => 'choice',
                    233: 	 choices     => ['top left','top right','bottom left','bottom right',
                    234: 			 'outside','below']
                    235: 	 }
1.1       matthew   236:      );
                    237: 
                    238: my %label_defaults = 
                    239:     (
1.20      matthew   240:      xpos    => {
                    241: 	 default => 0,
                    242: 	 test => $real_test,
                    243: 	 description => 'x position of label (graph coordinates)',
1.38      matthew   244: 	 edit_type   => 'entry',
                    245: 	 size        => '10'
1.20      matthew   246: 	 },
                    247:      ypos    => {
                    248: 	 default => 0, 
                    249: 	 test => $real_test,
                    250: 	 description => 'y position of label (graph coordinates)',
1.38      matthew   251: 	 edit_type   => 'entry',
                    252: 	 size        => '10'
1.20      matthew   253: 	 },
                    254:      justify => {
                    255: 	 default => 'left',    
                    256: 	 test => sub {$_[0]=~/^(left|right|center)$/},
                    257: 	 description => 'justification of the label text on the plot',
                    258: 	 edit_type   => 'choice',
                    259: 	 choices     => ['left','right','center']
                    260:      }
1.1       matthew   261:      );
                    262: 
1.45      matthew   263: my @tic_edit_order = ('location','mirror','start','increment','end');
                    264: my %tic_defaults =
                    265:     (
                    266:      location => {
                    267: 	 default => 'border', 
                    268: 	 test => sub {$_[0]=~/^(border|axis)$/},
                    269: 	 description => 'Location of tick marks',
                    270: 	 edit_type   => 'choice',
                    271: 	 choices     => ['border','axis']
                    272: 	 },
                    273:      mirror => {
                    274: 	 default => 'on', 
                    275: 	 test => $onoff_test,
                    276: 	 description => 'mirror ticks on opposite axis?',
                    277: 	 edit_type   => 'onoff'
                    278: 	 },
                    279:      start => {
                    280: 	 default => '-10.0',
                    281: 	 test => $real_test,
                    282: 	 description => 'Start ticks at',
                    283: 	 edit_type   => 'entry',
                    284: 	 size        => '10'
                    285: 	 },
                    286:      increment => {
                    287: 	 default => '1.0',
                    288: 	 test => $real_test,
                    289: 	 description => 'Place a tick every',
                    290: 	 edit_type   => 'entry',
                    291: 	 size        => '10'
                    292: 	 },
                    293:      end => {
                    294: 	 default => ' 10.0',
                    295: 	 test => $real_test,
                    296: 	 description => 'Stop ticks at ',
                    297: 	 edit_type   => 'entry',
                    298: 	 size        => '10'
                    299: 	 },
                    300:      );
                    301: 
1.1       matthew   302: my %axis_defaults = 
                    303:     (
1.28      matthew   304:      color   => {
1.20      matthew   305: 	 default => 'x000000', 
                    306: 	 test => $color_test,
                    307: 	 description => 'color of axes (x000000)',
1.38      matthew   308: 	 edit_type   => 'entry',
                    309: 	 size        => '10'
1.20      matthew   310: 	 },
                    311:      xmin      => {
                    312: 	 default => '-10.0',
                    313: 	 test => $real_test,
                    314: 	 description => 'minimum x-value shown in plot',
1.38      matthew   315: 	 edit_type   => 'entry',
                    316: 	 size        => '10'
1.20      matthew   317: 	 },
                    318:      xmax      => {
                    319: 	 default => ' 10.0',
                    320: 	 test => $real_test,
                    321: 	 description => 'maximum x-value shown in plot',	 
1.38      matthew   322: 	 edit_type   => 'entry',
                    323: 	 size        => '10'
1.20      matthew   324: 	 },
                    325:      ymin      => {
                    326: 	 default => '-10.0',
                    327: 	 test => $real_test,
                    328: 	 description => 'minimum y-value shown in plot',	 
1.38      matthew   329: 	 edit_type   => 'entry',
                    330: 	 size        => '10'
1.20      matthew   331: 	 },
                    332:      ymax      => {
                    333: 	 default => ' 10.0',
                    334: 	 test => $real_test,
                    335: 	 description => 'maximum y-value shown in plot',	 
1.38      matthew   336: 	 edit_type   => 'entry',
                    337: 	 size        => '10'
1.20      matthew   338: 	 }
1.1       matthew   339:      );
                    340: 
1.48      matthew   341: my $curve_help_text = <<"ENDCURVEHELP";
                    342: The <b>curve</b> tag is where you set the data to be plotted by gnuplot.
                    343: There are two ways of entering the information:
                    344: <dl>
                    345:     <dt> Curve Data
                    346:     <dd> Using a <b>data</b> tag you can specify the numbers used to produce 
                    347: the plot.  
                    348: <p>
                    349: By default, two <b>data</b> tags will be available in a plot.  The
                    350: first will specify X coordinates of the data and the second will
                    351: give the Y coordinates of the data.  When working with a linestyle that 
                    352: requires more than two data sets, inserting another <b>data</b> tag is
                    353: required.  Unfortunately, you must make sure the <b>data</b> tags appear
                    354: in the order gnuplot expects the data.
                    355: </p><p>
                    356: Specifying the data should usually be done with a perl variable or array, 
                    357: such as \@Xdata and \@Ydata.  You may also specify numerical data seperated 
                    358: by commas.  Again, the order of the <b>data</b> tags is important.  The
                    359: first tag will be the X data and the second will be the Y data.
                    360: </p>
                    361:     <dt> Curve Function
                    362:     <dd> The <b>function</b> tag allows you to specify the curve to be 
1.57      matthew   363: plotted as a formula that gnuplot can understand.  <b>Be careful using this
                    364: tag.</b>  It is surprisingly easy to give gnuplot a function it cannot deal
1.48      matthew   365: with properly.  Be explicit: 2*sin(2*3.141592*x/4) will work but
                    366: 2sin(2*3.141592x/4) will not.  If you do not receive any errors in the
                    367: gnuplot data but still do not have an image produced, it is likely there
                    368: is an error in your <b>function</b> tag.
                    369: </dl>
                    370: ENDCURVEHELP
                    371: 
1.62      matthew   372: my @curve_edit_order = ('color','name','linestyle','pointtype','pointsize');
1.60      matthew   373: 
1.1       matthew   374: my %curve_defaults = 
                    375:     (
1.20      matthew   376:      color     => {
                    377: 	 default => 'x000000',
                    378: 	 test => $color_test,
                    379: 	 description => 'color of curve (x000000)',
1.38      matthew   380: 	 edit_type   => 'entry',
                    381: 	 size        => '10'
1.20      matthew   382: 	 },
                    383:      name      => {
                    384: 	 default => '',
                    385: 	 test => $words_test,
                    386: 	 description => 'name of curve to appear in key',
1.38      matthew   387: 	 edit_type   => 'entry',
                    388: 	 size        => '20'
1.20      matthew   389: 	 },
                    390:      linestyle => {
                    391: 	 default => 'lines',
                    392: 	 test => $linestyle_test,
1.35      matthew   393: 	 description => 'Line style',
1.20      matthew   394: 	 edit_type   => 'choice',
1.38      matthew   395: 	 choices     => [keys(%linestyles)]
1.60      matthew   396: 	 },
                    397: # gnuplots term=gif driver does not handle linewidth :(
                    398: #     linewidth => {
                    399: #         default     => 1,
                    400: #         test        => $int_test,
                    401: #         description => 'Line width (may not apply to all line styles)',
                    402: #         edit_type   => 'choice',
                    403: #         choices     => [1,2,3,4,5,6,7,8,9,10]
                    404: #         },
                    405:      pointsize => {
                    406:          default     => 1,
1.62      matthew   407:          test        => $pos_real_test,
                    408:          description => 'point size (may not apply to all line styles)',
                    409:          edit_type   => 'entry',
                    410:          size        => '5'
                    411:          },
                    412:      pointtype => {
                    413:          default     => 1,
1.60      matthew   414:          test        => $int_test,
1.62      matthew   415:          description => 'point type (may not apply to all line styles)',
1.60      matthew   416:          edit_type   => 'choice',
1.62      matthew   417:          choices     => [0,1,2,3,4,5,6]
1.60      matthew   418:          }
1.1       matthew   419:      );
                    420: 
1.21      matthew   421: ###################################################################
                    422: ##                                                               ##
                    423: ##                    parsing and edit rendering                 ##
                    424: ##                                                               ##
                    425: ###################################################################
1.45      matthew   426: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves,%xtics,%ytics);
1.1       matthew   427: 
1.47      matthew   428: sub start_gnuplot {
1.23      matthew   429:     %plot    = ();      %key     = ();      %axis   = (); 
1.10      matthew   430:     $title   = undef;   $xlabel  = undef;   $ylabel = undef;
                    431:     $#labels = -1;      $#curves = -1;
1.45      matthew   432:     %xtics    = ();      %ytics    = ();
1.6       matthew   433:     #
1.1       matthew   434:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    435:     my $result='';
1.25      matthew   436:     &Apache::lonxml::register('Apache::lonplot',
1.45      matthew   437: 	     ('title','xlabel','ylabel','key','axis','label','curve',
                    438: 	      'xtics','ytics'));
1.29      matthew   439:     push (@Apache::lonxml::namespace,'lonplot');
1.51      matthew   440:     if ($target eq 'web' || $target eq 'tex') {
1.47      matthew   441: 	&get_attributes(\%plot,\%gnuplot_defaults,$parstack,$safeeval,
1.17      matthew   442: 			$tagstack->[-1]);
1.20      matthew   443:     } elsif ($target eq 'edit') {
1.47      matthew   444: 	$result .= &Apache::edit::tag_start($target,$token,'GnuPlot');
1.48      matthew   445: 	$result .= &make_javascript();
                    446: 	$result .= &help_win($gnuplot_help_text);
1.47      matthew   447: 	$result .= &edit_attributes($target,$token,\%gnuplot_defaults,
                    448: 				    \@gnuplot_edit_order);
1.20      matthew   449:     } elsif ($target eq 'modified') {
                    450: 	my $constructtag=&Apache::edit::get_new_args
1.47      matthew   451: 	    ($token,$parstack,$safeeval,keys(%gnuplot_defaults));
1.20      matthew   452: 	if ($constructtag) {
                    453: 	    $result = &Apache::edit::rebuild_tag($token);
                    454: 	}
1.4       matthew   455:     }
1.21      matthew   456:     return $result;
1.1       matthew   457: }
                    458: 
1.47      matthew   459: sub end_gnuplot {
1.1       matthew   460:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    461:     pop @Apache::lonxml::namespace;
1.4       matthew   462:     &Apache::lonxml::deregister('Apache::lonplot',
                    463: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    464:     my $result = '';
1.56      albertel  465:     my $randnumber;
                    466:     # need to call rand everytime start_script would evaluate, as the
                    467:     # safe space rand number generator and the global rand generator 
                    468:     # are not seperate
                    469:     if ($target eq 'web' || $target eq 'tex' || $target eq 'grade' ||
                    470: 	$target eq 'answer') {
                    471:       $randnumber=int(rand(1000));
                    472:     }
1.51      matthew   473:     if ($target eq 'web' || $target eq 'tex') {
1.21      matthew   474: 	&check_inputs(); # Make sure we have all the data we need
1.13      matthew   475: 	##
                    476: 	## Determine filename
1.4       matthew   477: 	my $tmpdir = '/home/httpd/perl/tmp/';
1.12      matthew   478: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.56      albertel  479: 	    '_'.time.'_'.$$.$randnumber.'_plot.data';
1.4       matthew   480: 	## Write the plot description to the file
1.51      matthew   481: 	&write_gnuplot_file($tmpdir,$filename,$target);
1.54      matthew   482: 	$filename = &Apache::lonnet::escape($filename);
1.4       matthew   483: 	## return image tag for the plot
1.51      matthew   484: 	if ($target eq 'web') {
                    485: 	    $result .= <<"ENDIMAGE";
1.53      matthew   486: <img src    = "/cgi-bin/plot.gif?file=$filename&output=gif" 
1.16      matthew   487:      width  = "$plot{'width'}" 
                    488:      height = "$plot{'height'}"
                    489:      align  = "$plot{'align'}"
1.47      matthew   490:      alt    = "image should be /cgi-bin/plot.gif?$filename" />
1.12      matthew   491: ENDIMAGE
1.51      matthew   492:         } elsif ($target eq 'tex') {
1.53      matthew   493: 	    &Apache::lonnet::ssi('cgi-bin/plot.gif?file=$filename'.
                    494: 				 '&output=eps');
1.51      matthew   495: 	    $result = "$filename.eps";
                    496: 	}
1.20      matthew   497:     } elsif ($target eq 'edit') {
1.21      matthew   498: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   499:     }
1.1       matthew   500:     return $result;
                    501: }
1.2       matthew   502: 
1.45      matthew   503: 
                    504: ##--------------------------------------------------------------- xtics
                    505: sub start_xtics {
                    506:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    507:     my $result='';
1.51      matthew   508:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   509: 	&get_attributes(\%xtics,\%tic_defaults,$parstack,$safeeval,
                    510: 		    $tagstack->[-1]);
                    511:     } elsif ($target eq 'edit') {
                    512: 	$result .= &Apache::edit::tag_start($target,$token,'xtics');
                    513: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    514: 				    \@tic_edit_order);
                    515:     } elsif ($target eq 'modified') {
                    516: 	my $constructtag=&Apache::edit::get_new_args
                    517: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    518: 	if ($constructtag) {
                    519: 	    $result = &Apache::edit::rebuild_tag($token);
                    520: 	}
                    521:     }
                    522:     return $result;
                    523: }
                    524: 
                    525: sub end_xtics {
                    526:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    527:     my $result = '';
1.51      matthew   528:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   529:     } elsif ($target eq 'edit') {
                    530: 	$result.=&Apache::edit::tag_end($target,$token);
                    531:     }
                    532:     return $result;
                    533: }
                    534: 
                    535: ##--------------------------------------------------------------- ytics
                    536: sub start_ytics {
                    537:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    538:     my $result='';
1.51      matthew   539:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   540: 	&get_attributes(\%ytics,\%tic_defaults,$parstack,$safeeval,
                    541: 		    $tagstack->[-1]);
                    542:     } elsif ($target eq 'edit') {
                    543: 	$result .= &Apache::edit::tag_start($target,$token,'ytics');
                    544: 	$result .= &edit_attributes($target,$token,\%tic_defaults,
                    545: 				    \@tic_edit_order);
                    546:     } elsif ($target eq 'modified') {
                    547: 	my $constructtag=&Apache::edit::get_new_args
                    548: 	    ($token,$parstack,$safeeval,keys(%tic_defaults));
                    549: 	if ($constructtag) {
                    550: 	    $result = &Apache::edit::rebuild_tag($token);
                    551: 	}
                    552:     }
                    553:     return $result;
                    554: }
                    555: 
                    556: sub end_ytics {
                    557:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    558:     my $result = '';
1.51      matthew   559:     if ($target eq 'web' || $target eq 'tex') {
1.45      matthew   560:     } elsif ($target eq 'edit') {
                    561: 	$result.=&Apache::edit::tag_end($target,$token);
                    562:     }
                    563:     return $result;
                    564: }
                    565: 
                    566: 
1.1       matthew   567: ##----------------------------------------------------------------- key
                    568: sub start_key {
                    569:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    570:     my $result='';
1.51      matthew   571:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   572: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   573: 		    $tagstack->[-1]);
1.20      matthew   574:     } elsif ($target eq 'edit') {
1.25      matthew   575: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Key');
1.21      matthew   576: 	$result .= &edit_attributes($target,$token,\%key_defaults);
1.20      matthew   577:     } elsif ($target eq 'modified') {
                    578: 	my $constructtag=&Apache::edit::get_new_args
1.24      matthew   579: 	    ($token,$parstack,$safeeval,keys(%key_defaults));
1.20      matthew   580: 	if ($constructtag) {
                    581: 	    $result = &Apache::edit::rebuild_tag($token);
                    582: 	}
1.4       matthew   583:     }
1.1       matthew   584:     return $result;
                    585: }
                    586: 
                    587: sub end_key {
                    588:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    589:     my $result = '';
1.51      matthew   590:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   591:     } elsif ($target eq 'edit') {
1.21      matthew   592: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   593:     }
1.1       matthew   594:     return $result;
                    595: }
1.21      matthew   596: 
1.1       matthew   597: ##------------------------------------------------------------------- title
                    598: sub start_title {
                    599:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    600:     my $result='';
1.51      matthew   601:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   602: 	$title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.58      matthew   603: 	$title=&Apache::run::evaluate($title,$safeeval,$$parstack[-1]);
1.49      matthew   604: 	$title =~ s/\n/ /g;
1.32      matthew   605: 	if (length($title) > $max_str_len) {
                    606: 	    $title = substr($title,0,$max_str_len);
                    607: 	}
1.20      matthew   608:     } elsif ($target eq 'edit') {
1.25      matthew   609: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
1.22      matthew   610: 	my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.39      matthew   611: 	$result.=&Apache::edit::end_row().
                    612: 	    &Apache::edit::start_spanning_row().
1.63    ! albertel  613: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   614:     } elsif ($target eq 'modified') {
1.29      matthew   615: 	my $text=$$parser[-1]->get_text("/title");
1.42      matthew   616: 	$result.=&Apache::edit::rebuild_tag($token);
1.21      matthew   617: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   618:     }
1.1       matthew   619:     return $result;
                    620: }
                    621: 
                    622: sub end_title {
                    623:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    624:     my $result = '';
1.51      matthew   625:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   626:     } elsif ($target eq 'edit') {
1.27      matthew   627: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   628:     }
1.1       matthew   629:     return $result;
                    630: }
                    631: ##------------------------------------------------------------------- xlabel
                    632: sub start_xlabel {
                    633:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    634:     my $result='';
1.51      matthew   635:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   636: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.58      matthew   637: 	$xlabel=&Apache::run::evaluate($xlabel,$safeeval,$$parstack[-1]);
1.49      matthew   638: 	$xlabel =~ s/\n/ /g;
1.32      matthew   639: 	if (length($xlabel) > $max_str_len) {
                    640: 	    $xlabel = substr($xlabel,0,$max_str_len);
                    641: 	}
1.20      matthew   642:     } elsif ($target eq 'edit') {
1.25      matthew   643: 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
1.22      matthew   644: 	my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.39      matthew   645: 	$result.=&Apache::edit::end_row().
                    646: 	    &Apache::edit::start_spanning_row().
1.63    ! albertel  647: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   648:     } elsif ($target eq 'modified') {
1.29      matthew   649: 	my $text=$$parser[-1]->get_text("/xlabel");
1.42      matthew   650: 	$result.=&Apache::edit::rebuild_tag($token);	
1.21      matthew   651: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   652:     }
1.1       matthew   653:     return $result;
                    654: }
                    655: 
                    656: sub end_xlabel {
                    657:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    658:     my $result = '';
1.51      matthew   659:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   660:     } elsif ($target eq 'edit') {
1.27      matthew   661: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   662:     }
1.1       matthew   663:     return $result;
                    664: }
1.21      matthew   665: 
1.1       matthew   666: ##------------------------------------------------------------------- ylabel
                    667: sub start_ylabel {
                    668:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    669:     my $result='';
1.51      matthew   670:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   671: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.58      matthew   672: 	$ylabel = &Apache::run::evaluate($ylabel,$safeeval,$$parstack[-1]);
1.49      matthew   673: 	$ylabel =~ s/\n/ /g;
1.32      matthew   674: 	if (length($ylabel) > $max_str_len) {
                    675: 	    $ylabel = substr($ylabel,0,$max_str_len);
                    676: 	}
1.20      matthew   677:     } elsif ($target eq 'edit') {
1.25      matthew   678: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
1.22      matthew   679: 	my $text = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.39      matthew   680: 	$result .= &Apache::edit::end_row().
                    681: 	    &Apache::edit::start_spanning_row().
1.63    ! albertel  682: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   683:     } elsif ($target eq 'modified') {
1.29      matthew   684: 	my $text=$$parser[-1]->get_text("/ylabel");
1.42      matthew   685: 	$result.=&Apache::edit::rebuild_tag($token);
1.21      matthew   686: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   687:     }
1.1       matthew   688:     return $result;
                    689: }
                    690: 
                    691: sub end_ylabel {
                    692:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    693:     my $result = '';
1.51      matthew   694:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   695:     } elsif ($target eq 'edit') {
1.27      matthew   696: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   697:     }
1.1       matthew   698:     return $result;
                    699: }
1.21      matthew   700: 
1.1       matthew   701: ##------------------------------------------------------------------- label
                    702: sub start_label {
                    703:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    704:     my $result='';
1.51      matthew   705:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   706: 	my %label;
                    707: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew   708: 		    $tagstack->[-1]);
1.32      matthew   709: 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
1.58      matthew   710: 	$text = &Apache::run::evaluate($text,$safeeval,$$parstack[-1]);
1.49      matthew   711: 	$text =~ s/\n/ /g;
1.32      matthew   712: 	$text = substr($text,0,$max_str_len) if (length($text) > $max_str_len);
                    713: 	$label{'text'} = $text;
1.17      matthew   714: 	push(@labels,\%label);
1.20      matthew   715:     } elsif ($target eq 'edit') {
1.25      matthew   716: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
1.21      matthew   717: 	$result .= &edit_attributes($target,$token,\%label_defaults);
1.22      matthew   718: 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
1.39      matthew   719: 	$result .= &Apache::edit::end_row().
                    720: 	    &Apache::edit::start_spanning_row().
1.63    ! albertel  721: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   722:     } elsif ($target eq 'modified') {
1.42      matthew   723: 	&Apache::edit::get_new_args
1.24      matthew   724: 	    ($token,$parstack,$safeeval,keys(%label_defaults));
1.42      matthew   725: 	$result.=&Apache::edit::rebuild_tag($token);
1.22      matthew   726: 	my $text=$$parser[-1]->get_text("/label");
1.21      matthew   727: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   728:     }
1.1       matthew   729:     return $result;
                    730: }
                    731: 
                    732: sub end_label {
                    733:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    734:     my $result = '';
1.51      matthew   735:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   736:     } elsif ($target eq 'edit') {
1.21      matthew   737: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   738:     }
1.1       matthew   739:     return $result;
                    740: }
                    741: 
                    742: ##------------------------------------------------------------------- curve
                    743: sub start_curve {
                    744:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    745:     my $result='';
1.25      matthew   746:     &Apache::lonxml::register('Apache::lonplot',('function','data'));
                    747:     push (@Apache::lonxml::namespace,'curve');
1.51      matthew   748:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   749: 	my %curve;
                    750: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew   751: 		    $tagstack->[-1]);
1.17      matthew   752: 	push (@curves,\%curve);
1.20      matthew   753:     } elsif ($target eq 'edit') {
1.26      matthew   754: 	$result .= &Apache::edit::tag_start($target,$token,'Curve');
1.48      matthew   755: 	$result .= &help_win($curve_help_text);
1.60      matthew   756: 	$result .= &edit_attributes($target,$token,\%curve_defaults,
                    757:                                     \@curve_edit_order);
1.20      matthew   758:     } elsif ($target eq 'modified') {
                    759: 	my $constructtag=&Apache::edit::get_new_args
1.35      matthew   760: 	    ($token,$parstack,$safeeval,keys(%curve_defaults));
1.20      matthew   761: 	if ($constructtag) {
                    762: 	    $result = &Apache::edit::rebuild_tag($token);
                    763: 	    $result.= &Apache::edit::handle_insert();
                    764: 	}
1.4       matthew   765:     }
1.1       matthew   766:     return $result;
                    767: }
                    768: 
                    769: sub end_curve {
                    770:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    771:     my $result = '';
1.25      matthew   772:     pop @Apache::lonxml::namespace;
                    773:     &Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.51      matthew   774:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   775:     } elsif ($target eq 'edit') {
1.21      matthew   776: 	$result.=&Apache::edit::tag_end($target,$token);
1.4       matthew   777:     }
1.1       matthew   778:     return $result;
                    779: }
1.21      matthew   780: 
1.1       matthew   781: ##------------------------------------------------------------ curve function
                    782: sub start_function {
                    783:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    784:     my $result='';
1.51      matthew   785:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   786: 	if (exists($curves[-1]->{'data'})) {
                    787: 	    &Apache::lonxml::warning('Use of <function> precludes use of <data>.  The <data> will be omitted in favor of the <function> declaration.');
                    788: 	    delete $curves[-1]->{'data'} ;
                    789: 	}
1.58      matthew   790:         my $function = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
                    791: 	$function = &Apache::run::evaluate($function,$safeeval,$$parstack[-1]);
                    792: 	$curves[-1]->{'function'} = $function; 
1.20      matthew   793:     } elsif ($target eq 'edit') {
1.37      matthew   794: 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
1.22      matthew   795: 	my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.39      matthew   796: 	$result .= &Apache::edit::end_row().
                    797: 	    &Apache::edit::start_spanning_row().
1.63    ! albertel  798: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   799:     } elsif ($target eq 'modified') {
1.42      matthew   800: 	$result.=&Apache::edit::rebuild_tag($token);
1.20      matthew   801: 	my $text=$$parser[-1]->get_text("/function");
                    802: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   803:     }
1.1       matthew   804:     return $result;
                    805: }
                    806: 
                    807: sub end_function {
                    808:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    809:     my $result = '';
1.51      matthew   810:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   811:     } elsif ($target eq 'edit') {
1.26      matthew   812: 	$result .= &Apache::edit::end_table();
1.4       matthew   813:     }
1.1       matthew   814:     return $result;
                    815: }
1.21      matthew   816: 
1.1       matthew   817: ##------------------------------------------------------------ curve  data
                    818: sub start_data {
                    819:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    820:     my $result='';
1.51      matthew   821:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   822: 	if (exists($curves[-1]->{'function'})) {
                    823: 	    &Apache::lonxml::warning('Use of <data> precludes use of .'.
                    824: 	    '<function>.  The <function> will be omitted in favor of '.
                    825:             'the <data> declaration.');
                    826: 	    delete($curves[-1]->{'function'});
                    827: 	}
                    828: 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
1.58      matthew   829: 	$datatext=&Apache::run::evaluate($datatext,$safeeval,$$parstack[-1]);
1.40      matthew   830: 	# Deal with cases where we're given an array...
                    831: 	if ($datatext =~ /^\@/) {
                    832: 	    $datatext = &Apache::run::run('return "'.$datatext.'"',
                    833: 					  $safeeval,1);
                    834: 	}
1.49      matthew   835: 	$datatext =~ s/\s+/ /g;
1.17      matthew   836: 	# Need to do some error checking on the @data array - 
                    837: 	# make sure it's all numbers and make sure each array 
                    838: 	# is of the same length.
                    839: 	my @data;
1.35      matthew   840: 	if ($datatext =~ /,/) { # comma deliminated
1.17      matthew   841: 	    @data = split /,/,$datatext;
                    842: 	} else { # Assume it's space seperated.
                    843: 	    @data = split / /,$datatext;
                    844: 	}
                    845: 	for (my $i=0;$i<=$#data;$i++) {
                    846: 	    # Check that it's non-empty
1.19      matthew   847: 	    if (! defined($data[$i])) {
                    848: 		&Apache::lonxml::warning(
                    849: 		    'undefined <data> value.  Replacing with '.
                    850: 		    ' pi/e = 1.15572734979092');
                    851: 		$data[$i] = 1.15572734979092;
                    852: 	    }
1.17      matthew   853: 	    # Check that it's a number
1.19      matthew   854: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
                    855: 		&Apache::lonxml::warning(
                    856: 		    'Bad <data> value of '.$data[$i].'  Replacing with '.
                    857: 		    ' pi/e = 1.15572734979092');
                    858: 		$data[$i] = 1.15572734979092;
                    859: 	    }
1.17      matthew   860: 	}
1.35      matthew   861: 	# complain if the number of data points is not the same as
                    862: 	# in previous sets of data.
1.36      matthew   863: 	if (($curves[-1]->{'data'}) && ($#data != $#{@{$curves[-1]->{'data'}->[0]}})){
1.35      matthew   864: 	    &Apache::lonxml::warning
                    865: 		('Number of data points is not consistent with previous '.
                    866: 		 'number of data points');
                    867: 	}
1.17      matthew   868: 	push  @{$curves[-1]->{'data'}},\@data;
1.20      matthew   869:     } elsif ($target eq 'edit') {
1.37      matthew   870: 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
1.22      matthew   871: 	my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
1.39      matthew   872: 	$result .= &Apache::edit::end_row().
                    873: 	    &Apache::edit::start_spanning_row().
1.63    ! albertel  874: 	    &Apache::edit::editline('',$text,'',60);
1.20      matthew   875:     } elsif ($target eq 'modified') {
1.42      matthew   876: 	$result.=&Apache::edit::rebuild_tag($token);
1.21      matthew   877: 	my $text=$$parser[-1]->get_text("/data");
                    878: 	$result.=&Apache::edit::modifiedfield($token);
1.4       matthew   879:     }
1.1       matthew   880:     return $result;
                    881: }
                    882: 
                    883: sub end_data {
                    884:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    885:     my $result = '';
1.51      matthew   886:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   887:     } elsif ($target eq 'edit') {
1.26      matthew   888: 	$result .= &Apache::edit::end_table();
1.4       matthew   889:     }
1.1       matthew   890:     return $result;
                    891: }
                    892: 
                    893: ##------------------------------------------------------------------- axis
                    894: sub start_axis {
                    895:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    896:     my $result='';
1.51      matthew   897:     if ($target eq 'web' || $target eq 'tex') {
1.17      matthew   898: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                    899: 			$tagstack->[-1]);
1.20      matthew   900:     } elsif ($target eq 'edit') {
1.25      matthew   901: 	$result .= &Apache::edit::tag_start($target,$token,'Plot Axes');
1.21      matthew   902: 	$result .= &edit_attributes($target,$token,\%axis_defaults);
1.20      matthew   903:     } elsif ($target eq 'modified') {
1.29      matthew   904: 	my $constructtag=&Apache::edit::get_new_args
                    905: 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
                    906: 	if ($constructtag) {
                    907: 	    $result = &Apache::edit::rebuild_tag($token);
                    908: 	}
1.4       matthew   909:     }
1.1       matthew   910:     return $result;
                    911: }
                    912: 
                    913: sub end_axis {
                    914:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    915:     my $result = '';
1.51      matthew   916:     if ($target eq 'web' || $target eq 'tex') {
1.20      matthew   917:     } elsif ($target eq 'edit') {
1.21      matthew   918: 	$result.=&Apache::edit::tag_end($target,$token);
1.20      matthew   919:     } elsif ($target eq 'modified') {
1.4       matthew   920:     }
1.1       matthew   921:     return $result;
                    922: }
                    923: 
1.21      matthew   924: ###################################################################
                    925: ##                                                               ##
                    926: ##        Utility Functions                                      ##
                    927: ##                                                               ##
                    928: ###################################################################
                    929: 
1.13      matthew   930: ##----------------------------------------------------------- set_defaults
                    931: sub set_defaults {
1.21      matthew   932:     my ($var,$defaults) = @_;
1.13      matthew   933:     my $key;
1.24      matthew   934:     foreach $key (keys(%$defaults)) {
1.13      matthew   935: 	$var->{$key} = $defaults->{$key}->{'default'};
                    936:     }
                    937: }
                    938: 
1.1       matthew   939: ##------------------------------------------------------------------- misc
1.2       matthew   940: sub get_attributes{
1.21      matthew   941:     my ($values,$defaults,$parstack,$safeeval,$tag) = @_;
1.24      matthew   942:     foreach my $attr (keys(%{$defaults})) {
1.10      matthew   943: 	$values->{$attr} = 
1.15      matthew   944: 	    &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1.10      matthew   945: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew   946: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew   947: 	    next;
                    948: 	}
1.10      matthew   949: 	my $test = $defaults->{$attr}->{'test'};
                    950: 	if (! &$test($values->{$attr})) {
1.6       matthew   951: 	    &Apache::lonxml::warning
                    952: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew   953: 		 .$defaults->{$attr}->{'default'} );
                    954: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew   955: 	}
1.2       matthew   956:     }
1.11      matthew   957:     return ;
1.6       matthew   958: }
1.40      matthew   959: 
1.15      matthew   960: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew   961: sub write_gnuplot_file {
1.51      matthew   962:     my ($tmpdir,$filename,$target)= @_;
1.6       matthew   963:     my $gnuplot_input = '';
1.10      matthew   964:     my $curve;
1.6       matthew   965:     # Collect all the colors
                    966:     my @Colors;
                    967:     push @Colors, $plot{'bgcolor'};
                    968:     push @Colors, $plot{'fgcolor'}; 
1.13      matthew   969:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9       matthew   970:     foreach $curve (@curves) {
                    971: 	push @Colors, ($curve->{'color'} ne '' ? 
                    972: 		       $curve->{'color'}       : 
1.13      matthew   973: 		       $plot{'fgcolor'}        );
1.6       matthew   974:     }
                    975:     # set term
1.51      matthew   976:     if ($target eq 'web') {
                    977: 	$gnuplot_input .= 'set term gif ';
                    978: 	$gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
                    979: 	$gnuplot_input .= $plot{'font'} . ' ';
                    980: 	$gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
                    981: 	$gnuplot_input .= "@Colors\n";
                    982: 	# set output
                    983: 	$gnuplot_input .= "set output\n";
                    984:     } elsif ($target eq 'tex') {
                    985: 	$gnuplot_input .= "set term postscript eps monochrome\n";
                    986: 	$gnuplot_input .= "set output \"$filename.eps\"\n";
                    987:     }
1.7       matthew   988:     # grid
1.10      matthew   989:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7       matthew   990:     # border
1.9       matthew   991:     $gnuplot_input .= ($plot{'border'} eq 'on'?
                    992: 		       'set border'.$/           :
                    993: 		       'set noborder'.$/         );    # title, xlabel, ylabel
1.45      matthew   994:     # titles
1.13      matthew   995:     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;
                    996:     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
                    997:     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
1.45      matthew   998:     # tics
                    999:     if (%xtics) {    
                   1000: 	$gnuplot_input .= "set xtics $xtics{'location'} ";
1.46      matthew  1001: 	$gnuplot_input .= ( $xtics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1002: 	$gnuplot_input .= "$xtics{'start'}, ";
                   1003: 	$gnuplot_input .= "$xtics{'increment'}, ";
                   1004: 	$gnuplot_input .= "$xtics{'end'}\n";
                   1005:     }
                   1006:     if (%ytics) {    
                   1007: 	$gnuplot_input .= "set ytics $ytics{'location'} ";
1.46      matthew  1008: 	$gnuplot_input .= ( $ytics{'mirror'} eq 'on'?"mirror ":"nomirror ");
1.45      matthew  1009: 	$gnuplot_input .= "$ytics{'start'}, ";
                   1010: 	$gnuplot_input .= "$ytics{'increment'}, ";
                   1011:         $gnuplot_input .= "$ytics{'end'}\n";
                   1012:     }
                   1013:     # axis
1.23      matthew  1014:     if (%axis) {
1.13      matthew  1015: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                   1016: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6       matthew  1017:     }
                   1018:     # Key
1.23      matthew  1019:     if (%key) {
1.9       matthew  1020: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                   1021: 	if ($key{'title'} ne '') {
1.43      matthew  1022: 	    $gnuplot_input .= 'title " '.$key{'title'}.'" ';
1.11      matthew  1023: 	} 
                   1024: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew  1025:     } else {
1.9       matthew  1026: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew  1027:     }
1.6       matthew  1028:     # labels
1.10      matthew  1029:     my $label;
1.6       matthew  1030:     foreach $label (@labels) {
                   1031: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.9       matthew  1032: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
1.6       matthew  1033:     }
                   1034:     # curves
                   1035:     $gnuplot_input .= 'plot ';
1.9       matthew  1036:     for (my $i = 0;$i<=$#curves;$i++) {
                   1037: 	$curve = $curves[$i];
                   1038: 	$gnuplot_input.= ', ' if ($i > 0);
1.6       matthew  1039: 	if (exists($curve->{'function'})) {
1.9       matthew  1040: 	    $gnuplot_input.= 
                   1041: 		$curve->{'function'}.' title "'.
                   1042: 		$curve->{'name'}.'" with '.
                   1043: 		$curve->{'linestyle'};
1.60      matthew  1044: #
                   1045: # gnuplot's term=gif driver does not handle linewidths :(
                   1046: # . ' linewidth '. $curve->{'linewidth'};
                   1047: #
                   1048:             if (($curve->{'linestyle'} eq 'points')      ||
                   1049:                 ($curve->{'linestyle'} eq 'linespoints') ||
                   1050:                 ($curve->{'linestyle'} eq 'errorbars')   ||
                   1051:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
                   1052:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
                   1053:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
1.62      matthew  1054:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
1.60      matthew  1055:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
                   1056:             }
1.6       matthew  1057: 	} elsif (exists($curve->{'data'})) {
1.40      matthew  1058: 	    # Store data values in $datatext
                   1059: 	    my $datatext = '';
                   1060: 	    #   get new filename
                   1061: 	    my $datafilename = "$tmpdir/$filename.$i";
                   1062: 	    my $fh=Apache::File->new(">$datafilename");
                   1063: 	    # Compile data
1.6       matthew  1064: 	    my @Data = @{$curve->{'data'}};
1.9       matthew  1065: 	    my @Data0 = @{$Data[0]};
                   1066: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew  1067: 		my $dataset;
1.6       matthew  1068: 		foreach $dataset (@Data) {
1.9       matthew  1069: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew  1070: 		}
1.9       matthew  1071: 		$datatext .= $/;
1.6       matthew  1072: 	    }
1.40      matthew  1073: 	    #   write file
                   1074: 	    print $fh $datatext;
                   1075: 	    close ($fh);
                   1076: 	    #   generate gnuplot text
                   1077: 	    $gnuplot_input.= '"'.$datafilename.'" title "'.
                   1078: 		$curve->{'name'}.'" with '.
                   1079: 		$curve->{'linestyle'};
1.62      matthew  1080:             if (($curve->{'linestyle'} eq 'points')      ||
                   1081:                 ($curve->{'linestyle'} eq 'linespoints') ||
                   1082:                 ($curve->{'linestyle'} eq 'errorbars')   ||
                   1083:                 ($curve->{'linestyle'} eq 'xerrorbars')  ||
                   1084:                 ($curve->{'linestyle'} eq 'yerrorbars')  ||
                   1085:                 ($curve->{'linestyle'} eq 'xyerrorbars')) {
                   1086:                 $gnuplot_input.=' pointtype '.$curve->{'pointtype'};
                   1087:                 $gnuplot_input.=' pointsize '.$curve->{'pointsize'};
                   1088:             }
1.6       matthew  1089: 	}
                   1090:     }
1.40      matthew  1091:     # Write the output to a file.
                   1092:     my $fh=Apache::File->new(">$tmpdir$filename");
                   1093:     print $fh $gnuplot_input;
                   1094:     close($fh);
                   1095:     # That's all folks.
                   1096:     return ;
1.2       matthew  1097: }
1.21      matthew  1098: 
                   1099: #---------------------------------------------- check_inputs
                   1100: sub check_inputs {
                   1101:     ## Note: no inputs, no outputs - this acts only on global variables.
                   1102:     ## Make sure we have all the input we need:
1.47      matthew  1103:     if (! %plot) { &set_defaults(\%plot,\%gnuplot_defaults); }
1.23      matthew  1104:     if (! %key ) {} # No key for this plot, thats okay
1.34      matthew  1105: #    if (! %axis) { &set_defaults(\%axis,\%axis_defaults); }
1.21      matthew  1106:     if (! defined($title )) {} # No title for this plot, thats okay
                   1107:     if (! defined($xlabel)) {} # No xlabel for this plot, thats okay
                   1108:     if (! defined($ylabel)) {} # No ylabel for this plot, thats okay
                   1109:     if ($#labels < 0) { }      # No labels for this plot, thats okay
                   1110:     if ($#curves < 0) { 
                   1111: 	&Apache::lonxml::warning("No curves specified for plot!!!!");
                   1112: 	return '';
                   1113:     }
                   1114:     my $curve;
                   1115:     foreach $curve (@curves) {
                   1116: 	if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
                   1117: 	    &Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
                   1118: 	    return '';
                   1119: 	}
                   1120:     }
                   1121: }
                   1122: 
1.20      matthew  1123: #------------------------------------------------ make_edit
                   1124: sub edit_attributes {
1.34      matthew  1125:     my ($target,$token,$defaults,$keys) = @_;
                   1126:     my ($result,@keys);
                   1127:     if ($keys && ref($keys) eq 'ARRAY') {
                   1128:         @keys = @$keys;
                   1129:     } else {
                   1130: 	@keys = sort(keys(%$defaults));
                   1131:     }
                   1132:     foreach my $attr (@keys) {
1.35      matthew  1133: 	# append a ' ' to the description if it doesn't have one already.
                   1134: 	my $description = $defaults->{$attr}->{'description'};
                   1135: 	$description .= ' ' if ($description !~ / $/);
1.20      matthew  1136: 	if ($defaults->{$attr}->{'edit_type'} eq 'entry') {
1.35      matthew  1137: 	    $result .= &Apache::edit::text_arg
1.38      matthew  1138: 		($description,$attr,$token,
                   1139: 		 $defaults->{$attr}->{'size'});
1.20      matthew  1140: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'choice') {
1.35      matthew  1141: 	    $result .= &Apache::edit::select_arg
                   1142: 		($description,$attr,$defaults->{$attr}->{'choices'},$token);
1.45      matthew  1143: 	} elsif ($defaults->{$attr}->{'edit_type'} eq 'onoff') {
1.35      matthew  1144: 	    $result .= &Apache::edit::select_arg
                   1145: 		($description,$attr,['on','off'],$token);
1.20      matthew  1146: 	}
1.25      matthew  1147: 	$result .= '<br />';
1.20      matthew  1148:     }
                   1149:     return $result;
                   1150: }
1.1       matthew  1151: 
1.21      matthew  1152: 
                   1153: ###################################################################
                   1154: ##                                                               ##
                   1155: ##           Insertion functions for editing plots               ##
                   1156: ##                                                               ##
                   1157: ###################################################################
                   1158: 
1.47      matthew  1159: sub insert_gnuplot {
1.29      matthew  1160:     my $result = '';
1.20      matthew  1161:     #  plot attributes
1.61      matthew  1162:     $result .= "\n<gnuplot ";
1.47      matthew  1163:     foreach my $attr (keys(%gnuplot_defaults)) {
1.61      matthew  1164: 	$result .= "\n     $attr=\"$gnuplot_defaults{$attr}->{'default'}\"";
1.20      matthew  1165:     }
1.61      matthew  1166:     $result .= ">";
1.47      matthew  1167:     # Add the components (most are commented out for simplicity)
1.44      matthew  1168:     # $result .= &insert_key();
                   1169:     # $result .= &insert_axis();
                   1170:     # $result .= &insert_title();    
                   1171:     # $result .= &insert_xlabel();    
                   1172:     # $result .= &insert_ylabel();    
1.20      matthew  1173:     $result .= &insert_curve();
1.50      matthew  1174:     # close up the <gnuplot>
1.61      matthew  1175:     $result .= "\n</gnuplot>";
1.45      matthew  1176:     return $result;
                   1177: }
                   1178: 
1.46      matthew  1179: sub insert_tics {
                   1180:     my $result;
                   1181:     $result .= &insert_xtics() . &insert_ytics;
                   1182:     return $result;
                   1183: }
                   1184: 
1.45      matthew  1185: sub insert_xtics {
                   1186:     my $result;
1.46      matthew  1187:     $result .= "\n    <xtics ";
1.45      matthew  1188:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  1189: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1190:     }
1.61      matthew  1191:     $result .= "/>";
1.45      matthew  1192:     return $result;
                   1193: }
                   1194: 
                   1195: sub insert_ytics {
                   1196:     my $result;
1.46      matthew  1197:     $result .= "\n    <ytics ";
1.45      matthew  1198:     foreach my $attr (keys(%tic_defaults)) {
1.61      matthew  1199: 	$result .= "\n        $attr=\"$tic_defaults{$attr}->{'default'}\" ";
1.45      matthew  1200:     }
1.61      matthew  1201:     $result .= "/>";
1.20      matthew  1202:     return $result;
                   1203: }
                   1204: 
                   1205: sub insert_key {
                   1206:     my $result;
1.61      matthew  1207:     $result .= "\n    <key ";
1.30      matthew  1208:     foreach my $attr (keys(%key_defaults)) {
1.61      matthew  1209: 	$result .= "\n         $attr=\"$key_defaults{$attr}->{'default'}\"";
1.20      matthew  1210:     }
1.61      matthew  1211:     $result .= " />";
1.20      matthew  1212:     return $result;
                   1213: }
                   1214: 
                   1215: sub insert_axis{
                   1216:     my $result;
1.46      matthew  1217:     $result .= "\n    <axis ";
1.30      matthew  1218:    foreach my $attr (keys(%axis_defaults)) {
1.61      matthew  1219: 	$result .= "\n         $attr=\"$axis_defaults{$attr}->{'default'}\"";
1.20      matthew  1220:     }
1.61      matthew  1221:     $result .= " />";
1.20      matthew  1222:     return $result;
                   1223: }
1.28      matthew  1224: 
1.61      matthew  1225: sub insert_title  { return "\n    <title></title>"; }
                   1226: sub insert_xlabel { return "\n    <xlabel></xlabel>"; }
                   1227: sub insert_ylabel { return "\n    <ylabel></ylabel>"; }
1.20      matthew  1228: 
                   1229: sub insert_label {
                   1230:     my $result;
1.46      matthew  1231:     $result .= "\n    <label ";
1.30      matthew  1232:     foreach my $attr (keys(%label_defaults)) {
1.61      matthew  1233: 	$result .= "\n         $attr=\"".
                   1234:             $label_defaults{$attr}->{'default'}."\"";
1.20      matthew  1235:     }
1.61      matthew  1236:     $result .= "></label>";
1.20      matthew  1237:     return $result;
                   1238: }
                   1239: 
                   1240: sub insert_curve {
                   1241:     my $result;
1.41      matthew  1242:     $result .= "\n    <curve ";
1.30      matthew  1243:     foreach my $attr (keys(%curve_defaults)) {
1.61      matthew  1244: 	$result .= "\n         $attr=\"".
                   1245: 	    $curve_defaults{$attr}->{'default'}."\"";
1.20      matthew  1246:     }
1.61      matthew  1247:     $result .= " >";
                   1248:     $result .= &insert_data().&insert_data()."\n    </curve>";
1.20      matthew  1249: }
1.4       matthew  1250: 
1.20      matthew  1251: sub insert_function {
                   1252:     my $result;
1.61      matthew  1253:     $result .= "\n        <function></function>";
1.20      matthew  1254:     return $result;
                   1255: }
1.4       matthew  1256: 
1.20      matthew  1257: sub insert_data {
                   1258:     my $result;
1.61      matthew  1259:     $result .= "\n        <data></data>";
1.20      matthew  1260:     return $result;
                   1261: }
1.4       matthew  1262: 
1.48      matthew  1263: ##----------------------------------------------------------------------
                   1264: # Javascript functions to display help for tags
                   1265: 
                   1266: sub make_javascript {
                   1267:     my $helpwindowwidth  = 400;
                   1268:     my $helpwindowheight = 400;
                   1269:     my $result = '';
                   1270:     $result.=<<"ENDFUNCTION";
                   1271: <script language="JavaScript">
                   1272: function openWin(text)
                   1273: {
                   1274:   newWin = open("", "new_W", "width=$helpwindowwidth,height=$helpwindowheight,resizable=1,scrollbars=1");
                   1275:   newWin.document.open("text/html", "replace");
                   1276:   newWin.document.writeln(text);
                   1277:   newWin.document.writeln('<center><a href=\"javascript:window.close()\">close this window</a></center>');
                   1278:   newWin.document.close();
                   1279: }
                   1280: </script>
                   1281: ENDFUNCTION
                   1282:     return $result;
                   1283: }
                   1284: 
                   1285: sub help_win {
                   1286:     my ($helptext)=@_;
                   1287:     $helptext =~ s/\n/ /g;
                   1288:     $helptext =~ s/\'/\\\'/g;
                   1289:     my $result = '';
                   1290:     $result.=<<"ENDWIN";
                   1291: <table width="100%"><tr><td align="right">
                   1292: <a href="javascript:openWin('$helptext')">help</a>
                   1293: </td></tr></table><hr />
                   1294: ENDWIN
                   1295:     return $result;
                   1296: }
1.21      matthew  1297: ##----------------------------------------------------------------------
1.20      matthew  1298: 1;
                   1299: __END__
1.4       matthew  1300: 
                   1301: 

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