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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: # Dynamic plot
                      3: #
1.19    ! matthew     4: # $Id: lonplot.pm,v 1.18 2001/12/27 15:00:45 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.14      matthew    29: # 12/17 12/18 12/19 12/20 12/21 Matthew
1.1       matthew    30: package Apache::lonplot;
1.10      matthew    31: 
1.1       matthew    32: use strict;
1.10      matthew    33: use Apache::File;
1.1       matthew    34: use Apache::response;
1.2       matthew    35: use Apache::lonxml;
1.10      matthew    36: 
1.1       matthew    37: sub BEGIN {
                     38:   &Apache::lonxml::register('Apache::lonplot',('plot'));
                     39: }
                     40: 
1.10      matthew    41: ## 
                     42: ## Description of data structures:
                     43: ##
                     44: ##  %plot       %key    %axis
                     45: ## --------------------------
                     46: ##  height      title   color
                     47: ##  width       box     xmin
                     48: ##  bgcolor     pos     xmax
                     49: ##  fgcolor             ymin
                     50: ##  transparent         ymax
                     51: ##  grid
                     52: ##  border
                     53: ##  font
1.19    ! matthew    54: ##  align
1.10      matthew    55: ##
                     56: ##  @labels: $labels[$i] = \%label
                     57: ##           %label: text, xpos, ypos, justify
1.14      matthew    58: ##
1.10      matthew    59: ##  @curves: $curves[$i] = \%curve
1.14      matthew    60: ##           %curve: name, linestyle, ( function | data )
1.10      matthew    61: ##
                     62: ##  $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
                     63: ##                            [y1,y2,y3,y4] ]
                     64: ##
                     65: ##------------------------------------------------------------
1.1       matthew    66: ##
                     67: ## Tests used in checking the validitity of input
                     68: ##
1.11      matthew    69: my $int_test       = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
1.19    ! matthew    70: my $real_test      = 
        !            71:     sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
1.11      matthew    72: my $color_test     = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
1.1       matthew    73: my $onoff_test     = sub {$_[0]=~/^(on|off)$/};
1.15      matthew    74: my $key_pos_test   = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
1.1       matthew    75: my $sml_test       = sub {$_[0]=~/^(small|medium|large)$/};
                     76: my $linestyle_test = sub {$_[0]=~/^(lines|linespoints|dots|points|steps)$/};
