Skip to content

Commit

Permalink
add arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Dec 3, 2023
1 parent 0a0a6b8 commit ef19dbf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
32 changes: 25 additions & 7 deletions R/git_commits.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
# -end End date. By default it is the current date. The value can be a string such as "2022-01-01" or a ``Date`` object.
# -pt_range Range of the point sizes.
# -commits_range Range of the numbers of commits.
# -type Type of the plot.
# -colors If type is the heatmap, it controls the list of colors.
#
spiral_git_commits = function(repo = ".", show_legend = TRUE,
start = NULL, end = Sys.Date(),
pt_range = c(2, 16), commits_range = c(2, 20)) {
pt_range = c(2, 16), commits_range = c(2, 20),
type = c("points", "heatmap"),
colors = c("#3288BD", "#99D594", "#E6F598", "#FFFFBF", "#FEE08B", "#FC8D59", "#D53E4F")) {

type = match.arg(type)[1]

df_all = list()
for(rp in repo) {
Expand Down Expand Up @@ -70,9 +76,11 @@ spiral_git_commits = function(repo = ".", show_legend = TRUE,
ind = as.double(difftime(df_all[i, "date"], start), "days") + 1
n[ind] = n[ind] + df_all[i, "commits"]
}
l = n > 0
n = n[l]
d = d[l]
if(type == "points") {
l = n > 0
n = n[l]
d = d[l]
}

pt_range = pt_range + 0
commits_range = commits_range + 0
Expand All @@ -83,12 +91,18 @@ spiral_git_commits = function(repo = ".", show_legend = TRUE,
pt_size
}

col_fun = circlize::colorRamp2(seq(max(min(n), commits_range[1]), min(max(n), commits_range[2]), length = length(colors)), colors)

spiral_initialize_by_time(c(start, end), verbose = FALSE, normalize_year = TRUE)
spiral_track()
spiral_points(d, 0.5, pch = 16, size = unit(calc_pt_size(n), "pt"))
if(type == "points") {
spiral_points(d, 0.5, pch = 16, size = unit(calc_pt_size(n), "pt"))
} else {
spiral_rect(d-0.5, 0, d+0.5, 1, gp = gpar(fill = col_fun(n), col = NA))
}

for(y in start_year:end_year) {
spiral_text(paste0(y, "-01-01"), 0.5, y, gp = gpar(fontsize = 8, col = "#808080"), facing = "inside")
spiral_text(paste0(y, "-01-01"), 0.5, y, gp = gpar(fontsize = 8, col = ifelse(type == "points", "#808080", "black")), facing = "inside")
}

upViewport()
Expand All @@ -104,7 +118,11 @@ spiral_git_commits = function(repo = ".", show_legend = TRUE,
if(max(n) > commits_range[2]) {
labels[length(labels)] = paste0("[", labels[length(labels)], ", ", max(n), "]")
}
lgd = ComplexHeatmap::Legend(title = "#commits", at = breaks, labels = labels, type = "points", pch = 16, size = unit(calc_pt_size(breaks), "pt"))
if(type == "points") {
lgd = ComplexHeatmap::Legend(title = "#commits", at = breaks, labels = labels, type = "points", pch = 16, size = unit(calc_pt_size(breaks), "pt"))
} else {
lgd = ComplexHeatmap::Legend(title = "#commits", at = breaks, labels = labels, col_fun = col_fun)
}
ComplexHeatmap::draw(lgd, x = unit(0, "npc") + unit(4, "pt"), y = unit(1, "npc") - unit(24, "pt"), just = c("left", "top"))
}

Expand Down
6 changes: 5 additions & 1 deletion man/spiral_git_commits.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Visualize git commits
\usage{
spiral_git_commits(repo = ".", show_legend = TRUE,
start = NULL, end = Sys.Date(),
pt_range = c(2, 16), commits_range = c(2, 20))
pt_range = c(2, 16), commits_range = c(2, 20),
type = c("points", "heatmap"),
colors = c("#3288BD", "#99D594", "#E6F598", "#FFFFBF", "#FEE08B", "#FC8D59", "#D53E4F"))
}
\arguments{

Expand All @@ -19,6 +21,8 @@ spiral_git_commits(repo = ".", show_legend = TRUE,
\item{end}{End date. By default it is the current date. The value can be a string such as "2022-01-01" or a \code{Date} object.}
\item{pt_range}{Range of the point sizes.}
\item{commits_range}{Range of the numbers of commits.}
\item{type}{Type of the plot.}
\item{colors}{If type is the heatmap, it controls the list of colors.}

}
\examples{
Expand Down

0 comments on commit ef19dbf

Please sign in to comment.