Skip to content

Commit ef12cce

Browse files
committed
Fix zoom_Chart to draw subset series
New chart.lines function is given to fix error: 'x' and 'y' lengths differ. As x-axis and y-axis will not change with the subset series when using plot_object$subset(), x-axis is fixed first. x.pos is passed to x limit to control the number of subset series. y limit is reset by specifying structured range of subset series.
1 parent de872eb commit ef12cce

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

R/chart_Series.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,47 @@ chart_Series <- function(x,
318318

319319
# zoom_Chart {{{
320320
zoom_Chart <- function(subset) {
321+
# refactor xts:::chart.lines to make subset functionality work
322+
chart.lines <- function (x, type = "l", lty = 1, lwd = 2, lend = 1, col = 1:10,
323+
up.col = NULL, dn.col = NULL, legend.loc = NULL, ...)
324+
{
325+
if (is.null(up.col))
326+
up.col <- "green"
327+
if (is.null(dn.col))
328+
dn.col <- "red"
329+
xx <- xts:::current.xts_chob()
330+
switch(type, h = {
331+
colors <- ifelse(x[, 1] < 0, dn.col, up.col)
332+
lines(xx$Env$xycoords$x[match(index(x), index(xx$Env$xdata))], x[, 1], lwd = 2, col = colors,
333+
lend = lend, lty = 1, type = "h", ...)
334+
}, p = , l = , b = , c = , o = , s = , S = , n = {
335+
if (length(lty) < NCOL(x)) lty <- rep(lty, length.out = NCOL(x))
336+
if (length(lwd) < NCOL(x)) lwd <- rep(lwd, length.out = NCOL(x))
337+
if (length(col) < NCOL(x)) col <- rep(col, length.out = NCOL(x))
338+
for (i in NCOL(x):1) {
339+
lines(xx$Env$xycoords$x[match(index(x), index(xx$Env$xdata))], x[, i], type = type, lend = lend,
340+
col = col[i], lty = lty[i], lwd = lwd[i], ...)
341+
}
342+
}, {
343+
warning(paste(type, "not recognized. Type must be one of\n
344+
'p', 'l', 'b, 'c', 'o', 'h', 's', 'S', 'n'.\n
345+
plot.xts supports the same types as plot.default,\n
346+
see ?plot for valid arguments for type"))
347+
})
348+
if (!is.null(legend.loc)) {
349+
lc <- legend.coords(legend.loc, xx$Env$xlim, range(x,
350+
na.rm = TRUE))
351+
legend(x = lc$x, y = lc$y, legend = colnames(x), xjust = lc$xjust,
352+
yjust = lc$yjust, fill = col[1:NCOL(x)], bty = "n")
353+
}
354+
}
321355
chob <- current.chob()
356+
x <- chob$Env$xdata
357+
x.pos <- 1:NROW(x[subset])
358+
chob$Env$chart.lines <- chart.lines
322359
chob$subset(subset)
360+
chob$Env$xlim <- range(x.pos)
361+
chob$Env$ylim[[2]] <- structure(range(x[subset], na.rm=TRUE), fixed=TRUE)
323362
chob
324363
}
325364
# }}}

0 commit comments

Comments
 (0)