1.15      matthew    77: my $words_test     = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
1.1       matthew    78: ##
                     79: ## Default values for attributes of elements
                     80: ##
                     81: my %plot_defaults = 
                     82:     (
1.10      matthew    83:      height       => {default => 200,       test => $int_test   },
                     84:      width        => {default => 200,       test => $int_test   },
                     85:      bgcolor      => {default => 'xffffff', test => $color_test },
                     86:      fgcolor      => {default => 'x000000', test => $color_test },
                     87:      transparent  => {default => 'off',     test => $onoff_test },
                     88:      grid         => {default => 'off',     test => $onoff_test },
                     89:      border       => {default => 'on',      test => $onoff_test },
1.16      matthew    90:      font         => {default => 'medium',  test => $sml_test   },
1.19    ! matthew    91:      align        => {default => 'left',    test => 
        !            92: 			  sub {$_[0]=~/^(left|right|center)$/} }
1.1       matthew    93:      );
                     94: 
                     95: my %key_defaults = 
                     96:     (
1.10      matthew    97:      title => { default => '',          test => $words_test   },
                     98:      box   => { default => 'off',       test => $onoff_test   },
                     99:      pos   => { default => 'top right', test => $key_pos_test }
1.1       matthew   100:      );
                    101: 
                    102: my %label_defaults = 
                    103:     (
1.10      matthew   104:      xpos    => {default => 0,         test => $real_test     },
                    105:      ypos    => {default => 0,         test => $real_test     },
1.5       matthew   106:      justify => {default => 'left',    
1.10      matthew   107:                  test => sub {$_[0]=~/^(left|right|center)$/} }
1.1       matthew   108:      );
                    109: 
                    110: my %axis_defaults = 
                    111:     (
1.5       matthew   112:      color     => {default => 'x000000', test => $color_test},
1.11      matthew   113:      xmin      => {default => '-10.0',   test => $real_test },
                    114:      xmax      => {default => ' 10.0',   test => $real_test },
                    115:      ymin      => {default => '-10.0',   test => $real_test },
1.15      matthew   116:      ymax      => {default => ' 10.0',   test => $real_test },
                    117:      linestyle => {default => 'points',  test => $linestyle_test}
1.1       matthew   118:      );
                    119: 
                    120: my %curve_defaults = 
                    121:     (
1.13      matthew   122:      color     => {default => 'x000000', test => $color_test     },
                    123:      name      => {default => '',        test => $words_test     },
                    124:      linestyle => {default => 'lines',   test => $linestyle_test }
1.1       matthew   125:      );
                    126: 
                    127: ##
                    128: ## End of defaults
                    129: ##
                    130: my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
                    131: 
                    132: sub start_plot {
1.10      matthew   133:     %plot    = undef;   %key     = undef;   %axis   = undef; 
                    134:     $title   = undef;   $xlabel  = undef;   $ylabel = undef;
                    135:     $#labels = -1;      $#curves = -1;
1.6       matthew   136:     #
1.1       matthew   137:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    138:     my $result='';
1.4       matthew   139:     if ($target eq 'web') {
1.17      matthew   140: 	&Apache::lonxml::register('Apache::lonplot',
                    141: 	      ('title','xlabel','ylabel','key','axis','label','curve'));
                    142: 	push (@Apache::lonxml::namespace,'plot');
                    143: 	## Always evaluate the insides of the <plot></plot> tags
                    144: 	my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
                    145: 	$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
                    146: 	&Apache::lonxml::newparser($parser,\$inside);
                    147: 	##-------------------------------------------------------
                    148: 	&get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,
                    149: 			$tagstack->[-1]);
1.4       matthew   150:     }
1.1       matthew   151:     return '';
                    152: }
                    153: 
                    154: sub end_plot {
                    155:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    156:     pop @Apache::lonxml::namespace;
1.4       matthew   157:     &Apache::lonxml::deregister('Apache::lonplot',
                    158: 	('title','xlabel','ylabel','key','axis','label','curve'));
                    159:     my $result = '';
                    160:     if ($target eq 'web') {
1.13      matthew   161: 	##
                    162: 	## Make sure we have all the input we need:
                    163: 	if (! defined(%plot  )) { &set_defaults(\%plot,\%plot_defaults); }
1.14      matthew   164: 	if (! defined(%key   )) {} # No key for this plot
1.13      matthew   165: 	if (! defined(%axis  )) { &set_defaults(\%axis,\%axis_defaults); }
1.14      matthew   166: 	if (! defined($title )) {} # No title for this plot
                    167: 	if (! defined($xlabel)) {} # No xlabel for this plot
                    168: 	if (! defined($ylabel)) {} # No ylabel for this plot
1.13      matthew   169: 	if ($#labels < 0) { } # No labels for this plot
1.14      matthew   170: 	if ($#curves < 0) { 
                    171: 	    &Apache::lonxml::warning("No curves specified for plot!!!!");
                    172: 	    return '';
                    173: 	}
                    174: 	my $curve;
                    175: 	foreach $curve (@curves) {
                    176: 	    if (!defined($curve->{'function'})&&!defined($curve->{'data'})){
                    177: 		&Apache::lonxml::warning("One of the curves specified did not contain any <data> or <function> declarations\n");
                    178: 		return '';
                    179: 	    }
                    180: 	}
1.13      matthew   181: 	##
                    182: 	## Determine filename
1.4       matthew   183: 	my $tmpdir = '/home/httpd/perl/tmp/';
1.12      matthew   184: 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
1.13      matthew   185: 	    '_'.time.'_'.$$.'_plot.data';
1.4       matthew   186: 	## Write the plot description to the file
1.12      matthew   187: 	my $fh=Apache::File->new(">$tmpdir$filename");
                    188: 	print $fh &write_gnuplot_file();
1.14      matthew   189: 	close($fh);
1.4       matthew   190: 	## return image tag for the plot
1.12      matthew   191: 	$result .= <<"ENDIMAGE";
1.16      matthew   192: <img src    = "/cgi-bin/plot.gif?$filename" 
                    193:      width  = "$plot{'width'}" 
                    194:      height = "$plot{'height'}"
                    195:      align  = "$plot{'align'}"
                    196:      alt    = "/cgi-bin/plot.gif?$filename" />
1.12      matthew   197: ENDIMAGE
1.4       matthew   198:     }
1.1       matthew   199:     return $result;
                    200: }
1.2       matthew   201: 
1.1       matthew   202: ##----------------------------------------------------------------- key
                    203: sub start_key {
                    204:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    205:     my $result='';
1.17      matthew   206:     if ($target eq 'web') {
                    207: 	&get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
1.11      matthew   208: 		    $tagstack->[-1]);
1.4       matthew   209: 	# This routine should never return anything.
                    210:     }
1.1       matthew   211:     return $result;
                    212: }
                    213: 
                    214: sub end_key {
                    215:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    216:     my $result = '';
1.4       matthew   217:     if ($target eq 'web') {
                    218: 	# This routine should never return anything.
                    219:     }
1.1       matthew   220:     return $result;
                    221: }
                    222: ##------------------------------------------------------------------- title
                    223: sub start_title {
                    224:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    225:     my $result='';
1.4       matthew   226:     if ($target eq 'web') {
1.17      matthew   227: 	$title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
1.4       matthew   228: 	# This routine should never return anything.
                    229:     }
1.1       matthew   230:     return $result;
                    231: }
                    232: 
                    233: sub end_title {
                    234:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    235:     my $result = '';
1.4       matthew   236:     if ($target eq 'web') {
                    237: 	# This routine should never return anything.
                    238:     }
1.1       matthew   239:     return $result;
                    240: }
                    241: ##------------------------------------------------------------------- xlabel
                    242: sub start_xlabel {
                    243:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    244:     my $result='';
1.4       matthew   245:     if ($target eq 'web') {
1.17      matthew   246: 	$xlabel = &Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
1.4       matthew   247: 	# This routine should never return anything.
                    248:     }
1.1       matthew   249:     return $result;
                    250: }
                    251: 
                    252: sub end_xlabel {
                    253:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    254:     my $result = '';
1.4       matthew   255:     if ($target eq 'web') {
                    256: 	# This routine should never return anything.
                    257:     }
1.1       matthew   258:     return $result;
                    259: }
                    260: ##------------------------------------------------------------------- ylabel
                    261: sub start_ylabel {
                    262:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    263:     my $result='';
1.4       matthew   264:     if ($target eq 'web') {
1.17      matthew   265: 	$ylabel = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
1.4       matthew   266: 	# This routine should never return anything.
                    267:     }
1.1       matthew   268:     return $result;
                    269: }
                    270: 
                    271: sub end_ylabel {
                    272:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    273:     my $result = '';
1.4       matthew   274:     if ($target eq 'web') {
                    275: 	# This routine should never return anything.
                    276:     }
1.1       matthew   277:     return $result;
                    278: }
                    279: ##------------------------------------------------------------------- label
                    280: sub start_label {
                    281:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    282:     my $result='';
1.17      matthew   283:     if ($target eq 'web') {
                    284: 	my %label;
                    285: 	&get_attributes(\%label,\%label_defaults,$parstack,$safeeval,
1.11      matthew   286: 		    $tagstack->[-1]);
1.17      matthew   287: 	$label{'text'} = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
                    288: 	if (! &$words_test($label{'text'})) {
                    289: 	    # I should probably warn about it, too.
                    290: 	    $label{'text'} = 'Illegal text';
                    291: 	}
                    292: 	push(@labels,\%label);
1.4       matthew   293:     }
1.17      matthew   294:     # This routine should never return anything.
1.1       matthew   295:     return $result;
                    296: }
                    297: 
                    298: sub end_label {
                    299:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    300:     my $result = '';
1.4       matthew   301:     if ($target eq 'web') {
                    302: 	# This routine should never return anything.
                    303:     }
1.1       matthew   304:     return $result;
                    305: }
                    306: 
                    307: ##------------------------------------------------------------------- curve
                    308: sub start_curve {
                    309:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    310:     my $result='';
1.17      matthew   311:     if ($target eq 'web') {
                    312: 	my %curve;
                    313: 	&get_attributes(\%curve,\%curve_defaults,$parstack,$safeeval,
1.11      matthew   314: 		    $tagstack->[-1]);
1.17      matthew   315: 	push (@curves,\%curve);
                    316: 	&Apache::lonxml::register('Apache::lonplot',('function','data'));
                    317: 	push (@Apache::lonxml::namespace,'curve');
1.4       matthew   318: 	# This routine should never return anything.
                    319:     }
1.1       matthew   320:     return $result;
                    321: }
                    322: 
                    323: sub end_curve {
                    324:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    325:     my $result = '';
1.4       matthew   326:     if ($target eq 'web') {
1.17      matthew   327: 	pop @Apache::lonxml::namespace;
                    328: 	&Apache::lonxml::deregister('Apache::lonplot',('function','data'));
1.4       matthew   329: 	# This routine should never return anything.
                    330:     }
1.1       matthew   331:     return $result;
                    332: }
                    333: ##------------------------------------------------------------ curve function
                    334: sub start_function {
                    335:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    336:     my $result='';
1.4       matthew   337:     if ($target eq 'web') {
1.17      matthew   338: 	if (exists($curves[-1]->{'data'})) {
                    339: 	    &Apache::lonxml::warning('Use of <function> precludes use of <data>.  The <data> will be omitted in favor of the <function> declaration.');
                    340: 	    delete $curves[-1]->{'data'} ;
                    341: 	}
                    342: 	$curves[-1]->{'function'} = 
                    343: 	    &Apache::lonxml::get_all_text("/function",$$parser[-1]);
1.4       matthew   344: 	# This routine should never return anything.
                    345:     }
1.1       matthew   346:     return $result;
                    347: }
                    348: 
                    349: sub end_function {
                    350:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    351:     my $result = '';
1.4       matthew   352:     if ($target eq 'web') {
                    353: 	# This routine should never return anything.
                    354:     }
1.1       matthew   355:     return $result;
                    356: }
                    357: ##------------------------------------------------------------ curve  data
                    358: sub start_data {
                    359:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    360:     my $result='';
1.4       matthew   361:     if ($target eq 'web') {
1.17      matthew   362: 	if (exists($curves[-1]->{'function'})) {
                    363: 	    &Apache::lonxml::warning('Use of <data> precludes use of .'.
                    364: 	    '<function>.  The <function> will be omitted in favor of '.
                    365:             'the <data> declaration.');
                    366: 	    delete($curves[-1]->{'function'});
                    367: 	}
                    368: 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
                    369: 	$datatext =~ s/\s+/ /g;  
                    370: 	# Need to do some error checking on the @data array - 
                    371: 	# make sure it's all numbers and make sure each array 
                    372: 	# is of the same length.
                    373: 	my @data;
                    374: 	if ($datatext =~ /,/) {
                    375: 	    @data = split /,/,$datatext;
                    376: 	} else { # Assume it's space seperated.
                    377: 	    @data = split / /,$datatext;
                    378: 	}
                    379: 	for (my $i=0;$i<=$#data;$i++) {
                    380: 	    # Check that it's non-empty
1.19    ! matthew   381: 	    if (! defined($data[$i])) {
        !           382: 		&Apache::lonxml::warning(
        !           383: 		    'undefined <data> value.  Replacing with '.
        !           384: 		    ' pi/e = 1.15572734979092');
        !           385: 		$data[$i] = 1.15572734979092;
        !           386: 	    }
1.17      matthew   387: 	    # Check that it's a number
1.19    ! matthew   388: 	    if (! &$real_test($data[$i]) & ! &$int_test($data[$i])) {
        !           389: 		&Apache::lonxml::warning(
        !           390: 		    'Bad <data> value of '.$data[$i].'  Replacing with '.
        !           391: 		    ' pi/e = 1.15572734979092');
        !           392: 		$data[$i] = 1.15572734979092;
        !           393: 	    }
1.17      matthew   394: 	}
                    395: 	push  @{$curves[-1]->{'data'}},\@data;
1.4       matthew   396: 	# This routine should never return anything.
                    397:     }
1.1       matthew   398:     return $result;
                    399: }
                    400: 
                    401: sub end_data {
                    402:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    403:     my $result = '';
1.4       matthew   404:     if ($target eq 'web') {
                    405: 	# This routine should never return anything.
                    406:     }
1.1       matthew   407:     return $result;
                    408: }
                    409: 
                    410: ##------------------------------------------------------------------- axis
                    411: sub start_axis {
                    412:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    413:     my $result='';
1.4       matthew   414:     if ($target eq 'web') {
1.17      matthew   415: 	&get_attributes(\%axis,\%axis_defaults,$parstack,$safeeval,
                    416: 			$tagstack->[-1]);
1.4       matthew   417: 	# This routine should never return anything.
                    418:     }
1.1       matthew   419:     return $result;
                    420: }
                    421: 
                    422: sub end_axis {
                    423:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    424:     my $result = '';
1.4       matthew   425:     if ($target eq 'web') {
                    426: 	# This routine should never return anything.
                    427:     }
1.1       matthew   428:     return $result;
                    429: }
                    430: 
