Skip to content

Commit

Permalink
Use automatic percent formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Emir SARI <[email protected]>
  • Loading branch information
bitigchi committed Mar 3, 2024
1 parent da7c0ef commit 1abbfab
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 4 additions & 5 deletions scripts/pi-hole/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,8 @@ function updateSummaryData(runOnce = false) {
intl.format(parseInt(data.clients.total, 10)) + " total clients"
);
glowIfChanged($("span#blocked_queries"), intl.format(parseFloat(data.queries.blocked)));
glowIfChanged(
$("span#percent_blocked"),
parseFloat(data.queries.percent_blocked).toFixed(1) + "%"
);
var formattedPercentage = utils.toPercent(data.queries.percent_blocked, 1);
glowIfChanged($("span#percent_blocked"), formattedPercentage);
glowIfChanged(
$("span#gravity_size"),
intl.format(parseInt(data.gravity.domains_being_blocked, 10))
Expand Down Expand Up @@ -536,7 +534,8 @@ $(function () {
percentage = (100 * blocked) / (permitted + blocked);
}

label += ": " + tooltipLabel.parsed.y + " (" + percentage.toFixed(1) + "%)";
var formattedPercentage = utils.toPercent(percentage, 1);
label += `: ${tooltipLabel.parsed.y} (${formattedPercentage})`;
} else {
label += ": " + tooltipLabel.parsed.y;
}
Expand Down
16 changes: 13 additions & 3 deletions scripts/pi-hole/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,20 @@ function addTD(content) {
return "<td>" + content + "</td> ";
}

function toPercent(number, fractionDigits = 0) {
const userLocale = navigator.language || "en-US";
return new Intl.NumberFormat(userLocale, {
style: "percent",
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
}).format(number / 100);
}

function colorBar(percentage, total, cssClass) {
var title = percentage.toFixed(1) + "% of " + total;
var bar = '<div class="progress-bar ' + cssClass + '" style="width: ' + percentage + '%"></div>';
return '<div class="progress progress-sm" title="' + title + '"> ' + bar + " </div>";
const percentFormatted = toPercent(percentage, 1);
const title = `${percentFormatted} of ${total}`;
const bar = `<div class="progress-bar ${cssClass}" style="width: ${percentage}%"></div>`;
return `<div class="progress progress-sm" title="${title}"> ${bar} </div>`;
}

function checkMessages() {
Expand Down

0 comments on commit 1abbfab

Please sign in to comment.