Home > Back-end >  How do I word wrap in timevis?
How do I word wrap in timevis?

Time:05-08

I've made a gantt chart with timevis in R. Some of the bars in my gantt chart have quite long text in, so anything after about the 10th word is cut off. Is there a way to wrap the words into second or third lines?

gantt <- structure(list(group = c("How FAN1 stabilises repeats", "How FAN1 stabilises repeats"
), content = c("CRISPR nuclease and SPYF", "Effect of FAN1 SPAA and MIM mutant on MLH1 binding, ICL repair and CAG instability"
), start = structure(c(1662516351.36, 1662516351.36), tzone = "UTC", class = c("POSIXct", 
                                                                               "POSIXt")), end = structure(c(1677242989.44, 1677242989.44), tzone = "UTC", class = c("POSIXct", 
                                                                                                                                                                     "POSIXt")), style = c("color: white; background-color: maroon;font-size:9px;word-break: break-all;", 
                                                                                                                                                                                           "color: white; background-color: maroon;font-size:9px;word-break: break-all;"
                                                                                                                                                                     ), type = c("range", "range")), row.names = c(NA, -2L), class = c("tbl_df", 
                                                                                                                                                                                                                                       "tbl", "data.frame"))    


groups <- data.frame(id = unique(gantt$group),
                     content = unique(gantt$group),
                     style = "font-weight: bold; font-size:10px")

timevis(data = gantt, groups = groups,
        options = list(editable = TRUE, height = "600px"))

CodePudding user response:

You can use stringr::str_wrap to nicely wrap the text to the desired maximum width. It works by inserting newline characters which won't work in this context though, so you'll need to substitute these with <br>:

library(tidyverse)

gantt %>%
  mutate(content = gsub("\n", "<br>", str_wrap(content, 30))) %>%
  timevis(groups = groups, options = list(editable = TRUE, height = "600px"))

enter image description here

  • Related