Maxim Volgin
Maxim Volgin

Reputation: 4077

how to hide a part of a Mermaid Gantt chart before today (or any date), i.e. render only a specific time range?

how to hide a part of a Mermaid Gantt chart before today (or any date) or after some date, purely with Mermaid syntax?

context: in a long project plan, hide what's not relevant anymore/yet, render only a specific time range

Upvotes: 1

Views: 894

Answers (1)

Grasshopper_NZ
Grasshopper_NZ

Reputation: 765

The definition of gantt chart, according to wikipedia, is:

A Gantt chart is a type of bar chart that illustrates a project schedule. ... Gantt charts illustrate the start and finish dates of the terminal elements and summary elements of a project.

While the past tasks have been completed, remove them may distort the overview of the project (as we don't know when did it start, and how long it took so far), and make gantt chart less meaningful.

What about add a marker shows today? In this way, we can get a clear sense of when are we now, in relation to the past and future without losing information from the past.

Default Gantt chart with todayMarker

Here are the example code from Mermaid Live Editor:

gantt
    title A Gantt Diagram
    dateFormat  YYYY-MM-DD
    todayMarker on
    section Section
    A task           :a1, 2022-11-01, 30d
    Another task     :after a1  , 20d
    section Another
    Task in sec      :2022-12-12  , 12d
    another task      : 24d

The only two thing you need to change are:

  1. Add the line todayMarker on, and 2. Update the date to make sure today is within the range.

Option B: Using R to reshape the Mermaid code

If you need a strict truncated Gantt chart, and are familiar with R, it is possible to achieve it by detecting and deleting lines with dates that smaller than today (i.e. from the past), and only keep current and future dates.

library(tidyverse)

# 1. Save mermaid code as a vector:

my_text <- "gantt
title A Gantt Diagram with today marker
dateFormat  YY-MM-DD
todayMarker on
section Section
task 1 :a1, 2022-11-20, 10d
task 2 :a2, 2022-11-30, 10d
task 3 :a3, 2022-12-20, 10d
task 4 :a4, 2022-12-30, 10d"


# 2. Separate each element by the newline mark (\n):

my_text <- my_text |> 
  str_split(pattern = "\n") 


# 3. Extract the vector and add a few testing columns:

my_text <- tibble(
  code = my_text[[1]]      # Extract mermaid code
) |> 
  mutate(
    date = str_extract(    # Find rows with date
      string = code,
      pattern = "\\d{4}-\\d{2}-\\d{2}"
    ),
    date = as.POSIXct(date), # Transfrom to date format 
    to_keep = case_when(     # Logical variable to verify from the past, or from future 
      Sys.Date() < date ~ "Keep",
      is.na(date) ~ "Keep",  # keep non-date rows 
      TRUE ~ "delete"
    )
  )


# 4. Only keep lines that are non-date, or with dates which are greater than today:

new_text <- my_text |> 
  filter(to_keep == "Keep") |> 
  select(code) 


# 5. Prepare mermaid code:
mermaid_text <- str_c(new_text$code, collapse = "\n")
writeLines(mermaid_text)

Before, we have:

enter image description here

Copy and paste the outcome from writeLines to Mermaid Live Editor, you will then see a truncated Gantt chart:

enter image description here

Please notice that, by doing this the full picture of Gantt will be lost.

Upvotes: 0

Related Questions