--- loncom/xml/lonplot.pm 2001/12/18 16:06:01 1.3
+++ loncom/xml/lonplot.pm 2002/01/04 14:30:08 1.32
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Dynamic plot
#
-# $Id: lonplot.pm,v 1.3 2001/12/18 16:06:01 matthew Exp $
+# $Id: lonplot.pm,v 1.32 2002/01/04 14:30:08 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -26,122 +26,325 @@
# http://www.lon-capa.org/
#
# 12/15/01 Matthew
-# 12/18 Matthew
+# 12/17 12/18 12/19 12/20 12/21 12/27 12/28 12/30 12/31 Matthew
+# 01/01/02 Matthew
+# 01/02 01/03 Matthew
package Apache::lonplot;
+
use strict;
+use Apache::File;
use Apache::response;
use Apache::lonxml;
-use Digest::MD5 qw(md5 md5_hex md5_base64);
+use Apache::edit;
sub BEGIN {
&Apache::lonxml::register('Apache::lonplot',('plot'));
}
-
+##
+## Description of data structures:
+##
+## %plot %key %axis
+## --------------------------
+## height title color
+## width box xmin
+## bgcolor pos xmax
+## fgcolor ymin
+## transparent ymax
+## grid
+## border
+## font
+## align
+##
+## @labels: $labels[$i] = \%label
+## %label: text, xpos, ypos, justify
##
-## Tests used in checking the validitity of input
+## @curves: $curves[$i] = \%curve
+## %curve: name, linestyle, ( function | data )
##
-my $int_test = sub {$_[0]=~/^\d+$/};
-my $real_test = sub {$_[0]=~/^[+-]?\d*\.?\d*$/};
-my $color_test = sub {$_[0]=~/^x[\da-f]{6}$/};
+## $curves[$i]->{'data'} = [ [x1,x2,x3,x4],
+## [y1,y2,y3,y4] ]
+##
+
+###################################################################
+## ##
+## Tests used in checking the validitity of input ##
+## ##
+###################################################################
+
+my $max_str_len = 50; # if a label, title, xlabel, or ylabel text
+ # is longer than this, it will be truncated.
+
+my %linestyles =
+ (
+ lines => 2, # Maybe this will be used in the future
+ linespoints => 2, # to check on whether or not they have
+ dots => 2, # supplied enough fields
+ points => 2, # to use the given line style. But for
+ steps => 2, # now there are more important things
+ fsteps => 2, # for me to deal with.
+ histeps => 2,
+ errorbars => 2,
+ xerrorbars => 2,
+ yerrorbars => 2,
+ xyerrorbars => 2,
+ boxes => 2,
+ boxerrorbars => 2,
+ boxxyerrorbars => 2,
+ financebars => 2,
+ candlesticks => 2,
+ vector => 2
+ );
+
+my $int_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^\d+$/};
+my $real_test =
+ sub {$_[0]=~s/\s+//g;$_[0]=~/^[+-]?\d*\.?\d*([eE][+-]\d+)?$/};
+my $color_test = sub {$_[0]=~s/\s+//g;$_[0]=~/^x[\da-f]{6}$/};
my $onoff_test = sub {$_[0]=~/^(on|off)$/};
-my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below)+$/};
+my $key_pos_test = sub {$_[0]=~/^(top|bottom|right|left|outside|below| )+$/};
my $sml_test = sub {$_[0]=~/^(small|medium|large)$/};
-my $linestyle_test = sub {$_[0]=~/^(lines|linespoints|dots|points|steps)$/};
+my $linestyle_test = sub {exists($linestyles{$_[0]})};
+my $words_test = sub {$_[0]=~s/\s+/ /g;$_[0]=~/^([\w\(\)]+ ?)+$/};
-##
-## Default values for attributes of elements
-##
+###################################################################
+## ##
+## Attribute metadata ##
+## ##
+###################################################################
my %plot_defaults =
(
- height => {default => 200, test => $int_test },
- width => {default => 200, test => $int_test },
- bgcolor => {default => "xffffff", test => $color_test},
- fgcolor => {default => "x000000", test => $color_test},
- transparent => {default => "off", test => $onoff_test},
- grid => {default => "off", test => $onoff_test},
- border => {default => "on" , test => $onoff_test},
- font => {default => "medium", test => $sml_test }
+ height => {
+ default => 200,
+ test => $int_test,
+ description => 'height of image (pixels)',
+ edit_type => 'entry'
+ },
+ width => {
+ default => 200,
+ test => $int_test,
+ description => 'width of image (pixels)',
+ edit_type => 'entry'
+ },
+ bgcolor => {
+ default => 'xffffff',
+ test => $color_test,
+ description => 'background color of image (xffffff)',
+ edit_type => 'entry'
+ },
+ fgcolor => {
+ default => 'x000000',
+ test => $color_test,
+ description => 'foreground color of image (x000000)',
+ edit_type => 'entry'
+ },
+ transparent => {
+ default => 'off',
+ test => $onoff_test,
+ description => '',
+ edit_type => 'on_off'
+ },
+ grid => {
+ default => 'off',
+ test => $onoff_test,
+ description => '',
+ edit_type => 'on_off'
+ },
+ border => {
+ default => 'on',
+ test => $onoff_test,
+ description => '',
+ edit_type => 'on_off'
+ },
+ font => {
+ default => 'medium',
+ test => $sml_test,
+ description => 'Size of font to use',
+ edit_type => 'choice',
+ choices => ['small','medium','large']
+ },
+ align => {
+ default => 'left',
+ test => sub {$_[0]=~/^(left|right|center)$/},
+ description => 'alignment for image in html',
+ edit_type => 'choice',
+ choices => ['left','right','center']
+ }
);
my %key_defaults =
(
- title => { default => "on" , test => $onoff_test },
- box => { default => "off" , test => $onoff_test },
- pos => { default => "top right" , test => $key_pos_test}
+ title => {
+ default => '',
+ test => $words_test,
+ description => 'Title of key',
+ edit_type => 'entry'
+ },
+ box => {
+ default => 'off',
+ test => $onoff_test,
+ description => 'Draw a box around the key?',
+ edit_type => 'on_off'
+ },
+ pos => {
+ default => 'top right',
+ test => $key_pos_test,
+ description => 'position of the key on the plot',
+ edit_type => 'choice',
+ choices => ['top left','top right','bottom left','bottom right',
+ 'outside','below']
+ }
);
my %label_defaults =
(
- xpos => {default => 0, test => $real_test },
- ypos => {default => 0, test => $real_test },
- color => {default => "x000000", test => $color_test },
- justify => {default => "left",
- test => sub {$_[0]=~/^(left|right|center)$/}}
+ xpos => {
+ default => 0,
+ test => $real_test,
+ description => 'x position of label (graph coordinates)',
+ edit_type => 'entry'
+ },
+ ypos => {
+ default => 0,
+ test => $real_test,
+ description => 'y position of label (graph coordinates)',
+ edit_type => 'entry'
+ },
+ justify => {
+ default => 'left',
+ test => sub {$_[0]=~/^(left|right|center)$/},
+ description => 'justification of the label text on the plot',
+ edit_type => 'choice',
+ choices => ['left','right','center']
+ }
);
my %axis_defaults =
(
- color => {default => "x000000", test => $color_test},
- thickness => {default => 1, test => $int_test },
- xmin => {default => -10.0, test => $real_test },
- xmax => {default => 10.0, test => $real_test },
- ymin => {default => -10.0, test => $real_test },
- ymax => {default => 10.0, test => $real_test }
+ color => {
+ default => 'x000000',
+ test => $color_test,
+ description => 'color of axes (x000000)',
+ edit_type => 'entry'
+ },
+ xmin => {
+ default => '-10.0',
+ test => $real_test,
+ description => 'minimum x-value shown in plot',
+ edit_type => 'entry'
+ },
+ xmax => {
+ default => ' 10.0',
+ test => $real_test,
+ description => 'maximum x-value shown in plot',
+ edit_type => 'entry'
+ },
+ ymin => {
+ default => '-10.0',
+ test => $real_test,
+ description => 'minimum y-value shown in plot',
+ edit_type => 'entry'
+ },
+ ymax => {
+ default => ' 10.0',
+ test => $real_test,
+ description => 'maximum y-value shown in plot',
+ edit_type => 'entry'
+ }
);
my %curve_defaults =
(
- color => {default => "x000000", test => $color_test },
- name => {default => "x000000", test => sub {$_[0]=~/^[\w ]*$/} },
- linestyle => {default => "lines", test => $linestyle_test }
+ color => {
+ default => 'x000000',
+ test => $color_test,
+ description => 'color of curve (x000000)',
+ edit_type => 'entry'
+ },
+ name => {
+ default => '',
+ test => $words_test,
+ description => 'name of curve to appear in key',
+ edit_type => 'entry'
+ },
+ linestyle => {
+ default => 'lines',
+ test => $linestyle_test,
+ description => 'Style of the axis lines',
+ edit_type => 'choice',
+ choices => ['lines','linespoints','dots','points','steps',
+ 'fsteps','histeps','errorbars','xerrorbars',
+ 'yerrorbars','xyerrorbars','boxes','boxerrorbars',
+ 'boxxyerrorbars','financebars','candlesticks',
+ 'vector']
+ }
);
-##
-## End of defaults
-##
+###################################################################
+## ##
+## parsing and edit rendering ##
+## ##
+###################################################################
my (%plot,%key,%axis,$title,$xlabel,$ylabel,@labels,@curves);
sub start_plot {
- %plot = ''; %key=''; %axis='';
- $title=''; $xlabel=''; $ylabel='';
- @labels = ''; @curves='';
-
+ %plot = (); %key = (); %axis = ();
+ $title = undef; $xlabel = undef; $ylabel = undef;
+ $#labels = -1; $#curves = -1;
+ #
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result='';
- &Apache::lonxml::register('Apache::plot',
+ &Apache::lonxml::register('Apache::lonplot',
('title','xlabel','ylabel','key','axis','label','curve'));
- push (@Apache::lonxml::namespace,'plot');
- ##-------------------------------------------------------
- ## How do I do this? I need to "eval" and I need to keep the info
- ## available for the parser.
- ##
- my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
- my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
- if ($eval eq 'on') {
+ push (@Apache::lonxml::namespace,'lonplot');
+ if ($target eq 'web') {
+ my $inside = &Apache::lonxml::get_all_text("/plot",$$parser[-1]);
$inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
- #&Apache::lonxml::debug("M is evaulated to:$inside:");
+ &Apache::lonxml::newparser($parser,\$inside);
+ &get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,
+ $tagstack->[-1]);
+ } elsif ($target eq 'edit') {
+ $result .= &Apache::edit::tag_start($target,$token,'Plot');
+ $result .= &edit_attributes($target,$token,\%plot_defaults);
+ } elsif ($target eq 'modified') {
+ my $constructtag=&Apache::edit::get_new_args
+ ($token,$parstack,$safeeval,keys(%plot_defaults));
+ if ($constructtag) {
+ $result = &Apache::edit::rebuild_tag($token);
+# $result.= &Apache::edit::handle_insert();
+ }
}
- ##-------------------------------------------------------
- &get_attributes(\%plot,\%plot_defaults,$parstack,$safeeval,'plot');
- return '';
+ return $result;
}
sub end_plot {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+
pop @Apache::lonxml::namespace;
- my $result;
- ## Determine filename
- my $tmpdir = '/home/httpd/perl/tmp/';
- my $filename = $tmpdir.$ENV{'user.name'}.'_'.$ENV{'user.domain'}.
- '_plot.data';
- my $usersees=md5_base64($filename.'_'.$ENV{'REMOTE_ADDR'});
-
- ## Write the plot description to the file
- my $fh=&Apache::File->new('/home/httpd/perl/tmp/'.$realname);
- ## Ack!
- ## return image tag for the plot
- $result = 'new(">$tmpdir$filename");
+ print $fh &write_gnuplot_file();
+ close($fh);
+ ## return image tag for the plot
+ $result .= <<"ENDIMAGE";
+
+ENDIMAGE
+ } elsif ($target eq 'edit') {
+ $result.=&Apache::edit::tag_end($target,$token);
+ }
return $result;
}
@@ -149,68 +352,163 @@ sub end_plot {
sub start_key {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result='';
- &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,$tagstack);
+ if ($target eq 'web') {
+ &get_attributes(\%key,\%key_defaults,$parstack,$safeeval,
+ $tagstack->[-1]);
+ } elsif ($target eq 'edit') {
+ $result .= &Apache::edit::tag_start($target,$token,'Plot Key');
+ $result .= &edit_attributes($target,$token,\%key_defaults);
+ } elsif ($target eq 'modified') {
+ my $constructtag=&Apache::edit::get_new_args
+ ($token,$parstack,$safeeval,keys(%key_defaults));
+ if ($constructtag) {
+ $result = &Apache::edit::rebuild_tag($token);
+ $result.= &Apache::edit::handle_insert();
+ }
+ }
return $result;
}
sub end_key {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result = '';
+ if ($target eq 'web') {
+ } elsif ($target eq 'edit') {
+ $result.=&Apache::edit::tag_end($target,$token);
+ }
return $result;
}
+
##------------------------------------------------------------------- title
sub start_title {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
- $title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
my $result='';
+ if ($target eq 'web') {
+ $title = &Apache::lonxml::get_all_text("/title",$$parser[-1]);
+ if (length($title) > $max_str_len) {
+ $title = substr($title,0,$max_str_len);
+ }
+ } elsif ($target eq 'edit') {
+ $result.=&Apache::edit::tag_start($target,$token,'Plot Title');
+ my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
+ $result.='