Skip to content

Commit b35f650

Browse files
authored
Merge pull request #1245 from somiaj/plots-improvements
Plots Improvements - A continuation of #966
2 parents 945912f + cfba117 commit b35f650

File tree

8 files changed

+600
-273
lines changed

8 files changed

+600
-273
lines changed

htdocs/js/ImageView/imageview.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@
162162
let width = naturalWidth;
163163
let height = naturalHeight;
164164

165-
if (imgType == 'div') this.dispatchEvent(new Event('shown.imageview'));
165+
if (graphDiv) {
166+
graphDiv.style.width = `${naturalWidth}px`;
167+
graphDiv.style.height = `${naturalHeight}px`;
168+
this.dispatchEvent(new Event('shown.imageview'));
169+
}
166170

167171
// Dialog position
168172
let left;

lib/Plots/Axes.pm

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Each axis and styles can be configured individually, such as:
3131
3232
$plot->axes->xaxis(min => -10, max => 10, tick_delta => 4);
3333
$plot->axes->yaxis(min => 0, max => 100, tick_delta => 20);
34-
$plot->axes->style(title => 'Graph of function y = f(x).', show_grid => 0);
34+
$plot->axes->style(ariaLabel => 'Graph of function y = f(x).', show_grid => 0);
3535
3636
This can be combined using the set method by prepending either C<x> or C<y> in front
3737
of each key of the axes to configure (note keys that do not start with C<x> or C<y>
@@ -44,7 +44,7 @@ sent to C<< $plot->axes->style >>):
4444
ymin => 0,
4545
ymax => 100,
4646
ytick_delta => 20,
47-
title => 'Graph of function y = f(x).',
47+
ariaLabel => 'Graph of function y = f(x).',
4848
show_grid => 0,
4949
);
5050
@@ -151,9 +151,20 @@ The following styles configure aspects about the axes.
151151
152152
=over 5
153153
154-
=item title
154+
=item aspect_ratio
155155
156-
The title of the graph used as the ARIA label in JSX graph output. Default is ''.
156+
If this style is set, then the height of the graph will be computed using
157+
this aspect_ratio for the size of the image unless explicitly set.
158+
Default: ''
159+
160+
=item ariaLabel
161+
162+
The ARIA label in JSX graph output. Default is 'Graph'.
163+
164+
=item ariaDescription
165+
166+
The ARIA description in JSX graph output. This will be set to the images alt tag.
167+
Default is 'Generated graph'.
157168
158169
=item show_grid
159170
@@ -163,19 +174,14 @@ Either draw (1) or don't draw (0) the grid lines for the axis. Default is 1.
163174
164175
The color of the grid lines. Default is 'gray'.
165176
166-
=item grid_style
167-
168-
The line style of grid lines. This can be 'dashed', 'dotted', 'solid', etc.
169-
Default is 'solid'.
170-
171177
=item grid_alpha
172178
173179
The alpha value to use to draw the grid lines in Tikz. This is a number from
174180
0 (fully transparent) to 100 (fully solid). Default is 40.
175181
176182
=item axis_on_top
177183
178-
Configures if the axis should be drawn on top of the graph (1) or below the graph (0).
184+
Configures if the Tikz axis should be drawn on top of the graph (1) or below the graph (0).
179185
Useful when filling a region that covers an axis, if the axis are on top they will still
180186
be visible after the fill, otherwise the fill will cover the axis. Default: 0
181187
@@ -205,11 +211,11 @@ sub new {
205211
xaxis => {},
206212
yaxis => {},
207213
styles => {
208-
title => '',
209-
grid_color => 'gray',
210-
grid_style => 'solid',
211-
grid_alpha => 40,
212-
show_grid => 1,
214+
ariaLabel => 'Graph',
215+
ariaDescription => 'Generated graph',
216+
grid_color => 'gray',
217+
grid_alpha => 40,
218+
show_grid => 1,
213219
},
214220
@_
215221
}, $class;

lib/Plots/Data.pm

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
116116
a JavaScript or PGF function string. If the function contains any function
117117
tokens 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

191193
sub get_math_object {
@@ -249,22 +251,22 @@ sub update_min_max {
249251
}
250252

251253
sub 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

Comments
 (0)