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 250e6cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
18 changes: 18 additions & 0 deletions scripts/pi-hole/js/formatter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2024 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */

export 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);
}

// Appease the CI
toPercent(0);
11 changes: 6 additions & 5 deletions scripts/pi-hole/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/* global utils:false, Chart:false, apiFailure:false, THEME_COLORS:false, customTooltips:false, htmlLegendPlugin:false,doughnutTooltip:false, ChartDeferred:false, REFRESH_INTERVAL: false */

import { toPercent } from "./formatter.mjs";

// Define global variables
var timeLineChart, clientsChart;
var queryTypePieChart, forwardDestinationPieChart;
Expand Down Expand Up @@ -397,10 +399,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 = 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 +536,8 @@ $(function () {
percentage = (100 * blocked) / (permitted + blocked);
}

label += ": " + tooltipLabel.parsed.y + " (" + percentage.toFixed(1) + "%)";
var formattedPercentage = toPercent(percentage, 1);
label += `: ${tooltipLabel.parsed.y} (${formattedPercentage})`;
} else {
label += ": " + tooltipLabel.parsed.y;
}
Expand Down
9 changes: 6 additions & 3 deletions scripts/pi-hole/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/* global moment:false, apiFailure: false, updateFtlInfo: false */

import { toPercent } from "./formatter.mjs";

$(function () {
// CSRF protection for AJAX requests, this has to be configured globally
// because we are using the jQuery $.ajax() function directly in some cases
Expand Down Expand Up @@ -379,9 +381,10 @@ function addTD(content) {
}

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 250e6cd

Please sign in to comment.