--- loncom/homework/functionplotresponse.pm 2010/11/20 00:14:44 1.42 +++ loncom/homework/functionplotresponse.pm 2010/11/23 23:55:01 1.46 @@ -1,7 +1,7 @@ # LearningOnline Network with CAPA # option list style responses # -# $Id: functionplotresponse.pm,v 1.42 2010/11/20 00:14:44 www Exp $ +# $Id: functionplotresponse.pm,v 1.46 2010/11/23 23:55:01 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -295,28 +295,42 @@ ENDAXESSCRIPT } sub axes_label { - my ($id,$xlabel,$ylabel)=@_; + my ($id,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel)=@_; unless ($xlabel || $ylabel) { return ''; } my $return='document.ggbApplet_'.$id.'.evalCommand("topRight=Corner[3]");'; if ($xlabel) { + if (($ymin<0) && ($ymax>0)) { $return.=(<<ENDXAXISLABELSCRIPT); document.ggbApplet_$id.evalCommand("Xlabel=(x(topRight)-AxisStepX[],AxisStepY[]/6)"); document.ggbApplet_$id.setVisible("Xlabel",false); document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", Xlabel]"); ENDXAXISLABELSCRIPT + } else { + $return.=(<<ENDXOFFAXISLABEL); +document.ggbApplet_$id.evalCommand("LowerRight=Corner[2]"); +document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", (x(LowerRight) - AxisStepX[], y(LowerRight) + AxisStepY[] / 2)]"); +ENDXOFFAXISLABEL + } } if ($ylabel) { + if (($xmin<0) && ($xmax>0)) { $return.=(<<ENDYAXISLABELSCRIPT); document.ggbApplet_$id.evalCommand("Ylabel=(AxisStepX[]/6,y(topRight)-AxisStepY[]/3)"); document.ggbApplet_$id.setVisible("Ylabel",false); document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", Ylabel]"); ENDYAXISLABELSCRIPT + } else { + $return.=(<<ENDYOFFAXISLABEL); +document.ggbApplet_$id.evalCommand("UpperLeft=Corner[4]"); +document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", (x(UpperLeft) + AxisStepX[] / 5, y(UpperLeft) - AxisStepY[] / 1.8)]"); +ENDYOFFAXISLABEL + } } return $return; } sub plot_script { - my ($id,$function,$fixed,$label,$xmin,$xmax)=@_; + my ($id,$function,$fixed,$label,$color,$xmin,$xmax)=@_; $label=~s/\W//g; if (($label) && ($label!~/^[A-Za-z]/)) { $label='C'.$label; @@ -328,11 +342,21 @@ sub plot_script { $Apache::functionplotresponse::counter++; $label='C'.$Apache::functionplotresponse::counter; } + my $rc=0; + my $gc=0; + my $bc=0; + if ($color) { + my ($rh,$gh,$bh)=($color=~/(..)(..)(..)/); + $rc=hex($rh); + $gc=hex($gh); + $bc=hex($bh); + } if ($fixed) { - return "document.ggbApplet_$id.evalCommand('$label=Function[$function,$xmin,$xmax]')". - ($visible?'':"document.ggbApplet.setLabelVisible('$label', false);"); + return "document.ggbApplet_$id.evalCommand('$label=Function[$function,$xmin,$xmax]');\n". + ($visible?'':"document.ggbApplet_$id.setLabelVisible('$label', false);\n"). + ($color?"document.ggbApplet_$id.setColor('$label',$rc,$gc,$bc);\n":''); } else { - return "document.ggbApplet_$id.evalCommand('y=$function')"; + return "document.ggbApplet_$id.evalCommand('y=$function');\n"; } } @@ -370,24 +394,29 @@ sub start_backgroundplot { my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1]; my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval); my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval); + my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval); + $color=~s/[^a-fA-F0-9]//gs; + unless (length($color)==6) { $color=''; } my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0); unless ($function) { $function="0"; } if ($target eq 'web') { my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3); - $result.=&plot_script($internalid,$function,$fixed,$label,$xmin,$xmax); + $result.=&plot_script($internalid,$function,$fixed,$label,$color,$xmin,$xmax); } elsif ($target eq 'edit') { $result=&Apache::edit::tag_start($target,$token,'Background Function Plot'). &Apache::edit::text_arg('Function:','function', $token,'16'). &Apache::edit::text_arg('Label on Plot:','label', $token,'8'). + &Apache::edit::text_arg('Color (hex code):','color', + $token,'8'). &Apache::edit::select_arg('Fixed location:','fixed', ['yes','no'],$token). &Apache::edit::end_row(); } elsif ($target eq 'modified') { my $constructtag=&Apache::edit::get_new_args($token,$parstack, - $safeeval,'function','label','fixed'); + $safeeval,'function','label','color','fixed'); if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); } } return $result; @@ -467,18 +496,19 @@ sub start_functionplotrule { ['1','First derivative'], ['2','Second derivative'], ['-1','Integral']],$token).'<br />'. - &Apache::edit::text_arg('(Initial) x-value:','xinitial', + &Apache::edit::text_arg('Initial x-value:','xinitial', $token,'8'). - &Apache::edit::select_or_text_arg('(Initial) x-value label:','xinitiallabel', - [['start','Start of Plot']],$token,'8').'<br />'. + &Apache::edit::select_or_text_arg('Initial x-value label:','xinitiallabel', + [['start','Start of Plot'], + ['end','End of Plot']],$token,'8').'<br />'. - &Apache::edit::text_arg('Optional final x-value for ranges:','xfinal', + &Apache::edit::text_arg('Final x-value (optional):','xfinal', $token,'8'). - &Apache::edit::select_or_text_arg('Optional final x-value label:','xfinallabel', + &Apache::edit::select_or_text_arg('Final x-value label (optional):','xfinallabel', [['end','End of Plot']],$token,'8').'<br />'. - &Apache::edit::text_arg('Optional minimum length for range:','minimumlength', + &Apache::edit::text_arg('Minimum length for range (optional):','minimumlength', $token,'8'). - &Apache::edit::text_arg('Optional maximum length for range:','maximumlength', + &Apache::edit::text_arg('Maximum length for range (optional):','maximumlength', $token,'8').'<br />'. &Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship', [['eq','equal'], @@ -562,7 +592,7 @@ sub start_spline { &Apache::edit::text_arg('Index:','index', $token,'4').' '. &Apache::edit::select_arg('Order:','order', - ['2','3','4','5','6','7','8','9'],$token).' '. + ['2','3','4','5','6','7','8'],$token).' '. &Apache::edit::text_arg('Initial x-value:','initx', $token,'4').' '. &Apache::edit::text_arg('Initial y-value:','inity', @@ -1212,7 +1242,7 @@ sub start_functionplotelements { $result.=&start_init_script($internalid); # put the axis commands inside $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible); - $result.=&axes_label($internalid,$xlabel,$ylabel); + $result.=&axes_label($internalid,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel); # init script is left open } return $result;