mirror of
https://github.com/tiennm99/github-readme-stats.git
synced 2026-08-12 08:23:06 +00:00
Top languages card donut vertical layout (#2701)
* Top languages card donut layout * dev * dev * dev * dev
This commit is contained in:
@@ -6,9 +6,11 @@ import {
|
||||
radiansToDegrees,
|
||||
polarToCartesian,
|
||||
cartesianToPolar,
|
||||
getCircleLength,
|
||||
calculateCompactLayoutHeight,
|
||||
calculateNormalLayoutHeight,
|
||||
calculateDonutLayoutHeight,
|
||||
calculateDonutVerticalLayoutHeight,
|
||||
calculatePieLayoutHeight,
|
||||
donutCenterTranslation,
|
||||
trimTopLanguages,
|
||||
@@ -70,6 +72,20 @@ const langPercentFromDonutLayoutSvg = (d, centerX, centerY) => {
|
||||
return (endAngle - startAngle) / 3.6;
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate language percentage for donut vertical chart SVG.
|
||||
*
|
||||
* @param {number} partLength Length of current chart part..
|
||||
* @param {number} totalCircleLength Total length of circle.
|
||||
* @return {number} Chart part percentage.
|
||||
*/
|
||||
const langPercentFromDonutVerticalLayoutSvg = (
|
||||
partLength,
|
||||
totalCircleLength,
|
||||
) => {
|
||||
return (partLength / totalCircleLength) * 100;
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the language percentage from the pie chart SVG.
|
||||
*
|
||||
@@ -230,6 +246,20 @@ describe("Test renderTopLanguages helper functions", () => {
|
||||
expect(calculateDonutLayoutHeight(10)).toBe(375);
|
||||
});
|
||||
|
||||
it("calculateDonutVerticalLayoutHeight", () => {
|
||||
expect(calculateDonutVerticalLayoutHeight(0)).toBe(300);
|
||||
expect(calculateDonutVerticalLayoutHeight(1)).toBe(325);
|
||||
expect(calculateDonutVerticalLayoutHeight(2)).toBe(325);
|
||||
expect(calculateDonutVerticalLayoutHeight(3)).toBe(350);
|
||||
expect(calculateDonutVerticalLayoutHeight(4)).toBe(350);
|
||||
expect(calculateDonutVerticalLayoutHeight(5)).toBe(375);
|
||||
expect(calculateDonutVerticalLayoutHeight(6)).toBe(375);
|
||||
expect(calculateDonutVerticalLayoutHeight(7)).toBe(400);
|
||||
expect(calculateDonutVerticalLayoutHeight(8)).toBe(400);
|
||||
expect(calculateDonutVerticalLayoutHeight(9)).toBe(425);
|
||||
expect(calculateDonutVerticalLayoutHeight(10)).toBe(425);
|
||||
});
|
||||
|
||||
it("calculatePieLayoutHeight", () => {
|
||||
expect(calculatePieLayoutHeight(0)).toBe(300);
|
||||
expect(calculatePieLayoutHeight(1)).toBe(325);
|
||||
@@ -258,6 +288,18 @@ describe("Test renderTopLanguages helper functions", () => {
|
||||
expect(donutCenterTranslation(10)).toBe(35);
|
||||
});
|
||||
|
||||
it("getCircleLength", () => {
|
||||
expect(getCircleLength(20)).toBeCloseTo(125.663);
|
||||
expect(getCircleLength(30)).toBeCloseTo(188.495);
|
||||
expect(getCircleLength(40)).toBeCloseTo(251.327);
|
||||
expect(getCircleLength(50)).toBeCloseTo(314.159);
|
||||
expect(getCircleLength(60)).toBeCloseTo(376.991);
|
||||
expect(getCircleLength(70)).toBeCloseTo(439.822);
|
||||
expect(getCircleLength(80)).toBeCloseTo(502.654);
|
||||
expect(getCircleLength(90)).toBeCloseTo(565.486);
|
||||
expect(getCircleLength(100)).toBeCloseTo(628.318);
|
||||
});
|
||||
|
||||
it("trimTopLanguages", () => {
|
||||
expect(trimTopLanguages([])).toStrictEqual({
|
||||
langs: [],
|
||||
@@ -569,6 +611,104 @@ describe("Test renderTopLanguages", () => {
|
||||
"circle",
|
||||
);
|
||||
});
|
||||
|
||||
it("should render with layout donut vertical", () => {
|
||||
document.body.innerHTML = renderTopLanguages(langs, {
|
||||
layout: "donut-vertical",
|
||||
});
|
||||
|
||||
expect(queryByTestId(document.body, "header")).toHaveTextContent(
|
||||
"Most Used Languages",
|
||||
);
|
||||
|
||||
expect(queryAllByTestId(document.body, "lang-name")[0]).toHaveTextContent(
|
||||
"HTML 40.00%",
|
||||
);
|
||||
expect(queryAllByTestId(document.body, "lang-donut")[0]).toHaveAttribute(
|
||||
"size",
|
||||
"40",
|
||||
);
|
||||
|
||||
const totalCircleLength = queryAllByTestId(
|
||||
document.body,
|
||||
"lang-donut",
|
||||
)[0].getAttribute("stroke-dasharray");
|
||||
|
||||
const HTMLLangPercent = langPercentFromDonutVerticalLayoutSvg(
|
||||
queryAllByTestId(document.body, "lang-donut")[1].getAttribute(
|
||||
"stroke-dashoffset",
|
||||
) -
|
||||
queryAllByTestId(document.body, "lang-donut")[0].getAttribute(
|
||||
"stroke-dashoffset",
|
||||
),
|
||||
totalCircleLength,
|
||||
);
|
||||
expect(HTMLLangPercent).toBeCloseTo(40);
|
||||
|
||||
expect(queryAllByTestId(document.body, "lang-name")[1]).toHaveTextContent(
|
||||
"javascript 40.00%",
|
||||
);
|
||||
expect(queryAllByTestId(document.body, "lang-donut")[1]).toHaveAttribute(
|
||||
"size",
|
||||
"40",
|
||||
);
|
||||
const javascriptLangPercent = langPercentFromDonutVerticalLayoutSvg(
|
||||
queryAllByTestId(document.body, "lang-donut")[2].getAttribute(
|
||||
"stroke-dashoffset",
|
||||
) -
|
||||
queryAllByTestId(document.body, "lang-donut")[1].getAttribute(
|
||||
"stroke-dashoffset",
|
||||
),
|
||||
totalCircleLength,
|
||||
);
|
||||
expect(javascriptLangPercent).toBeCloseTo(40);
|
||||
|
||||
expect(queryAllByTestId(document.body, "lang-name")[2]).toHaveTextContent(
|
||||
"css 20.00%",
|
||||
);
|
||||
expect(queryAllByTestId(document.body, "lang-donut")[2]).toHaveAttribute(
|
||||
"size",
|
||||
"20",
|
||||
);
|
||||
const cssLangPercent = langPercentFromDonutVerticalLayoutSvg(
|
||||
totalCircleLength -
|
||||
queryAllByTestId(document.body, "lang-donut")[2].getAttribute(
|
||||
"stroke-dashoffset",
|
||||
),
|
||||
totalCircleLength,
|
||||
);
|
||||
expect(cssLangPercent).toBeCloseTo(20);
|
||||
|
||||
expect(HTMLLangPercent + javascriptLangPercent + cssLangPercent).toBe(100);
|
||||
});
|
||||
|
||||
it("should render with layout donut vertical full donut circle of one language is 100%", () => {
|
||||
document.body.innerHTML = renderTopLanguages(
|
||||
{ HTML: langs.HTML },
|
||||
{ layout: "donut-vertical" },
|
||||
);
|
||||
expect(queryAllByTestId(document.body, "lang-name")[0]).toHaveTextContent(
|
||||
"HTML 100.00%",
|
||||
);
|
||||
expect(queryAllByTestId(document.body, "lang-donut")[0]).toHaveAttribute(
|
||||
"size",
|
||||
"100",
|
||||
);
|
||||
const totalCircleLength = queryAllByTestId(
|
||||
document.body,
|
||||
"lang-donut",
|
||||
)[0].getAttribute("stroke-dasharray");
|
||||
|
||||
const HTMLLangPercent = langPercentFromDonutVerticalLayoutSvg(
|
||||
totalCircleLength -
|
||||
queryAllByTestId(document.body, "lang-donut")[0].getAttribute(
|
||||
"stroke-dashoffset",
|
||||
),
|
||||
totalCircleLength,
|
||||
);
|
||||
expect(HTMLLangPercent).toBeCloseTo(100);
|
||||
});
|
||||
|
||||
it("should render with layout pie", () => {
|
||||
document.body.innerHTML = renderTopLanguages(langs, { layout: "pie" });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user