Skip to content

Commit cfba117

Browse files
committed
Modify how height of Plots image is computed.
Height is undefined, and will be set equal to the width when the image is drawn unless the 'aspect_ratio' axes style is set. If this style is set, the height is computed to the desired aspect ratio based on the axes bounding box. If the height is manually set, use that instead of computing it.
1 parent 232b000 commit cfba117

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/Plots/Axes.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ The following styles configure aspects about the axes.
151151
152152
=over 5
153153
154+
=item aspect_ratio
155+
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+
154160
=item ariaLabel
155161
156162
The ARIA label in JSX graph output. Default is 'Graph'.

lib/Plots/Plot.pm

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ use Plots::GD;
2020

2121
sub new {
2222
my ($class, %options) = @_;
23-
my $size = eval('$main::envir{onTheFlyImageSize}') || 350;
2423

2524
my $self = bless {
2625
imageName => {},
27-
width => $size,
28-
height => $size,
26+
width => eval('$main::envir{onTheFlyImageSize}') || 350,
27+
height => undef,
2928
tex_size => 600,
3029
axes => Plots::Axes->new,
3130
colors => {},
@@ -109,8 +108,20 @@ sub color_init {
109108
}
110109

111110
sub size {
112-
my $self = shift;
113-
return wantarray ? ($self->{width}, $self->{height}) : [ $self->{width}, $self->{height} ];
111+
my $self = shift;
112+
my $axes = $self->axes;
113+
my $width = $self->{width};
114+
my $height = $self->{height};
115+
unless ($height) {
116+
if ($axes->style('aspect_ratio')) {
117+
my $x_size = $axes->xaxis('max') - $axes->xaxis('min');
118+
my $y_size = $axes->yaxis('max') - $axes->yaxis('min');
119+
$height = int($axes->style('aspect_ratio') * $width * $y_size / $x_size);
120+
} else {
121+
$height = $width;
122+
}
123+
}
124+
return wantarray ? ($width, $height) : [ $width, $height ];
114125
}
115126

116127
sub data {

lib/Plots/Tikz.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ sub configure_axes {
157157
[
158158
trig format plots=rad,
159159
view={0}{90},
160+
scale only axis,
160161
height=$axes_height,
161162
width=$axes_width,
162163
${axis_on_top}axis x line=$axis_x_line$axis_x_pos,

0 commit comments

Comments
 (0)