--- loncom/xml/lonplot.pm	2002/01/10 19:45:51	1.38
+++ loncom/xml/lonplot.pm	2002/01/21 15:20:28	1.41
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Dynamic plot
 #
-# $Id: lonplot.pm,v 1.38 2002/01/10 19:45:51 matthew Exp $
+# $Id: lonplot.pm,v 1.41 2002/01/21 15:20:28 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -29,13 +29,8 @@
 # 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 01/04 01/07 01/08 01/09 Matthew
+# 01/21 Matthew
 
-# Current issues
-#   1. Gnuplot is unable to vary the color or linestyle of <data> plots.
-#      The key does not know this so it is misleading for the user.
-#      Multiple <function>s can be plotted with varying line styles and
-#      colors.
-#
 package Apache::lonplot;
 
 use strict;
@@ -330,7 +325,6 @@ sub start_plot {
 	    ($token,$parstack,$safeeval,keys(%plot_defaults));
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
-#	    $result.= &Apache::edit::handle_insert();
 	}
     }
     return $result;
@@ -351,9 +345,7 @@ sub end_plot {
 	my $filename = $ENV{'user.name'}.'_'.$ENV{'user.domain'}.
 	    '_'.time.'_'.$$.int(rand(1000)).'_plot.data';
 	## Write the plot description to the file
-	my $fh=Apache::File->new(">$tmpdir$filename");
-	print $fh &write_gnuplot_file();
-	close($fh);
+	&write_gnuplot_file($tmpdir,$filename);
 	## return image tag for the plot
 	$result .= <<"ENDIMAGE";
 <img src    = "/cgi-bin/plot.gif?$filename" 
@@ -383,7 +375,6 @@ sub start_key {
 	    ($token,$parstack,$safeeval,keys(%key_defaults));
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
-	    $result.= &Apache::edit::handle_insert();
 	}
     }
     return $result;
