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

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

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