1.13      matthew   431: ##----------------------------------------------------------- set_defaults
                    432: sub set_defaults {
                    433:     my $var      = shift;
                    434:     my $defaults = shift;
                    435:     my $key;
                    436:     foreach $key (keys %$defaults) {
                    437: 	$var->{$key} = $defaults->{$key}->{'default'};
                    438:     }
                    439: }
                    440: 
1.1       matthew   441: ##------------------------------------------------------------------- misc
1.2       matthew   442: sub get_attributes{
1.10      matthew   443:     my $values   = shift;
                    444:     my $defaults = shift;
                    445:     my $parstack = shift;
                    446:     my $safeeval = shift;
                    447:     my $tag      = shift;
1.19    ! matthew   448:     foreach my $attr (keys %{$defaults}) {
1.10      matthew   449: 	$values->{$attr} = 
1.15      matthew   450: 	    &Apache::lonxml::get_param($attr,$parstack,$safeeval);
1.10      matthew   451: 	if ($values->{$attr} eq '' | !defined($values->{$attr})) {
1.11      matthew   452: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.6       matthew   453: 	    next;
                    454: 	}
1.10      matthew   455: 	my $test = $defaults->{$attr}->{'test'};
                    456: 	if (! &$test($values->{$attr})) {
1.6       matthew   457: 	    &Apache::lonxml::warning
                    458: 		($tag.':'.$attr.': Bad value.'.'Replacing your value with : '
1.11      matthew   459: 		 .$defaults->{$attr}->{'default'} );
                    460: 	    $values->{$attr} = $defaults->{$attr}->{'default'};
1.10      matthew   461: 	}
1.2       matthew   462:     }
1.11      matthew   463:     return ;
1.6       matthew   464: }
1.15      matthew   465: ##------------------------------------------------------- write_gnuplot_file
1.6       matthew   466: sub write_gnuplot_file {
                    467:     my $gnuplot_input = '';
1.10      matthew   468:     my $curve;
1.6       matthew   469:     # Collect all the colors
                    470:     my @Colors;
                    471:     push @Colors, $plot{'bgcolor'};
                    472:     push @Colors, $plot{'fgcolor'}; 
1.13      matthew   473:     push @Colors, (defined($axis{'color'})?$axis{'color'}:$plot{'fgcolor'});
1.9       matthew   474:     foreach $curve (@curves) {
                    475: 	push @Colors, ($curve->{'color'} ne '' ? 
                    476: 		       $curve->{'color'}       : 
1.13      matthew   477: 		       $plot{'fgcolor'}        );
1.6       matthew   478:     }
                    479:     # set term
                    480:     $gnuplot_input .= 'set term gif ';
                    481:     $gnuplot_input .= 'transparent ' if ($plot{'transparent'} eq 'on');
                    482:     $gnuplot_input .= $plot{'font'} . ' ';
1.10      matthew   483:     $gnuplot_input .= 'size '.$plot{'width'}.','.$plot{'height'}.' ';
1.6       matthew   484:     $gnuplot_input .= "@Colors\n";
1.7       matthew   485:     # grid
1.10      matthew   486:     $gnuplot_input .= 'set grid'.$/ if ($plot{'grid'} eq 'on');
1.7       matthew   487:     # border
1.9       matthew   488:     $gnuplot_input .= ($plot{'border'} eq 'on'?
                    489: 		       'set border'.$/           :
                    490: 		       'set noborder'.$/         );    # title, xlabel, ylabel
1.13      matthew   491:     $gnuplot_input .= "set output\n";
                    492:     $gnuplot_input .= "set title  \"$title\"\n"  if (defined($title)) ;
                    493:     $gnuplot_input .= "set xlabel \"$xlabel\"\n" if (defined($xlabel));
                    494:     $gnuplot_input .= "set ylabel \"$ylabel\"\n" if (defined($ylabel));
                    495:     if (defined(%axis)) {
                    496: 	$gnuplot_input .= "set xrange \[$axis{'xmin'}:$axis{'xmax'}\]\n";
                    497: 	$gnuplot_input .= "set yrange \[$axis{'ymin'}:$axis{'ymax'}\]\n";
1.6       matthew   498:     }
                    499:     # Key
1.13      matthew   500:     if (defined(%key)) {
1.9       matthew   501: 	$gnuplot_input .= 'set key '.$key{'pos'}.' ';
                    502: 	if ($key{'title'} ne '') {
1.11      matthew   503: 	    $gnuplot_input .= 'title "'.$key{'title'}.'" ';
                    504: 	} 
                    505: 	$gnuplot_input .= ($key{'box'} eq 'on' ? 'box ' : 'nobox ').$/;
1.6       matthew   506:     } else {
1.9       matthew   507: 	$gnuplot_input .= 'set nokey'.$/;
1.13      matthew   508:     }
1.6       matthew   509:     # labels
1.10      matthew   510:     my $label;
1.6       matthew   511:     foreach $label (@labels) {
                    512: 	$gnuplot_input .= 'set label "'.$label->{'text'}.'" at '.
1.9       matthew   513: 	    $label->{'xpos'}.','.$label->{'ypos'}.' '.$label->{'justify'}.$/ ;
1.6       matthew   514:     }
                    515:     # curves
                    516:     $gnuplot_input .= 'plot ';
                    517:     my $datatext = '';
1.9       matthew   518:     for (my $i = 0;$i<=$#curves;$i++) {
                    519: 	$curve = $curves[$i];
                    520: 	$gnuplot_input.= ', ' if ($i > 0);
1.6       matthew   521: 	if (exists($curve->{'function'})) {
1.9       matthew   522: 	    $gnuplot_input.= 
                    523: 		$curve->{'function'}.' title "'.
                    524: 		$curve->{'name'}.'" with '.
                    525: 		$curve->{'linestyle'};
1.6       matthew   526: 	} elsif (exists($curve->{'data'})) {
1.9       matthew   527: 	    $gnuplot_input.= '\'-\' title "'.
                    528: 		$curve->{'name'}.'" with '.
                    529: 		$curve->{'linestyle'};
1.6       matthew   530: 	    my @Data = @{$curve->{'data'}};
1.9       matthew   531: 	    my @Data0 = @{$Data[0]};
                    532: 	    for (my $i =0; $i<=$#Data0; $i++) {
1.10      matthew   533: 		my $dataset;
1.6       matthew   534: 		foreach $dataset (@Data) {
1.9       matthew   535: 		    $datatext .= $dataset->[$i] . ' ';
1.6       matthew   536: 		}
1.9       matthew   537: 		$datatext .= $/;
1.6       matthew   538: 	    }
1.9       matthew   539: 	    $datatext .=$/;
1.6       matthew   540: 	}
                    541:     }
1.9       matthew   542:     $gnuplot_input .= $/.$datatext;
1.10      matthew   543:     return $gnuplot_input;
1.2       matthew   544: }
1.1       matthew   545: 
                    546: 1;
                    547: __END__
1.4       matthew   548: 
                    549: 
                    550: 
                    551: 
                    552: 

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