@@ -411,7 +402,8 @@ sub start_title {
     } elsif ($target eq 'edit') {
 	$result.=&Apache::edit::tag_start($target,$token,'Plot Title');
 	my $text=&Apache::lonxml::get_all_text("/title",$$parser[-1]);
-	$result.='</td></tr><tr><td colspan="3">'.
+	$result.=&Apache::edit::end_row().
+	    &Apache::edit::start_spanning_row().
 	    &Apache::edit::editfield('',$text,'',60,1);
     } elsif ($target eq 'modified') {
 	my $text=$$parser[-1]->get_text("/title");
@@ -441,7 +433,8 @@ sub start_xlabel {
     } elsif ($target eq 'edit') {
 	$result.=&Apache::edit::tag_start($target,$token,'Plot Xlabel');
 	my $text=&Apache::lonxml::get_all_text("/xlabel",$$parser[-1]);
-	$result.='</td></tr><tr><td colspan="3">'.
+	$result.=&Apache::edit::end_row().
+	    &Apache::edit::start_spanning_row().
 	    &Apache::edit::editfield('',$text,'',60,1);
     } elsif ($target eq 'modified') {
 	my $text=$$parser[-1]->get_text("/xlabel");
@@ -472,7 +465,8 @@ sub start_ylabel {
     } elsif ($target eq 'edit') {
 	$result .= &Apache::edit::tag_start($target,$token,'Plot Ylabel');
 	my $text = &Apache::lonxml::get_all_text("/ylabel",$$parser[-1]);
-	$result .= '</td></tr><tr><td colspan="3">'.
+	$result .= &Apache::edit::end_row().
+	    &Apache::edit::start_spanning_row().
 	    &Apache::edit::editfield('',$text,'',60,1);
     } elsif ($target eq 'modified') {
 	my $text=$$parser[-1]->get_text("/ylabel");
@@ -507,14 +501,14 @@ sub start_label {
 	$result .= &Apache::edit::tag_start($target,$token,'Plot Label');
 	$result .= &edit_attributes($target,$token,\%label_defaults);
 	my $text = &Apache::lonxml::get_all_text("/label",$$parser[-1]);
-	$result .= '</td></tr><tr><td colspan="3">'.
+	$result .= &Apache::edit::end_row().
+	    &Apache::edit::start_spanning_row().
 	    &Apache::edit::editfield('',$text,'',60,1);
     } elsif ($target eq 'modified') {
 	my $constructtag=&Apache::edit::get_new_args
 	    ($token,$parstack,$safeeval,keys(%label_defaults));
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
-	    $result.= &Apache::edit::handle_insert();
 	}
 	my $text=$$parser[-1]->get_text("/label");
 	$result.=&Apache::edit::modifiedfield($token);
@@ -583,7 +577,8 @@ sub start_function {
     } elsif ($target eq 'edit') {
 	$result .= &Apache::edit::tag_start($target,$token,'Gnuplot compatible curve function');
 	my $text = &Apache::lonxml::get_all_text("/function",$$parser[-1]);
-	$result .= '</td></tr><tr><td colspan="3">'.
+	$result .= &Apache::edit::end_row().
+	    &Apache::edit::start_spanning_row().
 	    &Apache::edit::editfield('',$text,'',60,1);
     } elsif ($target eq 'modified') {
 	# Why do I do this?
@@ -615,6 +610,11 @@ sub start_data {
 	    delete($curves[-1]->{'function'});
 	}
 	my $datatext = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
+	# Deal with cases where we're given an array...
+	if ($datatext =~ /^\@/) {
+	    $datatext = &Apache::run::run('return "'.$datatext.'"',
+					  $safeeval,1);
+	}
 	$datatext =~ s/\s+/ /g;  
 	# Need to do some error checking on the @data array - 
 	# make sure it's all numbers and make sure each array 
@@ -652,7 +652,8 @@ sub start_data {
     } elsif ($target eq 'edit') {
 	$result .= &Apache::edit::tag_start($target,$token,'Comma or space deliminated curve data');
 	my $text = &Apache::lonxml::get_all_text("/data",$$parser[-1]);
-	$result .= '</td></tr><tr><td colspan="3">'.
+	$result .= &Apache::edit::end_row().
+	    &Apache::edit::start_spanning_row().
 	    &Apache::edit::editfield('',$text,'',60,1);
     } elsif ($target eq 'modified') {
 	my $text=$$parser[-1]->get_text("/data");
@@ -686,7 +687,6 @@ sub start_axis {
 	    ($token,$parstack,$safeeval,keys(%axis_defaults));
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
-	    $result.= &Apache::edit::handle_insert();
 	}
     }
     return $result;
@@ -738,8 +738,10 @@ sub get_attributes{
     }
     return ;
 }
+
 ##------------------------------------------------------- write_gnuplot_file
 sub write_gnuplot_file {
+    my ($tmpdir,$filename)= @_;
     my $gnuplot_input = '';
     my $curve;
     # Collect all the colors
@@ -790,7 +792,6 @@ sub write_gnuplot_file {
     }
     # curves
     $gnuplot_input .= 'plot ';
-    my $datatext = '';
     for (my $i = 0;$i<=$#curves;$i++) {
 	$curve = $curves[$i];
 	$gnuplot_input.= ', ' if ($i > 0);
@@ -800,9 +801,12 @@ sub write_gnuplot_file {
 		$curve->{'name'}.'" with '.
 		$curve->{'linestyle'};
 	} elsif (exists($curve->{'data'})) {
-	    $gnuplot_input.= '\'-\' title "'.
-		$curve->{'name'}.'" with '.
-		$curve->{'linestyle'};
+	    # Store data values in $datatext
+	    my $datatext = '';
+	    #   get new filename
+	    my $datafilename = "$tmpdir/$filename.$i";
+	    my $fh=Apache::File->new(">$datafilename");
+	    # Compile data
 	    my @Data = @{$curve->{'data'}};
 	    my @Data0 = @{$Data[0]};
 	    for (my $i =0; $i<=$#Data0; $i++) {
@@ -812,11 +816,21 @@ sub write_gnuplot_file {
 		}
 		$datatext .= $/;
 	    }
-	    $datatext .=$/;
+	    #   write file
+	    print $fh $datatext;
+	    close ($fh);
+	    #   generate gnuplot text
+	    $gnuplot_input.= '"'.$datafilename.'" title "'.
+		$curve->{'name'}.'" with '.
+		$curve->{'linestyle'};
 	}
     }
-    $gnuplot_input .= $/.$datatext;
-    return $gnuplot_input;
+    # Write the output to a file.
+    my $fh=Apache::File->new(">$tmpdir$filename");
+    print $fh $gnuplot_input;
+    close($fh);
+    # That's all folks.
+    return ;
 }
 
 #---------------------------------------------- check_inputs
@@ -937,12 +951,13 @@ sub insert_label {
 
 sub insert_curve {
     my $result;
-    $result .= '    <curve ';
+    $result .= "\n    <curve ";
     foreach my $attr (keys(%curve_defaults)) {
 	$result .= '         '.$attr.'="'.
 	    $curve_defaults{$attr}->{'default'}."\"\n";
     }
-    $result .= "    ></curve>\n";
+    $result .= "    >\n";
+    $result .= &insert_data().&insert_data()."</curve>\n";
 }
 
 sub insert_function {