From 26b604a679d932fefbac668e1bca7ce52139f220 Mon Sep 17 00:00:00 2001 From: Joshua Ulrich Date: Thu, 19 Oct 2023 15:39:44 -0500 Subject: [PATCH] Refactor 'ylim_render' calculation It's not necessary to loop over the panels twice. --- R/plot.R | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/R/plot.R b/R/plot.R index f9ed21a4..337ab18e 100644 --- a/R/plot.R +++ b/R/plot.R @@ -1271,21 +1271,18 @@ new.replot_xts <- function(panel=1,asp=1,xlim=c(1,10),ylim=list(structure(c(1,10 update_panels <- function(headers=TRUE) { + # Recalculate each panel's 'ylim_render' value based on the + # 'xdata' of every action in the panel for (panel_n in seq_along(Env$panels)) { - panel <- Env$panels[[panel_n]] - if (!panel$use_fixed_ylim) { - # set ylim to +/-Inf when use_fixed_ylim is FALSE - # it will be updated to include all the panel's data - panel$ylim_render <- c(Inf, -Inf) - } - } - # reset all ylim values, by looking for range(env[[1]]$xdata) - # xdata should be either coming from Env or if lenv, lenv - for (panel_n in seq_along(Env$panels)) { panel <- get_panel(panel_n) if (!panel$use_fixed_ylim) { + # set 'ylim_render' to +/-Inf when ylim is NOT fixed, so + # it will be updated to include all the panel's data + panel$ylim_render <- c(Inf, -Inf) + + # calculate a new ylim based on all the panel's data for (action in panel$actions) { action_update_ylim <- attr(action, "update_ylim") @@ -1296,7 +1293,8 @@ new.replot_xts <- function(panel=1,asp=1,xlim=c(1,10),ylim=list(structure(c(1,10 # some actions (e.g. addLegend) do not have 'xdata' dat.range <- create_ylim(action_data[Env$xsubset]) - # re-retrieve ylim; actions may have changed it + # calculate new ylim based on the combination of the panel's + # original ylim and the action's 'xdata' ylim new_ylim <- c(min(panel$ylim[1], dat.range, na.rm = TRUE), max(panel$ylim[2], dat.range, na.rm = TRUE))