9
9
getCardColors ,
10
10
lowercaseTrim ,
11
11
measureText ,
12
+ formatBytes ,
12
13
} from "../common/utils.js" ;
13
14
import { langCardLocales } from "../translations.js" ;
14
15
@@ -196,27 +197,52 @@ const trimTopLanguages = (topLangs, langs_count, hide) => {
196
197
return { langs, totalLanguageSize } ;
197
198
} ;
198
199
200
+ /**
201
+ * Get display value corresponding to the format.
202
+ *
203
+ * @param {number } size Bytes size.
204
+ * @param {number } percentages Percentage value.
205
+ * @param {string } format Format of the stats.
206
+ * @returns {string } Display value.
207
+ */
208
+ const getDisplayValue = ( size , percentages , format ) => {
209
+ return format === "bytes" ? formatBytes ( size ) : `${ percentages . toFixed ( 2 ) } %` ;
210
+ } ;
211
+
199
212
/**
200
213
* Create progress bar text item for a programming language.
201
214
*
202
215
* @param {object } props Function properties.
203
216
* @param {number } props.width The card width
204
217
* @param {string } props.color Color of the programming language.
205
218
* @param {string } props.name Name of the programming language.
206
- * @param {number } props.progress Usage of the programming language in percentage.
219
+ * @param {number } props.size Size of the programming language.
220
+ * @param {number } props.totalSize Total size of all languages.
221
+ * @param {string } props.statsFormat Stats format.
207
222
* @param {number } props.index Index of the programming language.
208
223
* @returns {string } Programming language SVG node.
209
224
*/
210
- const createProgressTextNode = ( { width, color, name, progress, index } ) => {
225
+ const createProgressTextNode = ( {
226
+ width,
227
+ color,
228
+ name,
229
+ size,
230
+ totalSize,
231
+ statsFormat,
232
+ index,
233
+ } ) => {
211
234
const staggerDelay = ( index + 3 ) * 150 ;
212
235
const paddingRight = 95 ;
213
236
const progressTextX = width - paddingRight + 10 ;
214
237
const progressWidth = width - paddingRight ;
215
238
239
+ const progress = ( size / totalSize ) * 100 ;
240
+ const displayValue = getDisplayValue ( size , progress , statsFormat ) ;
241
+
216
242
return `
217
243
<g class="stagger" style="animation-delay: ${ staggerDelay } ms">
218
244
<text data-testid="lang-name" x="2" y="15" class="lang-name">${ name } </text>
219
- <text x="${ progressTextX } " y="34" class="lang-name">${ progress } % </text>
245
+ <text x="${ progressTextX } " y="34" class="lang-name">${ displayValue } </text>
220
246
${ createProgressNode ( {
221
247
x : 0 ,
222
248
y : 25 ,
@@ -237,19 +263,28 @@ const createProgressTextNode = ({ width, color, name, progress, index }) => {
237
263
* @param {Lang } props.lang Programming language object.
238
264
* @param {number } props.totalSize Total size of all languages.
239
265
* @param {boolean= } props.hideProgress Whether to hide percentage.
266
+ * @param {string= } props.statsFormat Stats format
240
267
* @param {number } props.index Index of the programming language.
241
268
* @returns {string } Compact layout programming language SVG node.
242
269
*/
243
- const createCompactLangNode = ( { lang, totalSize, hideProgress, index } ) => {
244
- const percentage = ( ( lang . size / totalSize ) * 100 ) . toFixed ( 2 ) ;
270
+ const createCompactLangNode = ( {
271
+ lang,
272
+ totalSize,
273
+ hideProgress,
274
+ statsFormat = "percentages" ,
275
+ index,
276
+ } ) => {
277
+ const percentages = ( lang . size / totalSize ) * 100 ;
278
+ const displayValue = getDisplayValue ( lang . size , percentages , statsFormat ) ;
279
+
245
280
const staggerDelay = ( index + 3 ) * 150 ;
246
281
const color = lang . color || "#858585" ;
247
282
248
283
return `
249
284
<g class="stagger" style="animation-delay: ${ staggerDelay } ms">
250
285
<circle cx="5" cy="6" r="5" fill="${ color } " />
251
286
<text data-testid="lang-name" x="15" y="10" class='lang-name'>
252
- ${ lang . name } ${ hideProgress ? "" : percentage + "%" }
287
+ ${ lang . name } ${ hideProgress ? "" : displayValue }
253
288
</text>
254
289
</g>
255
290
` ;
@@ -262,9 +297,15 @@ const createCompactLangNode = ({ lang, totalSize, hideProgress, index }) => {
262
297
* @param {Lang[] } props.langs Array of programming languages.
263
298
* @param {number } props.totalSize Total size of all languages.
264
299
* @param {boolean= } props.hideProgress Whether to hide percentage.
300
+ * @param {string= } props.statsFormat Stats format
265
301
* @returns {string } Programming languages SVG node.
266
302
*/
267
- const createLanguageTextNode = ( { langs, totalSize, hideProgress } ) => {
303
+ const createLanguageTextNode = ( {
304
+ langs,
305
+ totalSize,
306
+ hideProgress,
307
+ statsFormat,
308
+ } ) => {
268
309
const longestLang = getLongestLang ( langs ) ;
269
310
const chunked = chunkArray ( langs , langs . length / 2 ) ;
270
311
const layouts = chunked . map ( ( array ) => {
@@ -274,6 +315,7 @@ const createLanguageTextNode = ({ langs, totalSize, hideProgress }) => {
274
315
lang,
275
316
totalSize,
276
317
hideProgress,
318
+ statsFormat,
277
319
index,
278
320
} ) ,
279
321
) ;
@@ -299,15 +341,17 @@ const createLanguageTextNode = ({ langs, totalSize, hideProgress }) => {
299
341
* @param {object } props Function properties.
300
342
* @param {Lang[] } props.langs Array of programming languages.
301
343
* @param {number } props.totalSize Total size of all languages.
344
+ * @param {string } props.statsFormat Stats format
302
345
* @returns {string } Donut layout programming language SVG node.
303
346
*/
304
- const createDonutLanguagesNode = ( { langs, totalSize } ) => {
347
+ const createDonutLanguagesNode = ( { langs, totalSize, statsFormat } ) => {
305
348
return flexLayout ( {
306
349
items : langs . map ( ( lang , index ) => {
307
350
return createCompactLangNode ( {
308
351
lang,
309
352
totalSize,
310
353
hideProgress : false ,
354
+ statsFormat,
311
355
index,
312
356
} ) ;
313
357
} ) ,
@@ -322,18 +366,19 @@ const createDonutLanguagesNode = ({ langs, totalSize }) => {
322
366
* @param {Lang[] } langs Array of programming languages.
323
367
* @param {number } width Card width.
324
368
* @param {number } totalLanguageSize Total size of all languages.
369
+ * @param {string } statsFormat Stats format.
325
370
* @returns {string } Normal layout card SVG object.
326
371
*/
327
- const renderNormalLayout = ( langs , width , totalLanguageSize ) => {
372
+ const renderNormalLayout = ( langs , width , totalLanguageSize , statsFormat ) => {
328
373
return flexLayout ( {
329
374
items : langs . map ( ( lang , index ) => {
330
375
return createProgressTextNode ( {
331
376
width,
332
377
name : lang . name ,
333
378
color : lang . color || DEFAULT_LANG_COLOR ,
334
- progress : parseFloat (
335
- ( ( lang . size / totalLanguageSize ) * 100 ) . toFixed ( 2 ) ,
336
- ) ,
379
+ size : lang . size ,
380
+ totalSize : totalLanguageSize ,
381
+ statsFormat ,
337
382
index,
338
383
} ) ;
339
384
} ) ,
@@ -349,9 +394,16 @@ const renderNormalLayout = (langs, width, totalLanguageSize) => {
349
394
* @param {number } width Card width.
350
395
* @param {number } totalLanguageSize Total size of all languages.
351
396
* @param {boolean= } hideProgress Whether to hide progress bar.
397
+ * @param {string } statsFormat Stats format.
352
398
* @returns {string } Compact layout card SVG object.
353
399
*/
354
- const renderCompactLayout = ( langs , width , totalLanguageSize , hideProgress ) => {
400
+ const renderCompactLayout = (
401
+ langs ,
402
+ width ,
403
+ totalLanguageSize ,
404
+ hideProgress ,
405
+ statsFormat = "percentages" ,
406
+ ) => {
355
407
const paddingRight = 50 ;
356
408
const offsetWidth = width - paddingRight ;
357
409
// progressOffset holds the previous language's width and used to offset the next language
@@ -397,6 +449,7 @@ const renderCompactLayout = (langs, width, totalLanguageSize, hideProgress) => {
397
449
langs,
398
450
totalSize : totalLanguageSize ,
399
451
hideProgress,
452
+ statsFormat,
400
453
} ) }
401
454
</g>
402
455
` ;
@@ -407,9 +460,10 @@ const renderCompactLayout = (langs, width, totalLanguageSize, hideProgress) => {
407
460
*
408
461
* @param {Lang[] } langs Array of programming languages.
409
462
* @param {number } totalLanguageSize Total size of all languages.
463
+ * @param {string } statsFormat Stats format.
410
464
* @returns {string } Compact layout card SVG object.
411
465
*/
412
- const renderDonutVerticalLayout = ( langs , totalLanguageSize ) => {
466
+ const renderDonutVerticalLayout = ( langs , totalLanguageSize , statsFormat ) => {
413
467
// Donut vertical chart radius and total length
414
468
const radius = 80 ;
415
469
const totalCircleLength = getCircleLength ( radius ) ;
@@ -465,6 +519,7 @@ const renderDonutVerticalLayout = (langs, totalLanguageSize) => {
465
519
langs,
466
520
totalSize : totalLanguageSize ,
467
521
hideProgress : false ,
522
+ statsFormat,
468
523
} ) }
469
524
</svg>
470
525
</g>
@@ -477,9 +532,10 @@ const renderDonutVerticalLayout = (langs, totalLanguageSize) => {
477
532
*
478
533
* @param {Lang[] } langs Array of programming languages.
479
534
* @param {number } totalLanguageSize Total size of all languages.
535
+ * @param {string } statsFormat Stats format.
480
536
* @returns {string } Compact layout card SVG object.
481
537
*/
482
- const renderPieLayout = ( langs , totalLanguageSize ) => {
538
+ const renderPieLayout = ( langs , totalLanguageSize , statsFormat ) => {
483
539
// Pie chart radius and center coordinates
484
540
const radius = 90 ;
485
541
const centerX = 150 ;
@@ -560,6 +616,7 @@ const renderPieLayout = (langs, totalLanguageSize) => {
560
616
langs,
561
617
totalSize : totalLanguageSize ,
562
618
hideProgress : false ,
619
+ statsFormat,
563
620
} ) }
564
621
</svg>
565
622
</g>
@@ -610,9 +667,10 @@ const createDonutPaths = (cx, cy, radius, percentages) => {
610
667
* @param {Lang[] } langs Array of programming languages.
611
668
* @param {number } width Card width.
612
669
* @param {number } totalLanguageSize Total size of all languages.
670
+ * @param {string } statsFormat Stats format.
613
671
* @returns {string } Donut layout card SVG object.
614
672
*/
615
- const renderDonutLayout = ( langs , width , totalLanguageSize ) => {
673
+ const renderDonutLayout = ( langs , width , totalLanguageSize , statsFormat ) => {
616
674
const centerX = width / 3 ;
617
675
const centerY = width / 3 ;
618
676
const radius = centerX - 60 ;
@@ -655,7 +713,7 @@ const renderDonutLayout = (langs, width, totalLanguageSize) => {
655
713
return `
656
714
<g transform="translate(0, 0)">
657
715
<g transform="translate(0, 0)">
658
- ${ createDonutLanguagesNode ( { langs, totalSize : totalLanguageSize } ) }
716
+ ${ createDonutLanguagesNode ( { langs, totalSize : totalLanguageSize , statsFormat } ) }
659
717
</g>
660
718
661
719
<g transform="translate(125, ${ donutCenterTranslation ( langs . length ) } )">
@@ -738,6 +796,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
738
796
border_radius,
739
797
border_color,
740
798
disable_animations,
799
+ stats_format = "percentages" ,
741
800
} = options ;
742
801
743
802
const i18n = new I18n ( {
@@ -779,10 +838,14 @@ const renderTopLanguages = (topLangs, options = {}) => {
779
838
} ) ;
780
839
} else if ( layout === "pie" ) {
781
840
height = calculatePieLayoutHeight ( langs . length ) ;
782
- finalLayout = renderPieLayout ( langs , totalLanguageSize ) ;
841
+ finalLayout = renderPieLayout ( langs , totalLanguageSize , stats_format ) ;
783
842
} else if ( layout === "donut-vertical" ) {
784
843
height = calculateDonutVerticalLayoutHeight ( langs . length ) ;
785
- finalLayout = renderDonutVerticalLayout ( langs , totalLanguageSize ) ;
844
+ finalLayout = renderDonutVerticalLayout (
845
+ langs ,
846
+ totalLanguageSize ,
847
+ stats_format ,
848
+ ) ;
786
849
} else if ( layout === "compact" || hide_progress == true ) {
787
850
height =
788
851
calculateCompactLayoutHeight ( langs . length ) + ( hide_progress ? - 25 : 0 ) ;
@@ -792,13 +855,24 @@ const renderTopLanguages = (topLangs, options = {}) => {
792
855
width ,
793
856
totalLanguageSize ,
794
857
hide_progress ,
858
+ stats_format ,
795
859
) ;
796
860
} else if ( layout === "donut" ) {
797
861
height = calculateDonutLayoutHeight ( langs . length ) ;
798
862
width = width + 50 ; // padding
799
- finalLayout = renderDonutLayout ( langs , width , totalLanguageSize ) ;
863
+ finalLayout = renderDonutLayout (
864
+ langs ,
865
+ width ,
866
+ totalLanguageSize ,
867
+ stats_format ,
868
+ ) ;
800
869
} else {
801
- finalLayout = renderNormalLayout ( langs , width , totalLanguageSize ) ;
870
+ finalLayout = renderNormalLayout (
871
+ langs ,
872
+ width ,
873
+ totalLanguageSize ,
874
+ stats_format ,
875
+ ) ;
802
876
}
803
877
804
878
const card = new Card ( {
0 commit comments