/
TimeBoxes.R
62 lines (59 loc) · 1.76 KB
/
TimeBoxes.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# constructor of timeboxes
timeboxes<-function()
{
timebox<-list()
class(timebox)<-"timeboxes"
timebox
}
# this method adds boxes to a line in timeboxes
addBox.timeboxes<-function(data, line, box)
{
if (is.null(data[[line]]))
data[[line]]<-matrix(box, nrow=1, ncol=length(box))
else
data[[line]]<-rbind(data[[line]], box)
data
}
#it shows the time boxes given the timebox data. The lineGroupNum is the number of groups,
# the linesPerGroup is the number of timeboxes in a group. The lineNameConv parameter can be used to
# transform the names of groups (example had431->431)
plot.timeboxes<-function(data, lineGroupNum=length(data), linesPerGroup=1, sortGroups=TRUE, colors=c("green", "darkorange", "magenta", "blue"), ylim=c(0,lineGroupNum+0.5), lineNameConv=identity)
{
cols=ncol(data[[1]])
for(i in 1:length(data))
{
data[[i]]<-matrix(data[[i]][sort.list(data[[i]][,1]), ], ncol=cols)
}
if (sortGroups)
data<-data[order(names(data))]
minx=.Machine$double.xmax
maxx=0
for(n in 1:length(data))
{
mi<-min(data[[n]][data[[n]]>=0])
if (minx > mi )
minx <- mi
ma<-max(data[[n]][data[[n]]>=0])
if (maxx < ma )
maxx <- ma
}
plot(1, type="n", xlim=c(minx-0.5,maxx+0.5), ylim=ylim, xlab="time (ms)",ylab="nodes")
axis(4,at=(1:length(data))-0.5,labels=lineNameConv(names(data)))
for(i in 1:lineGroupNum)
{
lines(c(minx-0.5,maxx+0.5),c(i,i))
}
for(n in 1:length(data))
{
for(r in 1:nrow(data[[n]]))
{
for(c in 1:(ncol(data[[n]])-1))
{
col<-colors[c]
if ( data[[n]][r,c]!=-1 && data[[n]][r,c+1]!=-1)
#if ( data[[n]][r,c+1]!=-1)
rect(data[[n]][r,c],((n-1)*linesPerGroup+((r-1)%%linesPerGroup))/linesPerGroup,data[[n]][r,c+1],((n-1)*linesPerGroup+((r-1)%%linesPerGroup+1))/linesPerGroup, col=col)
}
}
}
}