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

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

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