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

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

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