Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion htdocs/js/Plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,17 @@ const PGplots = {
options.xAxis.overrideOptions ?? {}
)
));
xAxis.defaultTicks.generateLabelText = plot.generateLabelText;

xAxis.defaultTicks.formatLabelText = plot.formatLabelText;

if (options.xAxis.ticks?.customLabels) {
xAxis.defaultTicks.generateLabelText = function (tick) {
return options.xAxis.ticks.customLabels[tick.usrCoords[1] / options.xAxis.ticks.distance - 1];
};
} else {
xAxis.defaultTicks.generateLabelText = plot.generateLabelText;
}

if (options.xAxis.location !== 'middle' && options.xAxis.name !== '') {
plot.xLabel = board.create(
'text',
Expand Down
1 change: 1 addition & 0 deletions lib/Plots/Axes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ sub axis_defaults {
tick_labels => 1,
tick_label_format => 'decimal',
tick_label_digits => 2,
tick_label_custom => undef, # NEW: Array of custom labels
tick_distance => 0,
tick_scale => 1,
tick_scale_symbol => '',
Expand Down
4 changes: 2 additions & 2 deletions lib/Plots/Data.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ stored in the C<< $data->{function} >> hash, though other data is stored as a st
);

Note, the first argument must be $self->context when called from C<Plots::Plot>
to use a single context for all C<Plost::Data> objects.
to use a single context for all C<Plots::Data> objects.

This is also used to set a two variable function (used for slope or vector fields):

Expand Down Expand Up @@ -116,7 +116,7 @@ Takes a MathObject C<$formula> and replaces the function with either
a JavaScript or PGF function string. If the function contains any function
tokens not supported, a warning and empty string is returned.

$formula The mathobject formula object, either $self->{function}{Fx} or $self->{function}{Fy}.
$formula The MathObject formula object, either $self->{function}{Fx} or $self->{function}{Fy}.
$type 'js' or 'PGF' (falls back to js for any input except 'PGF').
$xvar The x-variable name, $self->{function}{xvar}.
$yvar The y-variable name, $self->{function}{yvar}, for vector fields.
Expand Down
28 changes: 18 additions & 10 deletions lib/Plots/JSXGraph.pm
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,29 @@ sub HTML {
$options->{mathJaxTickLabels} = $axes->style('mathjax_tick_labels') if $xvisible || $yvisible;

if ($xvisible) {
$options->{xAxis}{name} = $axes->xaxis('label');
$options->{xAxis}{ticks}{show} = $axes->xaxis('show_ticks');
$options->{xAxis}{ticks}{labels} = $axes->xaxis('tick_labels');
$options->{xAxis}{ticks}{labelFormat} = $axes->xaxis('tick_label_format');
$options->{xAxis}{ticks}{labelDigits} = $axes->xaxis('tick_label_digits');
$options->{xAxis}{name} = $axes->xaxis('label');
$options->{xAxis}{ticks}{show} = $axes->xaxis('show_ticks');
$options->{xAxis}{ticks}{labels} = $axes->xaxis('tick_labels');
if ($axes->xaxis('tick_label_custom')) {
$options->{xAxis}{ticks}{customLabels} = $axes->xaxis('tick_label_custom');
} else {
$options->{xAxis}{ticks}{labelFormat} = $axes->xaxis('tick_label_format');
$options->{xAxis}{ticks}{labelDigits} = $axes->xaxis('tick_label_digits');
}
$options->{xAxis}{ticks}{scaleSymbol} = $axes->xaxis('tick_scale_symbol');
$options->{xAxis}{arrowsBoth} = $axes->xaxis('arrows_both');
$options->{xAxis}{overrideOptions} = $axes->xaxis('jsx_options') if $axes->xaxis('jsx_options');
}
if ($yvisible) {
$options->{yAxis}{name} = $axes->yaxis('label');
$options->{yAxis}{ticks}{show} = $axes->yaxis('show_ticks');
$options->{yAxis}{ticks}{labels} = $axes->yaxis('tick_labels');
$options->{yAxis}{ticks}{labelFormat} = $axes->yaxis('tick_label_format');
$options->{yAxis}{ticks}{labelDigits} = $axes->yaxis('tick_label_digits');
$options->{yAxis}{name} = $axes->yaxis('label');
$options->{yAxis}{ticks}{show} = $axes->yaxis('show_ticks');
$options->{yAxis}{ticks}{labels} = $axes->yaxis('tick_labels');
if ($axes->yaxis('tick_label_custom')) {
$options->{yAxis}{ticks}{customLabels} = $axes->yaxis('tick_label_custom');
} else {
$options->{yAxis}{ticks}{labelFormat} = $axes->yaxis('tick_label_format');
$options->{yAxis}{ticks}{labelDigits} = $axes->yaxis('tick_label_digits');
}
$options->{yAxis}{ticks}{scaleSymbol} = $axes->yaxis('tick_scale_symbol');
$options->{yAxis}{arrowsBoth} = $axes->yaxis('arrows_both');
$options->{yAxis}{overrideOptions} = $axes->yaxis('jsx_options') if $axes->yaxis('jsx_options');
Expand Down
12 changes: 12 additions & 0 deletions lib/Plots/Plot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,18 @@ sub add_arc {
return $self->_add_arc(@data);
}

sub add_rectangle {
my ($self, $pt0, $pt2, %options) = @_;

Value::Error('The first point must be an array ref of length 2')
unless ref($pt0) eq 'ARRAY' && scalar(@$pt0) == 2;
Value::Error('The second point must be an array ref of length 2')
unless ref($pt2) eq 'ARRAY' && scalar(@$pt2) == 2;
# If the fill_color option is set, set the fill to 'self'.
$options{fill} = 'self' if $options{fill_color} && !defined($options{fill});
return $self->add_dataset($pt0, [ $pt2->[0], $pt0->[1] ], $pt2, [ $pt0->[0], $pt2->[1] ], $pt0, %options);
}

sub add_vectorfield {
my ($self, @options) = @_;
my $data = Plots::Data->new(name => 'vectorfield');
Expand Down
7 changes: 2 additions & 5 deletions macros/core/PGbasicmacros.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2923,7 +2923,7 @@ sub image {
);
next;
}
if (ref $image_item eq 'Plots::Plot') {
if (ref $image_item eq 'Plots::Plot' || ref $image_item eq 'Plots::StatPlot') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worthwhile to make this more general, for future extension macros. Maybe call your package Plots::Plot::StatPlot, and then have this only check if ref $image_item starts with Plots::Plot, that way this won't have to be updated each time someone wants to extend Plots?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think there are some other places that might need to be updated, I recall having to modify a few places to check for the ref being equal to Plots::Plot beyond just this place (for some older image macros).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would be an isa call. Then any object that derives from a Plots::Plot object would work here. Unfortunately, to properly do that you need the Scalar::Util::blessed method which is not available here. The correct way to check if an object, say $object, derives from a particular class is if (blessed($object) && $object->isa('Parent::Package')).

When I was creating the SimpleGraph.pl macro, I almost made that package derive from the Plots::Plot package, and then wanted to make this code that way, but ran into the issue with the lack of the blessed method availability. I ended up going a different direction with the SimpleGraph.pl macro though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, line 2946 below needs to check the ref also.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DO NOT add this ref check to either macros/core/VectorField2D.pl or macros/graph/unionImage.pl. There are checks for the Plots::Plot object in those macros.

The VectorField2D.pl macro usage doesn't make sense for this statistical graph macro.

As to the unionImage.pl one, I told @somiaj not to add that there. It should be removed. No one should be using that macro anymore, and it should be moved into the deprecated folder.

# Update image attributes as needed.
$image_item->{width} = $width if $out_options{width};
$image_item->{height} = $height if $out_options{height};
Expand All @@ -2942,10 +2942,7 @@ sub image {
$width_ratio = 0.001 * $image_item->{tex_size};
}
$image_item = insertGraph($image_item)
if (ref $image_item eq 'WWPlot'
|| ref $image_item eq 'Plots::Plot'
|| ref $image_item eq 'PGlateximage'
|| ref $image_item eq 'PGtikz');
if (grep { ref $image_item eq $_ } ('WWPlot', 'Plots::Plot', 'Plots::StatPlot', 'PGlateximage', 'PGtikz'));
my $imageURL = alias($image_item) // '';
$imageURL = ($envir{use_site_prefix}) ? $envir{use_site_prefix} . $imageURL : $imageURL;
my $id = $main::PG->getUniqueName('img');
Expand Down
Loading