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

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

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