-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add a display bytes option to top-languages card #3708
base: master
Are you sure you want to change the base?
Changes from 4 commits
3b101f3
9d37526
ef70589
ffc0230
0c90270
27da644
7e17b50
1c85ace
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import { | |
getCardColors, | ||
lowercaseTrim, | ||
measureText, | ||
formatBytes, | ||
} from "../common/utils.js"; | ||
import { langCardLocales } from "../translations.js"; | ||
|
||
|
@@ -203,20 +204,34 @@ const trimTopLanguages = (topLangs, langs_count, hide) => { | |
* @param {number} props.width The card width | ||
* @param {string} props.color Color of the programming language. | ||
* @param {string} props.name Name of the programming language. | ||
* @param {number} props.progress Usage of the programming language in percentage. | ||
* @param {number} props.size Size of the programming language. | ||
* @param {number} props.totalSize Total size of all languages. | ||
* @param {string} props.statsFormat Stats format. | ||
* @param {number} props.index Index of the programming language. | ||
* @returns {string} Programming language SVG node. | ||
*/ | ||
const createProgressTextNode = ({ width, color, name, progress, index }) => { | ||
const createProgressTextNode = ({ | ||
width, | ||
color, | ||
name, | ||
size, | ||
totalSize, | ||
statsFormat, | ||
index, | ||
}) => { | ||
const staggerDelay = (index + 3) * 150; | ||
const paddingRight = 95; | ||
const progressTextX = width - paddingRight + 10; | ||
const progressWidth = width - paddingRight; | ||
|
||
const progress = parseFloat(((size / totalSize) * 100).toFixed(2)); | ||
const bytes = formatBytes(size); | ||
const showValue = statsFormat === "bytes" ? bytes : `${progress}%`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that this file contains the almost same code on 266-269 lines. I think it will be better to avoid duplication of this logic and create separate function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer it that way too, modified at 0c90270. |
||
|
||
return ` | ||
<g class="stagger" style="animation-delay: ${staggerDelay}ms"> | ||
<text data-testid="lang-name" x="2" y="15" class="lang-name">${name}</text> | ||
<text x="${progressTextX}" y="34" class="lang-name">${progress}%</text> | ||
<text x="${progressTextX}" y="34" class="lang-name">${showValue}</text> | ||
${createProgressNode({ | ||
x: 0, | ||
y: 25, | ||
|
@@ -237,19 +252,30 @@ const createProgressTextNode = ({ width, color, name, progress, index }) => { | |
* @param {Lang} props.lang Programming language object. | ||
* @param {number} props.totalSize Total size of all languages. | ||
* @param {boolean=} props.hideProgress Whether to hide percentage. | ||
* @param {string=} props.statsFormat Stats format | ||
* @param {number} props.index Index of the programming language. | ||
* @returns {string} Compact layout programming language SVG node. | ||
*/ | ||
const createCompactLangNode = ({ lang, totalSize, hideProgress, index }) => { | ||
const percentage = ((lang.size / totalSize) * 100).toFixed(2); | ||
const createCompactLangNode = ({ | ||
lang, | ||
totalSize, | ||
hideProgress, | ||
statsFormat, | ||
index, | ||
}) => { | ||
const percentage = ((lang.size / totalSize) * 100).toFixed(2) + "%"; | ||
const bytes = formatBytes(lang.size); | ||
|
||
const showValue = statsFormat === "bytes" ? bytes : percentage; | ||
|
||
const staggerDelay = (index + 3) * 150; | ||
const color = lang.color || "#858585"; | ||
|
||
return ` | ||
<g class="stagger" style="animation-delay: ${staggerDelay}ms"> | ||
<circle cx="5" cy="6" r="5" fill="${color}" /> | ||
<text data-testid="lang-name" x="15" y="10" class='lang-name'> | ||
${lang.name} ${hideProgress ? "" : percentage + "%"} | ||
${lang.name} ${hideProgress ? "" : showValue} | ||
</text> | ||
</g> | ||
`; | ||
|
@@ -262,9 +288,15 @@ const createCompactLangNode = ({ lang, totalSize, hideProgress, index }) => { | |
* @param {Lang[]} props.langs Array of programming languages. | ||
* @param {number} props.totalSize Total size of all languages. | ||
* @param {boolean=} props.hideProgress Whether to hide percentage. | ||
* @param {string=} props.statsFormat Stats format | ||
* @returns {string} Programming languages SVG node. | ||
*/ | ||
const createLanguageTextNode = ({ langs, totalSize, hideProgress }) => { | ||
const createLanguageTextNode = ({ | ||
langs, | ||
totalSize, | ||
hideProgress, | ||
statsFormat, | ||
}) => { | ||
const longestLang = getLongestLang(langs); | ||
const chunked = chunkArray(langs, langs.length / 2); | ||
const layouts = chunked.map((array) => { | ||
|
@@ -274,6 +306,7 @@ const createLanguageTextNode = ({ langs, totalSize, hideProgress }) => { | |
lang, | ||
totalSize, | ||
hideProgress, | ||
statsFormat, | ||
index, | ||
}), | ||
); | ||
|
@@ -299,15 +332,17 @@ const createLanguageTextNode = ({ langs, totalSize, hideProgress }) => { | |
* @param {object} props Function properties. | ||
* @param {Lang[]} props.langs Array of programming languages. | ||
* @param {number} props.totalSize Total size of all languages. | ||
* @param {string} props.statsFormat Stats format | ||
* @returns {string} Donut layout programming language SVG node. | ||
*/ | ||
const createDonutLanguagesNode = ({ langs, totalSize }) => { | ||
const createDonutLanguagesNode = ({ langs, totalSize, statsFormat }) => { | ||
return flexLayout({ | ||
items: langs.map((lang, index) => { | ||
return createCompactLangNode({ | ||
lang, | ||
totalSize, | ||
hideProgress: false, | ||
statsFormat, | ||
index, | ||
}); | ||
}), | ||
|
@@ -322,18 +357,19 @@ const createDonutLanguagesNode = ({ langs, totalSize }) => { | |
* @param {Lang[]} langs Array of programming languages. | ||
* @param {number} width Card width. | ||
* @param {number} totalLanguageSize Total size of all languages. | ||
* @param {string} statsFormat Stats format. | ||
* @returns {string} Normal layout card SVG object. | ||
*/ | ||
const renderNormalLayout = (langs, width, totalLanguageSize) => { | ||
const renderNormalLayout = (langs, width, totalLanguageSize, statsFormat) => { | ||
return flexLayout({ | ||
items: langs.map((lang, index) => { | ||
return createProgressTextNode({ | ||
width, | ||
name: lang.name, | ||
color: lang.color || DEFAULT_LANG_COLOR, | ||
progress: parseFloat( | ||
((lang.size / totalLanguageSize) * 100).toFixed(2), | ||
), | ||
size: lang.size, | ||
totalSize: totalLanguageSize, | ||
statsFormat, | ||
index, | ||
}); | ||
}), | ||
|
@@ -349,9 +385,16 @@ const renderNormalLayout = (langs, width, totalLanguageSize) => { | |
* @param {number} width Card width. | ||
* @param {number} totalLanguageSize Total size of all languages. | ||
* @param {boolean=} hideProgress Whether to hide progress bar. | ||
* @param {string} statsFormat Stats format. | ||
* @returns {string} Compact layout card SVG object. | ||
*/ | ||
const renderCompactLayout = (langs, width, totalLanguageSize, hideProgress) => { | ||
const renderCompactLayout = ( | ||
langs, | ||
width, | ||
totalLanguageSize, | ||
hideProgress, | ||
statsFormat, | ||
) => { | ||
const paddingRight = 50; | ||
const offsetWidth = width - paddingRight; | ||
// progressOffset holds the previous language's width and used to offset the next language | ||
|
@@ -397,6 +440,7 @@ const renderCompactLayout = (langs, width, totalLanguageSize, hideProgress) => { | |
langs, | ||
totalSize: totalLanguageSize, | ||
hideProgress, | ||
statsFormat, | ||
})} | ||
</g> | ||
`; | ||
|
@@ -407,9 +451,10 @@ const renderCompactLayout = (langs, width, totalLanguageSize, hideProgress) => { | |
* | ||
* @param {Lang[]} langs Array of programming languages. | ||
* @param {number} totalLanguageSize Total size of all languages. | ||
* @param {string} statsFormat Stats format. | ||
* @returns {string} Compact layout card SVG object. | ||
*/ | ||
const renderDonutVerticalLayout = (langs, totalLanguageSize) => { | ||
const renderDonutVerticalLayout = (langs, totalLanguageSize, statsFormat) => { | ||
// Donut vertical chart radius and total length | ||
const radius = 80; | ||
const totalCircleLength = getCircleLength(radius); | ||
|
@@ -465,6 +510,7 @@ const renderDonutVerticalLayout = (langs, totalLanguageSize) => { | |
langs, | ||
totalSize: totalLanguageSize, | ||
hideProgress: false, | ||
statsFormat, | ||
})} | ||
</svg> | ||
</g> | ||
|
@@ -477,9 +523,10 @@ const renderDonutVerticalLayout = (langs, totalLanguageSize) => { | |
* | ||
* @param {Lang[]} langs Array of programming languages. | ||
* @param {number} totalLanguageSize Total size of all languages. | ||
* @param {string} statsFormat Stats format. | ||
* @returns {string} Compact layout card SVG object. | ||
*/ | ||
const renderPieLayout = (langs, totalLanguageSize) => { | ||
const renderPieLayout = (langs, totalLanguageSize, statsFormat) => { | ||
// Pie chart radius and center coordinates | ||
const radius = 90; | ||
const centerX = 150; | ||
|
@@ -560,6 +607,7 @@ const renderPieLayout = (langs, totalLanguageSize) => { | |
langs, | ||
totalSize: totalLanguageSize, | ||
hideProgress: false, | ||
statsFormat, | ||
})} | ||
</svg> | ||
</g> | ||
|
@@ -610,9 +658,10 @@ const createDonutPaths = (cx, cy, radius, percentages) => { | |
* @param {Lang[]} langs Array of programming languages. | ||
* @param {number} width Card width. | ||
* @param {number} totalLanguageSize Total size of all languages. | ||
* @param {string} statsFormat Stats format. | ||
* @returns {string} Donut layout card SVG object. | ||
*/ | ||
const renderDonutLayout = (langs, width, totalLanguageSize) => { | ||
const renderDonutLayout = (langs, width, totalLanguageSize, statsFormat) => { | ||
const centerX = width / 3; | ||
const centerY = width / 3; | ||
const radius = centerX - 60; | ||
|
@@ -655,7 +704,7 @@ const renderDonutLayout = (langs, width, totalLanguageSize) => { | |
return ` | ||
<g transform="translate(0, 0)"> | ||
<g transform="translate(0, 0)"> | ||
${createDonutLanguagesNode({ langs, totalSize: totalLanguageSize })} | ||
${createDonutLanguagesNode({ langs, totalSize: totalLanguageSize, statsFormat })} | ||
</g> | ||
|
||
<g transform="translate(125, ${donutCenterTranslation(langs.length)})"> | ||
|
@@ -738,6 +787,7 @@ const renderTopLanguages = (topLangs, options = {}) => { | |
border_radius, | ||
border_color, | ||
disable_animations, | ||
stats_format = "percentages", | ||
} = options; | ||
|
||
const i18n = new I18n({ | ||
|
@@ -779,10 +829,14 @@ const renderTopLanguages = (topLangs, options = {}) => { | |
}); | ||
} else if (layout === "pie") { | ||
height = calculatePieLayoutHeight(langs.length); | ||
finalLayout = renderPieLayout(langs, totalLanguageSize); | ||
finalLayout = renderPieLayout(langs, totalLanguageSize, stats_format); | ||
} else if (layout === "donut-vertical") { | ||
height = calculateDonutVerticalLayoutHeight(langs.length); | ||
finalLayout = renderDonutVerticalLayout(langs, totalLanguageSize); | ||
finalLayout = renderDonutVerticalLayout( | ||
langs, | ||
totalLanguageSize, | ||
stats_format, | ||
); | ||
} else if (layout === "compact" || hide_progress == true) { | ||
height = | ||
calculateCompactLayoutHeight(langs.length) + (hide_progress ? -25 : 0); | ||
|
@@ -792,13 +846,24 @@ const renderTopLanguages = (topLangs, options = {}) => { | |
width, | ||
totalLanguageSize, | ||
hide_progress, | ||
stats_format, | ||
); | ||
} else if (layout === "donut") { | ||
height = calculateDonutLayoutHeight(langs.length); | ||
width = width + 50; // padding | ||
finalLayout = renderDonutLayout(langs, width, totalLanguageSize); | ||
finalLayout = renderDonutLayout( | ||
langs, | ||
width, | ||
totalLanguageSize, | ||
stats_format, | ||
); | ||
} else { | ||
finalLayout = renderNormalLayout(langs, width, totalLanguageSize); | ||
finalLayout = renderNormalLayout( | ||
langs, | ||
width, | ||
totalLanguageSize, | ||
stats_format, | ||
); | ||
} | ||
|
||
const card = new Card({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to add new query string parameter information in table on 404 line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I did not check enough.
I added it at 27da644.