@@ -31,6 +31,32 @@ document.addEventListener('DOMContentLoaded', function() {
31
31
directoriesList . innerHTML = '<li class="card">Failed to load directories.</li>' ;
32
32
}
33
33
}
34
+ // Function to fetch and count subdirectories for each directory
35
+ async function fetchSubdirectoryCounts ( ) {
36
+ try {
37
+ const directoriesResponse = await fetch ( '/api/github/repos' ) ;
38
+ if ( ! directoriesResponse . ok ) {
39
+ throw new Error ( `HTTP error! status: ${ directoriesResponse . status } ` ) ;
40
+ }
41
+ const directoriesData = await directoriesResponse . json ( ) ;
42
+ const directories = directoriesData . filter ( item => item . type === 'dir' && item . name !== 'Website' ) ;
43
+ const directoryCounts = [ ] ;
44
+
45
+ for ( const directory of directories ) {
46
+ const subResponse = await fetch ( `https://api.github.com/repos/recodehive/machine-learning-repos/contents/${ directory . name } ` ) ;
47
+ if ( ! subResponse . ok ) {
48
+ throw new Error ( `HTTP error! status: ${ subResponse . status } for ${ directory . name } ` ) ;
49
+ }
50
+ const subData = await subResponse . json ( ) ;
51
+ const subDirectoriesCount = subData . filter ( item => item . type === 'dir' ) . length ;
52
+ directoryCounts . push ( { name : directory . name , count : subDirectoriesCount } ) ;
53
+ }
54
+
55
+ return directoryCounts ;
56
+ } catch ( error ) {
57
+ console . error ( 'Error fetching subdirectory counts:' , error ) ;
58
+ }
59
+ }
34
60
35
61
// Function to toggle languages section
36
62
function toggleLanguagesSection ( ) {
@@ -76,14 +102,12 @@ document.addEventListener('DOMContentLoaded', function() {
76
102
for ( const [ language , bytes ] of Object . entries ( languagesData ) ) {
77
103
const percentage = ( ( bytes / totalBytes ) * 100 ) . toFixed ( 2 ) ;
78
104
const listItem = document . createElement ( 'li' ) ;
79
- listItem . classList . add ( 'card-languages' ) ;
80
- const h3 = document . createElement ( 'h3' ) ;
81
- h3 . textContent = `${ language } ` ;
82
- listItem . appendChild ( h3 ) ;
83
- // listItem.innerHTML = `
84
- // <h3>${language}</h3>
85
- // <div class="progress-bar" style="width: ${percentage}%;"></div>
86
- // `;
105
+ listItem . innerHTML = `
106
+ <span>${ language } </span>
107
+ <div class="progress-bar-container">
108
+ <div class="progress-bar" style="width: ${ percentage } %;">${ percentage } %</div>
109
+ </div>
110
+ ` ;
87
111
languageList . appendChild ( listItem ) ;
88
112
89
113
if ( bytes > mostUsedLanguage . bytes ) {
@@ -97,7 +121,47 @@ document.addEventListener('DOMContentLoaded', function() {
97
121
console . error ( 'Error fetching data from GitHub API:' , error ) ;
98
122
}
99
123
}
100
-
124
+ async function createPieChart ( ) {
125
+ const directoryCounts = await fetchSubdirectoryCounts ( ) ;
126
+ const ctx = document . getElementById ( 'milestoneChart' ) . getContext ( '2d' ) ;
127
+ const labels = directoryCounts . map ( dir => dir . name ) ;
128
+ const data = directoryCounts . map ( dir => dir . count ) ;
129
+
130
+ const chart = new Chart ( ctx , {
131
+ type : 'pie' ,
132
+ data : {
133
+ labels : labels ,
134
+ datasets : [ {
135
+ data : data ,
136
+ backgroundColor : [
137
+ '#FF6384' ,
138
+ '#36A2EB' ,
139
+ '#FFCE56' ,
140
+ '#4BC0C0' ,
141
+ '#9966FF' ,
142
+ '#FF9F40' ,
143
+ '#01A02A' ,
144
+ '#FA5F20'
145
+ ] ,
146
+ } ]
147
+ } ,
148
+ options : {
149
+ responsive : true ,
150
+ plugins : {
151
+ legend : {
152
+ display : false // Disable Chart.js default legend
153
+ }
154
+ }
155
+ }
156
+ } ) ;
157
+
158
+ // Inject custom legend
159
+ const legendContainer = document . querySelector ( '.legend' ) ;
160
+ legendContainer . innerHTML = labels . map ( ( label , index ) => `
161
+ <span style="color: ${ chart . data . datasets [ 0 ] . backgroundColor [ index ] } ">${ label } </span>
162
+ ` ) . join ( '' ) ;
163
+ }
164
+
101
165
// Function to toggle statistics section
102
166
function toggleStatsSection ( ) {
103
167
const toggleButton = document . getElementById ( 'toggle-stats' ) ;
@@ -203,7 +267,7 @@ document.addEventListener('DOMContentLoaded', function() {
203
267
} ) ;
204
268
205
269
fetchDirectories ( ) ;
206
- toggleLanguagesSection ( ) ;
270
+ createPieChart ( ) ;
207
271
fetchRepoStats ( ) ;
208
272
toggleStatsSection ( ) ;
209
273
} ) ;
0 commit comments