shosaco / vistime

Pretty timelines in R.
https://shosaco.github.io/vistime
GNU General Public License v3.0
168 stars 11 forks source link

Some event labels disappear when removing weekends with rangebreaks. #31

Closed Rob-Blanford closed 1 year ago

Rob-Blanford commented 1 year ago

I need to hide weekends when mapping out project durations. I can do this by modifying the plotly plot, "p", using the magrittr pipe with:

p %<>% layout(plot_bgcolor= '#e5ecf6', xaxis = list(showgrid=TRUE, gridwidth=1, gridcolor = 'slategray', tickvals = as.list(seq(date_window[1],date_window[2], 1)), tickangle = -70, tickformat="%a %d %b", side='top', rangebreaks=list(list(bounds=c('sat', 'mon')))), yaxis = list())

I've included the whole call for completeness, but the only important aspect is the rangebreaks.

For some bars, their label disappears. It is not a problem without the rangebreaks. I assume that the labels are somehow tied to the days that I've excuded and by not plotting the days, it's not plotting the text. Any idea how I can work around this?

Rob-Blanford commented 1 year ago

I've fixed this for me by sourcing the attached code to replace the plot_plotly function.

The tweak I made is here:

  #add annotations or not

  if (show_labels) {
    x_pos <- toAdd$start + (toAdd$end - toAdd$start) / 2 # in der Mitte
    x_pos <- x_pos - days(max(0, as.numeric(strftime(x_pos, format = "%u"))-5))
    p <- add_text(p,
                  x = x_pos,
                  y = toAdd$y,
                  textfont = list(family = "Arial", size = 14, color = toRGB(toAdd$fontcol)),
                  textposition = "center",
                  showlegend = F,
                  text = toAdd$label,
                  hoverinfo = "none"
    )
  }

In case you were interested in implementing removing weekends as a separate function, there is another small issue to consider. You need to push start and end dates off the weekends due to the same non-plotting issue. That can also mean that the titles are not printed centrally within the bar.

fix_vistime.txt