|
| 1 | +#!# |
| 2 | +# Copyright (c) 2025 Hangzhou Guanwaii Technology Co., Ltd. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT License, |
| 5 | +# which is located in the LICENSE file in the source tree's root directory. |
| 6 | +# |
| 7 | +# File: treemap.R |
| 8 | +# Author: mingcheng <mingcheng@apache.org> |
| 9 | +# File Created: 2025-10-25 23:41:07 |
| 10 | +# |
| 11 | +# Modified By: mingcheng <mingcheng@apache.org> |
| 12 | +# Last Modified: 2025-10-25 23:59:44 |
| 13 | +## |
| 14 | + |
| 15 | +# Load required packages |
| 16 | +if (!require("ggplot2", quietly = TRUE)) { |
| 17 | + install.packages("ggplot2") |
| 18 | + library(ggplot2) |
| 19 | +} |
| 20 | + |
| 21 | +if (!require("treemapify", quietly = TRUE)) { |
| 22 | + install.packages("treemapify") |
| 23 | + library(treemapify) |
| 24 | +} |
| 25 | + |
| 26 | +if (!require("showtext", quietly = TRUE)) { |
| 27 | + install.packages("showtext") |
| 28 | + library(showtext) |
| 29 | +} |
| 30 | + |
| 31 | +if (!require("svglite", quietly = TRUE)) { |
| 32 | + install.packages("svglite") |
| 33 | + library(svglite) |
| 34 | +} |
| 35 | + |
| 36 | +#' Generate Treemap Visualization |
| 37 | +#' |
| 38 | +#' Creates a treemap visualization of repository language distribution |
| 39 | +#' using ggplot2 and treemapify packages. |
| 40 | +#' |
| 41 | +#' @param username GitHub username |
| 42 | +#' @param repos Data frame with language statistics |
| 43 | +#' @param dir_name Output directory path |
| 44 | +#' @param json_data Raw repository data |
| 45 | +#' @export |
| 46 | +generate_treemap <- function(username, repos, dir_name, json_data) { |
| 47 | + # Setup font for visualization |
| 48 | + tryCatch( |
| 49 | + font_add("MapleMono", regular = "./assets/MapleMono-NF-CN-Regular.ttf"), |
| 50 | + error = function(e) { |
| 51 | + warning("Failed to load custom font, using default: ", e$message) |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + # Enable showtext |
| 56 | + showtext_auto() |
| 57 | + |
| 58 | + # Prepare data for treemap |
| 59 | + total_repos <- nrow(json_data) |
| 60 | + date_str <- format(Sys.Date(), "%Y-%m-%d") |
| 61 | + today <- format(Sys.Date(), "%Y%m%d") |
| 62 | + |
| 63 | + # Filter data: only keep languages with percentage >= 1% |
| 64 | + repos_filtered <- repos[repos$percentage >= 1, ] |
| 65 | + |
| 66 | + # If too many languages remain, keep only top 12 |
| 67 | + if (nrow(repos_filtered) > 12) { |
| 68 | + repos_filtered <- head(repos_filtered, 12) |
| 69 | + } |
| 70 | + |
| 71 | + message("Displaying ", nrow(repos_filtered), " languages in treemap (filtered >= 1%)") |
| 72 | + |
| 73 | + # Create the treemap plot |
| 74 | + p <- ggplot(repos_filtered, aes( |
| 75 | + area = final_score, |
| 76 | + fill = language, |
| 77 | + # label = paste0(language, "\n", percentage, "%\n(", repo_count, " repos)") |
| 78 | + label = paste0(language, "\n", percentage, "%") |
| 79 | + )) + |
| 80 | + geom_treemap(colour = "white", size = 2) + |
| 81 | + geom_treemap_text( |
| 82 | + aes(size = percentage), |
| 83 | + place = "centre", |
| 84 | + family = "MapleMono", |
| 85 | + min.size = 12 |
| 86 | + ) + |
| 87 | + scale_size_continuous(range = c(12, 36)) + |
| 88 | + labs( |
| 89 | + title = "Repository Distribution by Language", |
| 90 | + subtitle = sprintf( |
| 91 | + "GitHub User: %s | Total Repositories: %d | Updated on %s", |
| 92 | + username, total_repos, date_str |
| 93 | + ) |
| 94 | + ) + |
| 95 | + theme_minimal(base_family = "MapleMono") + |
| 96 | + theme( |
| 97 | + plot.title = element_text( |
| 98 | + hjust = 0.5, |
| 99 | + size = 24 |
| 100 | + ), |
| 101 | + plot.subtitle = element_text( |
| 102 | + hjust = 0.5, |
| 103 | + size = 10, |
| 104 | + colour = "#777777" |
| 105 | + ), |
| 106 | + legend.position = "none" |
| 107 | + ) |
| 108 | + |
| 109 | + # Define treemap file paths |
| 110 | + treemap_files <- list( |
| 111 | + svg = c( |
| 112 | + file.path(dir_name, paste0("treemap_", today, ".svg")), |
| 113 | + file.path(dir_name, "treemap_latest.svg") |
| 114 | + ), |
| 115 | + png = c( |
| 116 | + file.path(dir_name, paste0("treemap_", today, ".png")), |
| 117 | + file.path(dir_name, "treemap_latest.png") |
| 118 | + ) |
| 119 | + ) |
| 120 | + |
| 121 | + # Save SVG versions |
| 122 | + for (svg_file in treemap_files$svg) { |
| 123 | + ggsave( |
| 124 | + svg_file, |
| 125 | + plot = p, |
| 126 | + width = 10, |
| 127 | + height = 6, |
| 128 | + bg = "white" |
| 129 | + ) |
| 130 | + message("Treemap SVG saved to ", svg_file) |
| 131 | + } |
| 132 | + |
| 133 | + # Save PNG versions |
| 134 | + for (png_file in treemap_files$png) { |
| 135 | + ggsave( |
| 136 | + png_file, |
| 137 | + plot = p, |
| 138 | + width = 10, |
| 139 | + height = 6, |
| 140 | + dpi = 120, |
| 141 | + bg = "white" |
| 142 | + ) |
| 143 | + message("Treemap PNG saved to ", png_file) |
| 144 | + } |
| 145 | +} |
0 commit comments