9
9
calculateCompactLayoutHeight ,
10
10
calculateNormalLayoutHeight ,
11
11
calculateDonutLayoutHeight ,
12
+ calculatePieLayoutHeight ,
12
13
donutCenterTranslation ,
13
14
trimTopLanguages ,
14
15
renderTopLanguages ,
@@ -40,12 +41,13 @@ const langs = {
40
41
41
42
/**
42
43
* Retrieve the language percentage from the donut chart SVG.
44
+ *
43
45
* @param {string } d The SVG path element.
44
46
* @param {number } centerX The center X coordinate of the donut chart.
45
47
* @param {number } centerY The center Y coordinate of the donut chart.
46
48
* @returns {number } The percentage of the language.
47
49
*/
48
- const langPercentFromSvg = ( d , centerX , centerY ) => {
50
+ const langPercentFromDonutLayoutSvg = ( d , centerX , centerY ) => {
49
51
const dTmp = d
50
52
. split ( " " )
51
53
. filter ( ( x ) => ! isNaN ( x ) )
@@ -58,6 +60,34 @@ const langPercentFromSvg = (d, centerX, centerY) => {
58
60
return ( endAngle - startAngle ) / 3.6 ;
59
61
} ;
60
62
63
+ /**
64
+ * Retrieve the language percentage from the pie chart SVG.
65
+ *
66
+ * @param {string } d The SVG path element.
67
+ * @param {number } centerX The center X coordinate of the pie chart.
68
+ * @param {number } centerY The center Y coordinate of the pie chart.
69
+ * @returns {number } The percentage of the language.
70
+ */
71
+ const langPercentFromPieLayoutSvg = ( d , centerX , centerY ) => {
72
+ const dTmp = d
73
+ . split ( " " )
74
+ . filter ( ( x ) => ! isNaN ( x ) )
75
+ . map ( ( x ) => parseFloat ( x ) ) ;
76
+ const startAngle = cartesianToPolar (
77
+ centerX ,
78
+ centerY ,
79
+ dTmp [ 2 ] ,
80
+ dTmp [ 3 ] ,
81
+ ) . angleInDegrees ;
82
+ let endAngle = cartesianToPolar (
83
+ centerX ,
84
+ centerY ,
85
+ dTmp [ 9 ] ,
86
+ dTmp [ 10 ] ,
87
+ ) . angleInDegrees ;
88
+ return ( ( endAngle - startAngle ) / 360 ) * 100 ;
89
+ } ;
90
+
61
91
describe ( "Test renderTopLanguages helper functions" , ( ) => {
62
92
it ( "getLongestLang" , ( ) => {
63
93
const langArray = Object . values ( langs ) ;
@@ -193,6 +223,20 @@ describe("Test renderTopLanguages helper functions", () => {
193
223
expect ( calculateDonutLayoutHeight ( 10 ) ) . toBe ( 375 ) ;
194
224
} ) ;
195
225
226
+ it ( "calculatePieLayoutHeight" , ( ) => {
227
+ expect ( calculatePieLayoutHeight ( 0 ) ) . toBe ( 300 ) ;
228
+ expect ( calculatePieLayoutHeight ( 1 ) ) . toBe ( 325 ) ;
229
+ expect ( calculatePieLayoutHeight ( 2 ) ) . toBe ( 325 ) ;
230
+ expect ( calculatePieLayoutHeight ( 3 ) ) . toBe ( 350 ) ;
231
+ expect ( calculatePieLayoutHeight ( 4 ) ) . toBe ( 350 ) ;
232
+ expect ( calculatePieLayoutHeight ( 5 ) ) . toBe ( 375 ) ;
233
+ expect ( calculatePieLayoutHeight ( 6 ) ) . toBe ( 375 ) ;
234
+ expect ( calculatePieLayoutHeight ( 7 ) ) . toBe ( 400 ) ;
235
+ expect ( calculatePieLayoutHeight ( 8 ) ) . toBe ( 400 ) ;
236
+ expect ( calculatePieLayoutHeight ( 9 ) ) . toBe ( 425 ) ;
237
+ expect ( calculatePieLayoutHeight ( 10 ) ) . toBe ( 425 ) ;
238
+ } ) ;
239
+
196
240
it ( "donutCenterTranslation" , ( ) => {
197
241
expect ( donutCenterTranslation ( 0 ) ) . toBe ( - 45 ) ;
198
242
expect ( donutCenterTranslation ( 1 ) ) . toBe ( - 45 ) ;
@@ -466,7 +510,7 @@ describe("Test renderTopLanguages", () => {
466
510
. filter ( ( x ) => ! isNaN ( x ) )
467
511
. map ( ( x ) => parseFloat ( x ) ) ;
468
512
const center = { x : d [ 7 ] , y : d [ 7 ] } ;
469
- const HTMLLangPercent = langPercentFromSvg (
513
+ const HTMLLangPercent = langPercentFromDonutLayoutSvg (
470
514
queryAllByTestId ( document . body , "lang-donut" ) [ 0 ] . getAttribute ( "d" ) ,
471
515
center . x ,
472
516
center . y ,
@@ -480,7 +524,7 @@ describe("Test renderTopLanguages", () => {
480
524
"size" ,
481
525
"40" ,
482
526
) ;
483
- const javascriptLangPercent = langPercentFromSvg (
527
+ const javascriptLangPercent = langPercentFromDonutLayoutSvg (
484
528
queryAllByTestId ( document . body , "lang-donut" ) [ 1 ] . getAttribute ( "d" ) ,
485
529
center . x ,
486
530
center . y ,
@@ -494,7 +538,7 @@ describe("Test renderTopLanguages", () => {
494
538
"size" ,
495
539
"20" ,
496
540
) ;
497
- const cssLangPercent = langPercentFromSvg (
541
+ const cssLangPercent = langPercentFromDonutLayoutSvg (
498
542
queryAllByTestId ( document . body , "lang-donut" ) [ 2 ] . getAttribute ( "d" ) ,
499
543
center . x ,
500
544
center . y ,
@@ -520,6 +564,81 @@ describe("Test renderTopLanguages", () => {
520
564
"circle" ,
521
565
) ;
522
566
} ) ;
567
+ it ( "should render with layout pie" , ( ) => {
568
+ document . body . innerHTML = renderTopLanguages ( langs , { layout : "pie" } ) ;
569
+
570
+ expect ( queryByTestId ( document . body , "header" ) ) . toHaveTextContent (
571
+ "Most Used Languages" ,
572
+ ) ;
573
+
574
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 0 ] ) . toHaveTextContent (
575
+ "HTML 40.00%" ,
576
+ ) ;
577
+ expect ( queryAllByTestId ( document . body , "lang-pie" ) [ 0 ] ) . toHaveAttribute (
578
+ "size" ,
579
+ "40" ,
580
+ ) ;
581
+
582
+ const d = queryAllByTestId ( document . body , "lang-pie" ) [ 0 ]
583
+ . getAttribute ( "d" )
584
+ . split ( " " )
585
+ . filter ( ( x ) => ! isNaN ( x ) )
586
+ . map ( ( x ) => parseFloat ( x ) ) ;
587
+ const center = { x : d [ 0 ] , y : d [ 1 ] } ;
588
+ const HTMLLangPercent = langPercentFromPieLayoutSvg (
589
+ queryAllByTestId ( document . body , "lang-pie" ) [ 0 ] . getAttribute ( "d" ) ,
590
+ center . x ,
591
+ center . y ,
592
+ ) ;
593
+ expect ( HTMLLangPercent ) . toBeCloseTo ( 40 ) ;
594
+
595
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 1 ] ) . toHaveTextContent (
596
+ "javascript 40.00%" ,
597
+ ) ;
598
+ expect ( queryAllByTestId ( document . body , "lang-pie" ) [ 1 ] ) . toHaveAttribute (
599
+ "size" ,
600
+ "40" ,
601
+ ) ;
602
+ const javascriptLangPercent = langPercentFromPieLayoutSvg (
603
+ queryAllByTestId ( document . body , "lang-pie" ) [ 1 ] . getAttribute ( "d" ) ,
604
+ center . x ,
605
+ center . y ,
606
+ ) ;
607
+ expect ( javascriptLangPercent ) . toBeCloseTo ( 40 ) ;
608
+
609
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 2 ] ) . toHaveTextContent (
610
+ "css 20.00%" ,
611
+ ) ;
612
+ expect ( queryAllByTestId ( document . body , "lang-pie" ) [ 2 ] ) . toHaveAttribute (
613
+ "size" ,
614
+ "20" ,
615
+ ) ;
616
+ const cssLangPercent = langPercentFromPieLayoutSvg (
617
+ queryAllByTestId ( document . body , "lang-pie" ) [ 2 ] . getAttribute ( "d" ) ,
618
+ center . x ,
619
+ center . y ,
620
+ ) ;
621
+ expect ( cssLangPercent ) . toBeCloseTo ( 20 ) ;
622
+
623
+ expect ( HTMLLangPercent + javascriptLangPercent + cssLangPercent ) . toBe ( 100 ) ;
624
+
625
+ // Should render full pie (circle) if one language is 100%.
626
+ document . body . innerHTML = renderTopLanguages (
627
+ { HTML : langs . HTML } ,
628
+ { layout : "pie" } ,
629
+ ) ;
630
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 0 ] ) . toHaveTextContent (
631
+ "HTML 100.00%" ,
632
+ ) ;
633
+ expect ( queryAllByTestId ( document . body , "lang-pie" ) [ 0 ] ) . toHaveAttribute (
634
+ "size" ,
635
+ "100" ,
636
+ ) ;
637
+ expect ( queryAllByTestId ( document . body , "lang-pie" ) ) . toHaveLength ( 1 ) ;
638
+ expect ( queryAllByTestId ( document . body , "lang-pie" ) [ 0 ] . tagName ) . toBe (
639
+ "circle" ,
640
+ ) ;
641
+ } ) ;
523
642
524
643
it ( "should render a translated title" , ( ) => {
525
644
document . body . innerHTML = renderTopLanguages ( langs , { locale : "cn" } ) ;
0 commit comments