Skip to content

Commit 6913356

Browse files
committed
Update bugs for addPoints and addLines
Originally, argument "x" is mixed up with the x representing current chart passed to chartPoints and chartLines, which makes points and lines change with "x" continuously whenever new addPoints() and addLines() are called. New arguments are specified to chartPoints and chartLines to distinguish from x representing the current chart.
1 parent cf5950d commit 6913356

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

R/addTA.R

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,55 +1646,53 @@ function(x) {
16461646
if(missing(v)) v <- NULL
16471647

16481648
lenv <- new.env()
1649-
lenv$chartLines <- function(x, h, v, on, overlay, col) {
1650-
xdata <- x$Env$xdata
1649+
lenv$chartLines <- function(x, series, h, v, on, overlay, col) {
16511650
xsubset <- x$Env$xsubset
1652-
xdata <- cbind(Hi(xdata),Lo(xdata))
1653-
lines <- x$Env$TA$lines[xsubset]
1651+
series <- series[which(.index(series) %in% .index(x$Env$xdata[xsubset]))]
1652+
x.points <- which(.index(x$Env$xdata[xsubset]) %in% .index(series))
16541653
spacing <- x$Env$theme$spacing
16551654
xlim <- x$Env$xlim
16561655
ylim <- x$get_ylim()[[abs(on)+1L]]
16571656
theme <- x$Env$theme
1658-
y_grid_lines <- x$Env$y_grid_lines
1657+
y_grid_series <- x$Env$y_grid_series
16591658

16601659
if(!overlay) {
1661-
ylim <- range(lines[,1], na.rm=TRUE) * 1.05
1660+
ylim <- range(series[,1], na.rm=TRUE) * 1.05
16621661
# add inbox color
16631662
rect(xlim[1], ylim[1], xlim[2], ylim[2], col=theme$fill)
1664-
# add grid lines and left-side axis labels
1665-
segments(xlim[1], y_grid_lines(ylim),
1666-
xlim[2], y_grid_lines(ylim),
1663+
# add grid series and left-side axis labels
1664+
segments(xlim[1], y_grid_series(ylim),
1665+
xlim[2], y_grid_series(ylim),
16671666
col = theme$grid, lwd = x$Env$grid.ticks.lwd, lty = 3)
1668-
text(xlim[1], y_grid_lines(ylim), y_grid_lines(ylim),
1667+
text(xlim[1], y_grid_series(ylim), y_grid_series(ylim),
16691668
col = theme$labels, srt = theme$srt,
16701669
offset = 0.5, pos = 2, cex = theme$cex.axis, xpd = TRUE)
16711670
# add border of plotting area
16721671
rect(xlim[1], ylim[1], xlim[2], ylim[2], border=theme$labels)
16731672
}
1674-
if(!is.null(lines)) {
1675-
# draw lines given positions specified in x
1676-
x.pos <- 1 + spacing * (1:nrow(lines) - 1)
1677-
lines(x.pos, lines[,1],col=col)
1673+
if(!is.null(series)) {
1674+
# draw series given positions specified in x
1675+
lines(x.points,series[,1],col=col)
16781676
}
16791677
if(!is.null(h)) {
1680-
# draw horizontal lines given positions specified in h
1678+
# draw horizontal series given positions specified in h
16811679
segments(xlim[1],h,xlim[2],h,col=col)
16821680
}
16831681
if(!is.null(v)) {
1684-
# draw vertical lines given positions specified in v
1682+
# draw vertical series given positions specified in v
16851683
segments((v-1)*spacing+1,ylim[1],(v-1)*spacing+1,ylim[2],col=col)
16861684
}
16871685
}
16881686
mapply(function(name, value) {
16891687
assign(name, value, envir = lenv)
1690-
}, names(list(h = h, v = v, on = on, overlay = overlay, col = col)),
1691-
list(h = h, v = v, on = on, overlay = overlay, col = col))
1688+
}, names(list(x = x, h = h, v = v, on = on, overlay = overlay, col = col)),
1689+
list(x = x, h = h, v = v, on = on, overlay = overlay, col = col))
16921690
exp <- parse(text = gsub("list", "chartLines", as.expression(substitute(list(x = current.chob(),
1691+
series = get("x"),
16931692
h = h, v = v, on = on, overlay = overlay, col = col)))), srcfile = NULL)
16941693
lchob <- current.chob()
16951694
ncalls <- length(lchob$Env$call_list)
16961695
lchob$Env$call_list[[ncalls + 1]] <- match.call()
1697-
lchob$Env$TA$lines <- x
16981696

16991697
if(overlay) {
17001698
lchob$set_frame(sign(on)*(abs(on)+1L))
@@ -1738,16 +1736,15 @@ function(x) {
17381736
on=1,overlay=TRUE) {
17391737

17401738
lenv <- new.env()
1741-
lenv$chartPoints <- function(x, type, pch, offset, col, bg, cex, on, overlay) {
1739+
lenv$chartPoints <- function(x, x.points, y.points, type, pch, offset, col, bg, cex, on, overlay) {
17421740
xdata <- x$Env$xdata
17431741
xsubset <- x$Env$xsubset
1744-
if(is.xts(x$Env$x.points)) {
1745-
y.points <- x$Env$x.points[.index(x$Env$x.points) %in% .index(xdata[xsubset])]
1746-
x.points <- which(.index(xdata[xsubset]) %in% .index(x$Env$x.points))
1742+
if(is.xts(x.points)) {
1743+
y.points <- x.points[.index(x.points) %in% .index(xdata[xsubset])]
1744+
x.points <- which(.index(xdata[xsubset]) %in% .index(x.points))
17471745
}
17481746
else {
1749-
x.points <- which(.index(xdata[xsubset]) %in% .index(xdata[x$Env$x.points]))
1750-
y.points <- x$Env$y.points
1747+
x.points <- which(.index(xdata[xsubset]) %in% .index(xdata[x.points]))
17511748
}
17521749
spacing <- x$Env$theme$spacing
17531750
# if OHLC and above - get Hi, else Lo
@@ -1758,7 +1755,10 @@ function(x) {
17581755
} else Lo(xdata)
17591756
} else xdata
17601757

1761-
if(is.null(y.points)) y.points <- y.data[x.points] * offset
1758+
if(is.null(y.points))
1759+
y.points <- y.data[x.points] * offset
1760+
else
1761+
y.points <- y.points[.index(y.points) %in% .index(xdata[xsubset])] * offset
17621762

17631763
if(!overlay) {
17641764
xlim <- x$Env$xlim
@@ -1782,11 +1782,12 @@ function(x) {
17821782
points(x=x.points, y=y.points, type=type,pch=pch,col=col,bg=bg,cex=cex)
17831783
}
17841784
mapply(function(name,value) { assign(name,value,envir=lenv) },
1785-
names(list(type = type, pch = pch, offset = offset, col = col,
1786-
bg = bg, cex = cex, on = on, overlay = overlay)),
1787-
list(type = type, pch = pch, offset = offset, col = col,
1788-
bg = bg, cex = cex, on = on, overlay = overlay))
1785+
names(list(x = x, y = y, type = type, pch = pch, offset = offset,
1786+
col = col, bg = bg, cex = cex, on = on, overlay = overlay)),
1787+
list(x = x, y = y, type = type, pch = pch, offset = offset,
1788+
col = col, bg = bg, cex = cex, on = on, overlay = overlay))
17891789
exp <- parse(text=gsub("list","chartPoints",as.expression(substitute(list(x=current.chob(),
1790+
x.points=get("x"), y.points=get("y"),
17901791
type = type, pch = pch, offset = offset, col = col,
17911792
bg = bg, cex = cex, on = on, overlay = overlay)))),
17921793
srcfile=NULL)
@@ -1798,10 +1799,6 @@ function(x) {
17981799

17991800
if(!is.null(y))
18001801
if(NROW(x) != NROW(y)) stop('x and y must be of equal lengths')
1801-
1802-
lchob$Env$x.points <- x
1803-
lchob$Env$y.points <- y
1804-
18051802

18061803
if(overlay)
18071804
lchob$set_frame(on+1)

0 commit comments

Comments
 (0)