@@ -110,15 +110,17 @@ to add / change the styles.
110110
111111 $data->style(color => 'blue', width => 3);
112112
113- =item C<< $str = $data->function_string($coord , $type, $nvars ); >>
113+ =item C<< $str = $data->function_string($formula , $type, $xvar, $yvar ); >>
114114
115- Takes a MathObject function string and replaces the function with either
115+ Takes a MathObject C< $formula > and replaces the function with either
116116a JavaScript or PGF function string. If the function contains any function
117117tokens not supported, a warning and empty string is returned.
118118
119- $coord 'x' or 'y' coordinate function.
120- $type 'js' or 'PGF' (falls back to js for any input except 'PGF').
121- $nvars 1 (single variable functions) or 2 (used for slope/vector fields).
119+ $formula The mathobject formula object, either $self->{function}{Fx} or $self->{function}{Fy}.
120+ $type 'js' or 'PGF' (falls back to js for any input except 'PGF').
121+ $xvar The x-variable name, $self->{function}{xvar}.
122+ $yvar The y-variable name, $self->{function}{yvar}, for vector fields.
123+ Leave undefined for single variable functions.
122124
123125=item C<< $data->update_min_max >>
124126
@@ -185,7 +187,7 @@ sub style {
185187 map { $self -> {styles }{$_ } = $style_hash {$_ } } keys %style_hash ;
186188 return ;
187189 }
188- return $self -> {styles }{ $styles [0] };
190+ return $self -> {styles }{ $styles [0] } // ' ' ;
189191}
190192
191193sub get_math_object {
@@ -249,22 +251,22 @@ sub update_min_max {
249251}
250252
251253sub function_string {
252- my ($self , $coord , $type , $nvars ) = @_ ;
253- my $f = $self -> {function };
254- my $MO = $coord eq ' y' ? $f -> {Fy } : $f -> {Fx };
255- return ' ' if ref ($MO ) eq ' CODE' ;
254+ my ($self , $formula , $type , $xvar , $yvar , $xtransform ) = @_ ;
255+ return ' ' unless Value::isFormula($formula );
256+ my %vars = ($xvar => $xtransform || ' x' , $yvar ? ($yvar => ' y' ) : ());
256257
257258 # Ensure -x^2 gets print as -(x^2), since JavaScript finds this ambiguous.
258- my $extraParens = $MO -> context-> flag(' showExtraParens' );
259- $MO -> context-> flags-> set(showExtraParens => 2);
260- my $func = $MO -> string;
259+ my $extraParens = $formula -> context-> flag(' showExtraParens' );
260+ my $format = $formula -> context-> {format }{number };
261+ $formula -> context-> flags-> set(showExtraParens => 2);
262+ $formula -> context-> {format }{number } = " %f #" ;
263+ my $func = $formula -> string;
261264 $func =~ s /\s // g ;
262- $MO -> context-> flags-> set(showExtraParens => $extraParens );
265+ $formula -> context-> flags-> set(showExtraParens => $extraParens );
266+ $formula -> context-> {format }{number } = $format ;
263267
264- $nvars = 1 unless $nvars ;
265268 my %tokens ;
266269 if ($type eq ' PGF' ) {
267- my %vars = ($nvars == 2 ? ($f -> {xvar } => ' x' , $f -> {yvar } => ' y' ) : ($f -> {xvar } => ' x' ));
268270 %tokens = (
269271 sqrt => ' sqrt' ,
270272 pow => ' pow' ,
@@ -302,7 +304,6 @@ sub function_string {
302304 %vars
303305 );
304306 } else {
305- my %vars = ($nvars == 2 ? ($f -> {xvar } => ' x' , $f -> {yvar } => ' y' ) : ($f -> {xvar } => ' t' ));
306307 %tokens = (
307308 sqrt => ' Math.sqrt' ,
308309 cbrt => ' Math.cbrt' ,
@@ -367,6 +368,8 @@ sub function_string {
367368 }
368369 }
369370
371+ $out =~ s /\[ / (/ g ;
372+ $out =~ s /\] / )/ g ;
370373 return $out ;
371374}
372375
0 commit comments