Skip to content

Commit

Permalink
Allow adding new x-values within original range
Browse files Browse the repository at this point in the history
Allow users to add a series to an existing plot when the new series
has x-values that do not exist in the original series. The only
restriction is that the new series cannot extend the x-axis beyond its
original start/end range.

The new behavior is consistent with chart_Series(). It looks like its
removal was a regression created when the 'observation.based = TRUE'
feature was implemented.

Fixes #360, see #216.
  • Loading branch information
joshuaulrich committed Dec 26, 2021
1 parent d1da3f0 commit 86c06fe
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,10 @@ chart.lines <- function(x,
if(length(lwd) < NCOL(x)) lwd <- rep(lwd, length.out = NCOL(x))
if(length(col) < NCOL(x)) col <- rep(col, length.out = NCOL(x))

# ensure series only has index values in xdata subset
xdataSubset <- xx$Env$xdata[xx$Env$xsubset]
y <- merge(x, .xts(,.index(xdataSubset)), join = "right")
xcoords <- xx$Env$xycoords$x

for(i in NCOL(y):1) {
lines(xcoords, y[,i], type=type, lend=lend, col=col[i],
for(i in NCOL(x):1) {
lines(xcoords, x[,i], type=type, lend=lend, col=col[i],
lty=lty[i], lwd=lwd[i], ...)
}
},
Expand Down Expand Up @@ -611,13 +608,19 @@ addSeries <- function(x, main="", on=NA, type="l", col=NULL, lty=1, lwd=1, pch=1
x$Env$x_grid_lines(xDataSubset, x$Env$grid.ticks.on, par("usr")[3:4])
}
# we can add points that are not necessarily at the points
# on the main series
if(xsubset == "") {
subset.range <- xsubset
} else {
subset.range <- paste(start(xDataSubset), end(xDataSubset),sep="/")
}
# on the main series, but need to ensure the new series only
# has index values within the xdata subset
subset.range <-
paste(format(start(xdata[xsubset]), "%Y%m%d %H:%M:%OS6"),
format(end(xdata[xsubset]), "%Y%m%d %H:%M:%OS6"),
sep = "/")

ta.y <- merge(ta, .xts(,.index(xDataSubset), tzone=tzone(xdata)))[subset.range]
# update xy-coordinates for chart.lines()
x$Env$xycoords <- xy.coords(.index(ta.y), ta.y[,1])
x$Env$xlim <- range(x$Env$xycoords$x, na.rm=TRUE)
x$Env$xstep <- diff(x$Env$xycoords$x[1:2])

chart.lines(ta.y, type=type, col=col, lty=lty, lwd=lwd, pch=pch, ...)
}

Expand Down

0 comments on commit 86c06fe

Please sign in to comment.