Skip to content
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

compact layout for top languages card #134 #179

Merged
merged 10 commits into from
Jul 26, 2020
2 changes: 2 additions & 0 deletions api/top-langs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = async (req, res) => {
bg_color,
theme,
cache_seconds,
layout
} = req.query;
let topLangs;

Expand Down Expand Up @@ -49,6 +50,7 @@ module.exports = async (req, res) => {
text_color,
bg_color,
theme,
layout
})
);
};
46 changes: 46 additions & 0 deletions src/renderTopLanguages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ const createProgressNode = ({ width, color, name, progress }) => {
`;
};

const createLanguageTextNode = ({langs, totalSize}) => {
let output = ``

for (let i = 0; i < langs.length; i = i+2) {
output+= `
<g transform="translate(0, ${12.5 * i})">
<svg>
<circle cx="5" cy="6" r="5" fill="${langs[i].color || '#858585'}" />
<text x="15" y="12" class='lang-name'>${langs[i].name} ${((langs[i].size / totalSize) * 100).toFixed(2)}%</text>
</svg>
</g>
${langs[i+1] && `
<g transform="translate(150, ${12.5 * i})">
<svg>
<circle cx="5" cy="6" r="5" fill="${langs[i+1].color || '#858585'}" />
<text x="15" y="12" fill='#333' class='lang-name'>${langs[i+1].name} ${((langs[i+1].size / totalSize) * 100).toFixed(2)}%</text>
</svg>
</g>
`}
`
}

return output
}

const lowercaseTrim = (name) => name.toLowerCase().trim();

const renderTopLanguages = (topLangs, options = {}) => {
Expand All @@ -34,6 +59,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
bg_color,
hide,
theme,
layout
} = options;

let langs = Object.values(topLangs);
Expand Down Expand Up @@ -72,6 +98,26 @@ const renderTopLanguages = (topLangs, options = {}) => {
if (hide_title) {
height -= 30;
}

if(layout === 'compact') {
return `
<svg width="${width+50}" height="${height}" viewBox="0 0 ${width+50} ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
<style>
.header { font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor} }
.lang-name { font: 400 14px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
</style>
<rect width="${width+50}" height="${height}" fill='#fff' stroke="#E4E2E2" />
${hide_title ? '' : `<text x="25" y="35" fill='#333' class='header'>Top Languages</text>`}
<svg x='25' y='${hide_title ? 25 : 50}'>
${langs.map(lang => `<rect width='${((lang.size / totalSize) * width).toFixed(2)}' height='10' fill='${lang.color}' rx='5'/>`).join('')}
</svg>
<svg x="25" y="${hide_title ? 50 : 75}">
${createLanguageTextNode({langs, totalSize})}
</svg>
</svg>
`;
}

anuraghazra marked this conversation as resolved.
Show resolved Hide resolved
return `
<svg width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" fill="none" xmlns="http://www.w3.org/2000/svg">
<style>
Expand Down