#!/usr/bin/env Rscript
# Load necessary libraries
library(ggplot2)
library(readr)
library(dplyr)
library(plotly)
QEMU_SHIFT<-5
TIMESCALE<-1000000
# Function to create a Gantt chart with dots on short segments
create_gantt_chart <- function(csv_file, MIN_WIDTH, output_format = NULL) {
# Read the CSV file
df <- read_csv(csv_file)
# Ensure start and end columns are treated as integers
df <- df %>%
mutate(start = (as.integer(start) * 2**QEMU_SHIFT)/TIMESCALE,
end = (as.integer(end) * 2**QEMU_SHIFT)/TIMESCALE)
# Calculate the segment width
df <- df %>%
mutate(width = end - start)
# Sort the DataFrame by 'prio' column in descending order
df <- df %>%
arrange(prio)
df$label <- paste(
"Start:", df$start,
"
",
"Prio:", df$prio,
"
",
"Name:", df$name,
"
",
"State:", df$state_id,
"
",
"State:", df$state,
"
",
"End:", df$end
)
# Create the Gantt chart with ggplot2
p <- ggplot(df, aes(x = start, xend = end, y = reorder(name, prio), yend = name, text = label)) +
geom_segment(aes(color = factor(prio)), size = 6) +
labs(title = "Gantt Chart", x = "Time Step", y = "Task", color = "Priority") +
theme_minimal()
# Add dots on segments shorter than MIN_WIDTH
p <- p + geom_point(data = df %>% filter(width < MIN_WIDTH),
aes(x = start, y = name),
color = "red", size = 1)
# Convert the ggplot object to a plotly object for interactivity
p_interactive <- ggplotly(p)
# Handle output format
if (!is.null(output_format)) {
output_file <- sub("\\.csv$", paste0(".", output_format), csv_file)
if (output_format == "html") {
htmlwidgets::saveWidget(p_interactive, output_file)
} else if (output_format == "png") {
ggsave(output_file, plot = p, device = "png")
} else {
stop("Invalid output format. Use 'html' or 'png'.")
}
} else {
# Print the interactive Gantt chart
print(p_interactive)
}
}
# Main execution
args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 1 || length(args) > 2) {
stop("Usage: Rscript script.R [output_format]")
} else {
csv_file <- args[1]
if (length(args) == 2) {
output_format <- args[2]
} else {
output_format <- NULL
}
}
MIN_WIDTH <- 500 # You can set your desired minimum width here
create_gantt_chart(csv_file, MIN_WIDTH, output_format)