@@ -6,9 +6,11 @@ import {
6
6
radiansToDegrees ,
7
7
polarToCartesian ,
8
8
cartesianToPolar ,
9
+ getCircleLength ,
9
10
calculateCompactLayoutHeight ,
10
11
calculateNormalLayoutHeight ,
11
12
calculateDonutLayoutHeight ,
13
+ calculateDonutVerticalLayoutHeight ,
12
14
calculatePieLayoutHeight ,
13
15
donutCenterTranslation ,
14
16
trimTopLanguages ,
@@ -70,6 +72,20 @@ const langPercentFromDonutLayoutSvg = (d, centerX, centerY) => {
70
72
return ( endAngle - startAngle ) / 3.6 ;
71
73
} ;
72
74
75
+ /**
76
+ * Calculate language percentage for donut vertical chart SVG.
77
+ *
78
+ * @param {number } partLength Length of current chart part..
79
+ * @param {number } totalCircleLength Total length of circle.
80
+ * @return {number } Chart part percentage.
81
+ */
82
+ const langPercentFromDonutVerticalLayoutSvg = (
83
+ partLength ,
84
+ totalCircleLength ,
85
+ ) => {
86
+ return ( partLength / totalCircleLength ) * 100 ;
87
+ } ;
88
+
73
89
/**
74
90
* Retrieve the language percentage from the pie chart SVG.
75
91
*
@@ -230,6 +246,20 @@ describe("Test renderTopLanguages helper functions", () => {
230
246
expect ( calculateDonutLayoutHeight ( 10 ) ) . toBe ( 375 ) ;
231
247
} ) ;
232
248
249
+ it ( "calculateDonutVerticalLayoutHeight" , ( ) => {
250
+ expect ( calculateDonutVerticalLayoutHeight ( 0 ) ) . toBe ( 300 ) ;
251
+ expect ( calculateDonutVerticalLayoutHeight ( 1 ) ) . toBe ( 325 ) ;
252
+ expect ( calculateDonutVerticalLayoutHeight ( 2 ) ) . toBe ( 325 ) ;
253
+ expect ( calculateDonutVerticalLayoutHeight ( 3 ) ) . toBe ( 350 ) ;
254
+ expect ( calculateDonutVerticalLayoutHeight ( 4 ) ) . toBe ( 350 ) ;
255
+ expect ( calculateDonutVerticalLayoutHeight ( 5 ) ) . toBe ( 375 ) ;
256
+ expect ( calculateDonutVerticalLayoutHeight ( 6 ) ) . toBe ( 375 ) ;
257
+ expect ( calculateDonutVerticalLayoutHeight ( 7 ) ) . toBe ( 400 ) ;
258
+ expect ( calculateDonutVerticalLayoutHeight ( 8 ) ) . toBe ( 400 ) ;
259
+ expect ( calculateDonutVerticalLayoutHeight ( 9 ) ) . toBe ( 425 ) ;
260
+ expect ( calculateDonutVerticalLayoutHeight ( 10 ) ) . toBe ( 425 ) ;
261
+ } ) ;
262
+
233
263
it ( "calculatePieLayoutHeight" , ( ) => {
234
264
expect ( calculatePieLayoutHeight ( 0 ) ) . toBe ( 300 ) ;
235
265
expect ( calculatePieLayoutHeight ( 1 ) ) . toBe ( 325 ) ;
@@ -258,6 +288,18 @@ describe("Test renderTopLanguages helper functions", () => {
258
288
expect ( donutCenterTranslation ( 10 ) ) . toBe ( 35 ) ;
259
289
} ) ;
260
290
291
+ it ( "getCircleLength" , ( ) => {
292
+ expect ( getCircleLength ( 20 ) ) . toBeCloseTo ( 125.663 ) ;
293
+ expect ( getCircleLength ( 30 ) ) . toBeCloseTo ( 188.495 ) ;
294
+ expect ( getCircleLength ( 40 ) ) . toBeCloseTo ( 251.327 ) ;
295
+ expect ( getCircleLength ( 50 ) ) . toBeCloseTo ( 314.159 ) ;
296
+ expect ( getCircleLength ( 60 ) ) . toBeCloseTo ( 376.991 ) ;
297
+ expect ( getCircleLength ( 70 ) ) . toBeCloseTo ( 439.822 ) ;
298
+ expect ( getCircleLength ( 80 ) ) . toBeCloseTo ( 502.654 ) ;
299
+ expect ( getCircleLength ( 90 ) ) . toBeCloseTo ( 565.486 ) ;
300
+ expect ( getCircleLength ( 100 ) ) . toBeCloseTo ( 628.318 ) ;
301
+ } ) ;
302
+
261
303
it ( "trimTopLanguages" , ( ) => {
262
304
expect ( trimTopLanguages ( [ ] ) ) . toStrictEqual ( {
263
305
langs : [ ] ,
@@ -569,6 +611,104 @@ describe("Test renderTopLanguages", () => {
569
611
"circle" ,
570
612
) ;
571
613
} ) ;
614
+
615
+ it ( "should render with layout donut vertical" , ( ) => {
616
+ document . body . innerHTML = renderTopLanguages ( langs , {
617
+ layout : "donut-vertical" ,
618
+ } ) ;
619
+
620
+ expect ( queryByTestId ( document . body , "header" ) ) . toHaveTextContent (
621
+ "Most Used Languages" ,
622
+ ) ;
623
+
624
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 0 ] ) . toHaveTextContent (
625
+ "HTML 40.00%" ,
626
+ ) ;
627
+ expect ( queryAllByTestId ( document . body , "lang-donut" ) [ 0 ] ) . toHaveAttribute (
628
+ "size" ,
629
+ "40" ,
630
+ ) ;
631
+
632
+ const totalCircleLength = queryAllByTestId (
633
+ document . body ,
634
+ "lang-donut" ,
635
+ ) [ 0 ] . getAttribute ( "stroke-dasharray" ) ;
636
+
637
+ const HTMLLangPercent = langPercentFromDonutVerticalLayoutSvg (
638
+ queryAllByTestId ( document . body , "lang-donut" ) [ 1 ] . getAttribute (
639
+ "stroke-dashoffset" ,
640
+ ) -
641
+ queryAllByTestId ( document . body , "lang-donut" ) [ 0 ] . getAttribute (
642
+ "stroke-dashoffset" ,
643
+ ) ,
644
+ totalCircleLength ,
645
+ ) ;
646
+ expect ( HTMLLangPercent ) . toBeCloseTo ( 40 ) ;
647
+
648
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 1 ] ) . toHaveTextContent (
649
+ "javascript 40.00%" ,
650
+ ) ;
651
+ expect ( queryAllByTestId ( document . body , "lang-donut" ) [ 1 ] ) . toHaveAttribute (
652
+ "size" ,
653
+ "40" ,
654
+ ) ;
655
+ const javascriptLangPercent = langPercentFromDonutVerticalLayoutSvg (
656
+ queryAllByTestId ( document . body , "lang-donut" ) [ 2 ] . getAttribute (
657
+ "stroke-dashoffset" ,
658
+ ) -
659
+ queryAllByTestId ( document . body , "lang-donut" ) [ 1 ] . getAttribute (
660
+ "stroke-dashoffset" ,
661
+ ) ,
662
+ totalCircleLength ,
663
+ ) ;
664
+ expect ( javascriptLangPercent ) . toBeCloseTo ( 40 ) ;
665
+
666
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 2 ] ) . toHaveTextContent (
667
+ "css 20.00%" ,
668
+ ) ;
669
+ expect ( queryAllByTestId ( document . body , "lang-donut" ) [ 2 ] ) . toHaveAttribute (
670
+ "size" ,
671
+ "20" ,
672
+ ) ;
673
+ const cssLangPercent = langPercentFromDonutVerticalLayoutSvg (
674
+ totalCircleLength -
675
+ queryAllByTestId ( document . body , "lang-donut" ) [ 2 ] . getAttribute (
676
+ "stroke-dashoffset" ,
677
+ ) ,
678
+ totalCircleLength ,
679
+ ) ;
680
+ expect ( cssLangPercent ) . toBeCloseTo ( 20 ) ;
681
+
682
+ expect ( HTMLLangPercent + javascriptLangPercent + cssLangPercent ) . toBe ( 100 ) ;
683
+ } ) ;
684
+
685
+ it ( "should render with layout donut vertical full donut circle of one language is 100%" , ( ) => {
686
+ document . body . innerHTML = renderTopLanguages (
687
+ { HTML : langs . HTML } ,
688
+ { layout : "donut-vertical" } ,
689
+ ) ;
690
+ expect ( queryAllByTestId ( document . body , "lang-name" ) [ 0 ] ) . toHaveTextContent (
691
+ "HTML 100.00%" ,
692
+ ) ;
693
+ expect ( queryAllByTestId ( document . body , "lang-donut" ) [ 0 ] ) . toHaveAttribute (
694
+ "size" ,
695
+ "100" ,
696
+ ) ;
697
+ const totalCircleLength = queryAllByTestId (
698
+ document . body ,
699
+ "lang-donut" ,
700
+ ) [ 0 ] . getAttribute ( "stroke-dasharray" ) ;
701
+
702
+ const HTMLLangPercent = langPercentFromDonutVerticalLayoutSvg (
703
+ totalCircleLength -
704
+ queryAllByTestId ( document . body , "lang-donut" ) [ 0 ] . getAttribute (
705
+ "stroke-dashoffset" ,
706
+ ) ,
707
+ totalCircleLength ,
708
+ ) ;
709
+ expect ( HTMLLangPercent ) . toBeCloseTo ( 100 ) ;
710
+ } ) ;
711
+
572
712
it ( "should render with layout pie" , ( ) => {
573
713
document . body . innerHTML = renderTopLanguages ( langs , { layout : "pie" } ) ;
574
714
0 commit comments