--- loncom/interface/loncommon.pm 2003/10/24 21:09:24 1.135 +++ loncom/interface/loncommon.pm 2003/10/27 21:21:08 1.136 @@ -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.136 2003/10/27 21:21:08 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,16 +2713,50 @@ sub DrawBarGraph { my $NumSets=1; foreach my $array (@Values) { next if (! ref($array)); - $ValuesHash{'cgi.'.$identifier.'.data.'.$NumSets++} = + $ValuesHash{$id.'.data.'.$NumSets++} = join(',',@$array); } # + 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); + } + # $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); + $ValuesHash{$id.'.title'} = &Apache::lonnet::escape($Title); + $ValuesHash{$id.'.xlabel'} = &Apache::lonnet::escape($xlabel); + $ValuesHash{$id.'.ylabel'} = &Apache::lonnet::escape($ylabel); + $ValuesHash{$id.'.Max'} = $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); # $Max = 1 if ($Max < 1); if ( int($Max) < $Max ) { @@ -2726,14 +2764,52 @@ sub DrawBarGraph { $Max = int($Max); } # - &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 ''; +} + +############################################################ +############################################################ + +=pod + +=item DrawXYGraph + +=cut + +############################################################ +############################################################ +sub DrawXYGraph { + my ($Title,$xlabel,$ylabel,$Max,$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.'.Max' => $Max, + $id.'.labels' => join(',',@$Xlabels), + $id.'.PlotType' => 'XY', + $id.'.NumSets' => 2, + ); + # + 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); + } + # + &Apache::lonnet::appenv(%ValuesHash); return ''; }