--- loncom/interface/loncommon.pm 2003/10/24 21:09:24 1.135
+++ loncom/interface/loncommon.pm 2003/10/29 15:21:10 1.137
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# a pile of common routines
#
-# $Id: loncommon.pm,v 1.135 2003/10/24 21:09:24 albertel Exp $
+# $Id: loncommon.pm,v 1.137 2003/10/29 15:21:10 matthew Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -2678,6 +2678,10 @@ sub check_if_partid_hidden {
return undef;
}
+sub get_cgi_id {
+ return (time.'_'.int(rand(1000)));
+}
+
############################################################
############################################################
@@ -2685,7 +2689,6 @@ sub check_if_partid_hidden {
=item DrawBarGraph
-
=cut
############################################################
@@ -2700,7 +2703,8 @@ sub DrawBarGraph {
];
}
#
- my $identifier = time.'_'.int(rand(1000));
+ my $identifier = &get_cgi_id();
+ my $id = 'cgi.'.$identifier;
if (! @Values || ref($Values[0]) ne 'ARRAY') {
return '';
}
@@ -2709,31 +2713,172 @@ sub DrawBarGraph {
my $NumSets=1;
foreach my $array (@Values) {
next if (! ref($array));
- $ValuesHash{'cgi.'.$identifier.'.data.'.$NumSets++} =
+ $ValuesHash{$id.'.data.'.$NumSets++} =
join(',',@$array);
}
#
- $Title = '' if (! defined($Title));
- $xlabel = '' if (! defined($xlabel));
- $ylabel = '' if (! defined($ylabel));
- $Title = &Apache::lonnet::escape($Title);
- $xlabel = &Apache::lonnet::escape($xlabel);
- $ylabel = &Apache::lonnet::escape($ylabel);
+ my ($height,$width,$xskip,$bar_width) = (200,120,1,15);
+ if ($NumBars < 10) {
+ $width = 120+$NumBars*15;
+ $xskip = 1;
+ $bar_width = 15;
+ } elsif ($NumBars <= 25) {
+ $width = 120+$NumBars*11;
+ $xskip = 5;
+ $bar_width = 8;
+ } elsif ($NumBars <= 50) {
+ $width = 120+$NumBars*8;
+ $xskip = 5;
+ $bar_width = 4;
+ } else {
+ $width = 120+$NumBars*8;
+ $xskip = 5;
+ $bar_width = 4;
+ }
+ #
+ my @Labels;
+ for (my $i=0;$i<@{$Values[0]};$i++) {
+ push (@Labels,$i+1);
+ }
#
$Max = 1 if ($Max < 1);
if ( int($Max) < $Max ) {
$Max++;
$Max = int($Max);
}
+ $Title = '' if (! defined($Title));
+ $xlabel = '' if (! defined($xlabel));
+ $ylabel = '' if (! defined($ylabel));
+ $ValuesHash{$id.'.title'} = &Apache::lonnet::escape($Title);
+ $ValuesHash{$id.'.xlabel'} = &Apache::lonnet::escape($xlabel);
+ $ValuesHash{$id.'.ylabel'} = &Apache::lonnet::escape($ylabel);
+ $ValuesHash{$id.'.y_max_value'} = $Max;
+ $ValuesHash{$id.'.NumBars'} = $NumBars;
+ $ValuesHash{$id.'.NumSets'} = $NumSets;
+ $ValuesHash{$id.'.PlotType'} = 'bar';
+ $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
+ $ValuesHash{$id.'.height'} = $height;
+ $ValuesHash{$id.'.width'} = $width;
+ $ValuesHash{$id.'.xskip'} = $xskip;
+ $ValuesHash{$id.'.bar_width'} = $bar_width;
+ $ValuesHash{$id.'.labels'} = join(',',@Labels);
+ #
+ &Apache::lonnet::appenv(%ValuesHash);
+ return '';
+}
+
+############################################################
+############################################################
+
+=pod
+
+=item DrawXYGraph
+
+=cut
+
+############################################################
+############################################################
+sub DrawXYGraph {
+ my ($Title,$xlabel,$ylabel,$Max,$colors,$Xlabels,$Ydata,%Values)=@_;
+ #
+ # Create the identifier for the graph
+ my $identifier = &get_cgi_id();
+ my $id = 'cgi.'.$identifier;
+ #
+ $Title = '' if (! defined($Title));
+ $xlabel = '' if (! defined($xlabel));
+ $ylabel = '' if (! defined($ylabel));
+ my %ValuesHash =
+ (
+ $id.'.title' => &Apache::lonnet::escape($Title),
+ $id.'.xlabel' => &Apache::lonnet::escape($xlabel),
+ $id.'.ylabel' => &Apache::lonnet::escape($ylabel),
+ $id.'.y_max_value'=> $Max,
+ $id.'.labels' => join(',',@$Xlabels),
+ $id.'.PlotType' => 'XY',
+ $id.'.NumSets' => 1,
+ );
+ #
+ if (defined($colors) && ref($colors) eq 'ARRAY') {
+ $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
+ }
+ #
+ if (! ref($Ydata) || ref($Ydata) ne 'ARRAY') {
+ return '';
+ }
+ my $NumSets=1;
+ foreach my $array ($Ydata){
+ next if (! ref($array));
+ $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
+ }
+ #
+ # Deal with other parameters
+ while (my ($key,$value) = each(%Values)) {
+ $ValuesHash{$id.'.'.$key} = $value;
+ }
+ #
+ &Apache::lonnet::appenv(%ValuesHash);
+ return '';
+}
+
+
+############################################################
+############################################################
+
+=pod
+
+=item DrawXYGraph
+
+=cut
+
+############################################################
+############################################################
+sub DrawXYYGraph {
+ my ($Title,$xlabel,$ylabel,$colors,$Xlabels,$Ydata1,$Min1,$Max1,
+ $Ydata2,$Min2,$Max2,%Values)=@_;
+ #
+ # Create the identifier for the graph
+ my $identifier = &get_cgi_id();
+ my $id = 'cgi.'.$identifier;
+ #
+ $Title = '' if (! defined($Title));
+ $xlabel = '' if (! defined($xlabel));
+ $ylabel = '' if (! defined($ylabel));
+ my %ValuesHash =
+ (
+ $id.'.title' => &Apache::lonnet::escape($Title),
+ $id.'.xlabel' => &Apache::lonnet::escape($xlabel),
+ $id.'.ylabel' => &Apache::lonnet::escape($ylabel),
+ $id.'.labels' => join(',',@$Xlabels),
+ $id.'.PlotType' => 'XY',
+ $id.'.NumSets' => 2,
+ $id.'.two_axes' => 1,
+ $id.'.y1_max_value' => $Max1,
+ $id.'.y1_min_value' => $Min1,
+ $id.'.y2_max_value' => $Max2,
+ $id.'.y2_min_value' => $Min2,
+ );
+ #
+ if (defined($colors) && ref($colors) eq 'ARRAY') {
+ $ValuesHash{$id.'.Colors'} = join(',',@{$colors});
+ }
+ #
+ if (! ref($Ydata1) || ref($Ydata1) ne 'ARRAY' ||
+ ! ref($Ydata2) || ref($Ydata2) ne 'ARRAY'){
+ return '';
+ }
+ my $NumSets=1;
+ foreach my $array ($Ydata1,$Ydata2){
+ next if (! ref($array));
+ $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array);
+ }
+ #
+ # Deal with other parameters
+ while (my ($key,$value) = each(%Values)) {
+ $ValuesHash{$id.'.'.$key} = $value;
+ }
#
- &Apache::lonnet::appenv('cgi.'.$identifier.'.title' => $Title,
- 'cgi.'.$identifier.'.xlabel' => $xlabel,
- 'cgi.'.$identifier.'.ylabel' => $ylabel,
- 'cgi.'.$identifier.'.Max' => $Max,
- 'cgi.'.$identifier.'.NumBars' => $NumBars,
- 'cgi.'.$identifier.'.NumSets' => $NumSets,
- 'cgi.'.$identifier.'.Colors' => join(',',@{$colors}),
- %ValuesHash);
+ &Apache::lonnet::appenv(%ValuesHash);
return '';
}