Dear R users
I need to auto scale the left y axis in the code below, so that when
I scroll left or right the left y-axis scale changes to accumulate the
range of the displayed data with in the max hight of the y-axis.
also how can I make the crosshair horizontal since it is only
vertical in this code. this code with a kind help from
"Gregory (Greg) L. Snow Ph.D."
just stated with tcl/tk and waiting for the book to arrive.
thank you
#**************** code starts ****************
library(tkrplot)
L <- length(myData);
tt <- tktoplevel()#cursor='crosshair')
left <- tclVar(1)
oldleft <- tclVar(1)
right <- tclVar(L)
cury <- tclVar(' ')
curx <- NA
tmpusr <- numeric(4)
tmpplt <- numeric(4)
f1 <- function(){
lleft <- as.numeric(tclvalue(left))
rright <- as.numeric(tclvalue(right))
x <- seq(lleft,rright,by=1)
par(bg='black', fg='green', col='white',
col.axis='white',
col.lab='magenta', col.main='blue',
col.sub='cyan')
plot(x,myData[x], type='s', ylim=range(myData))
## par(new=TRUE)
## plot(x,myData2[x], type='s', col='yellow',axes=F)
par(new=TRUE)
plot(x,myData3[x], type='s', col='cyan',axes=F)
axis(4)
tmpusr <<- par('usr')
tmpplt <<- par('plt')
if(!is.na(curx)){
abline(v=curx, col='red', lty=2)
points(curx,myData[curx],pch=16,col='red')
}
}
img <- tkrplot(tt, f1,hscale=2,vscale=1.2)
tkconfigure(img, cursor='crosshair')
f2 <- function(...){
ol <- as.numeric(tclvalue(oldleft))
tclvalue(oldleft) <- tclvalue(left)
r <- as.numeric(tclvalue(right))
tclvalue(right) <- as.character(r + as.numeric(...) - ol)
tkrreplot(img)
}
f3 <- function(...){
tkrreplot(img)
}
f4 <- function(...){
ol <- as.numeric(tclvalue(oldleft))
tclvalue(left) <- as.character(ol+10)
tclvalue(oldleft) <- as.character(ol+10)
r <- as.numeric(tclvalue(right))
tclvalue(right) <- as.character(r+10)
tkrreplot(img)
}
iw <- as.numeric(tcl('image','width',
tkcget(img,'-image')))
ih <-
as.numeric(tcl('image','height',tkcget(img,'-image')))
mm <- function(x,Y){
tx <- (as.numeric(x)-1)/iw
ty <- 1-(as.numeric(Y)-1)/ih
if( tx > tmpplt[1] & tx < tmpplt[2] &
ty > tmpplt[3] & ty < tmpplt[4] ){
newx <-
(tx-tmpplt[1])/(tmpplt[2]-tmpplt[1])*(tmpusr[2]-tmpusr[1])+tmpusr[1]
curx <<- round(newx)
tkrreplot(img)
newy <- myData[curx]
newy <- floor(newy) + (newy-floor(newy))*32/100
# newy2 <- myData2[curx]
newy3 <- myData3[curx]
tclvalue(cury) <- paste('x =',curx,'
myData=',round(newy,2)
,' myData3=',round(newy3,4))
}
}
tkbind(img, '<Motion>', mm)
l1 <- tklabel(tt, textvariable=cury)
s1 <- tkscale(tt, command=f2, from=1, to=length(myData),
variable=left, orient="horiz",label='left',length=700)
s2 <- tkscale(tt, command=f3, from=1, to=length(myData),
variable=right, orient="horiz",label='right',length=700)
b1 <- tkbutton(tt, text='->', command=f4)
tkpack(l1,img,s1,s2,b1)
#**************** code ends ****************
---------------------------------
[[alternative HTML version deleted]]