statnet / ndtv

ndtv: Network Dynamic Temporal Visualizations in R
https://cran.r-project.org/web/packages/ndtv/index.html
49 stars 5 forks source link

add function for applying post-processing layout transformations #17

Open skyebend opened 8 years ago

skyebend commented 8 years ago

(transfered from statnet trac ticket #1212) there are several function like layout.normalize that transform coords. But can be difficult to include within the layout process. Would be useful to be able to 'lapply' them to all of the slices after a layout has been created

perhaps coordApply ?

below is a draft I wrote for layout.normalize, could easily be adapted to be more general.

# function to re-process all the coordinates and scale them to the same interval
rescaleAnimation<-function(nd){
  # get the slicing params
  slice.par<-nd%n%'slice.par'
  starts<-seq(from=slice.par$start, to=slice.par$end,by = slice.par$interval)
  ends<-seq(from=slice.par$start+slice.par$aggregate.dur,to=slice.par$end+slice.par$aggregate.dur, by = slice.par$interval)
  for(s in seq_along(starts)){
    message('rescaling step ',s)
    coords<-cbind(get.vertex.attribute.active(nd,'animation.x',onset=starts[s],terminus=ends[s]),
                  get.vertex.attribute.active(nd,'animation.y',onset=starts[s],terminus=ends[s]))
    coords<-layout.normalize(coords)
    activate.vertex.attribute(nd,'animation.x',coords[,1],onset=starts[s],terminus=ends[s])
    activate.vertex.attribute(nd,'animation.y',coords[,2],onset=starts[s],terminus=ends[s])
  }
  return(nd)
}