Skip to content

Commit 80184c7

Browse files
committed
Make fraction decimal places adjustable
1 parent a15d39c commit 80184c7

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ <h1>Valgsimulator</h1>
5454
<p>Påvirker <strong>ikke</strong> beregningen av valgresultatet, men kun visningen av tabellene.</p>
5555
<span>
5656
<input type="checkbox" id="showfraction" oninput="update();"/>
57-
<label for="showfraction">Vis andeler (rundet <strong>ned</strong> til én desimal)</label>
57+
<label for="showfraction">Vis andeler rundet <strong>ned</strong> til</label>
58+
<input type="number" id="decimals" value="1" min="0" max="9" step="1" oninput="update();" style="width: 2.5em"/>
59+
<label for="decimals">desimaler</label>
5860
</span>
5961
<span>
6062
<input type="checkbox" id="groupotherparties" oninput="update();"/>

valg.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ var seatTable = document.getElementById("seats");
33
var voteLink = document.getElementById("voteslink");
44
var seatLink = document.getElementById("seatslink");
55

6-
function printTable(table, data, mergeParties, mergeDistricts, mergedDistrictLabel, showFraction, showColumnTotals, showRowTotals) {
6+
function roundDown(number, decimals) {
7+
number = Math.floor(number * 10**decimals) / 10**decimals;
8+
return number.toFixed(decimals);
9+
};
10+
11+
function printTable(table, data, mergeParties, mergeDistricts, mergedDistrictLabel, showFraction, showColumnTotals, showRowTotals, decimals) {
712
if (mergeParties.length > 0) {
813
var newData = {};
914
for (var district in data) {
@@ -16,7 +21,7 @@ function printTable(table, data, mergeParties, mergeDistricts, mergedDistrictLab
1621
}
1722
}
1823
}
19-
return printTable(table, newData, [], mergeDistricts, mergedDistrictLabel, showFraction, showColumnTotals, showRowTotals);
24+
return printTable(table, newData, [], mergeDistricts, mergedDistrictLabel, showFraction, showColumnTotals, showRowTotals, decimals);
2025
} else if (mergeDistricts.length > 0) {
2126
var newData = {};
2227
newData[mergedDistrictLabel] = {};
@@ -32,10 +37,9 @@ function printTable(table, data, mergeParties, mergeDistricts, mergedDistrictLab
3237
newData[district] = data[district];
3338
}
3439
}
35-
return printTable(table, newData, mergeParties, [], mergedDistrictLabel, showFraction, showColumnTotals, showRowTotals);
40+
return printTable(table, newData, mergeParties, [], mergedDistrictLabel, showFraction, showColumnTotals, showRowTotals, decimals);
3641
}
3742

38-
3943
// Build ordered list of unique parties
4044
var parties = [];
4145
for (var district in data) {
@@ -56,7 +60,7 @@ function printTable(table, data, mergeParties, mergeDistricts, mergedDistrictLab
5660
table.tBodies[0].innerHTML = "";
5761

5862
// Print table
59-
const format = (x, total) => showFraction ? ((Math.floor(1000*x/total)/10).toFixed(1) + " %") : x.toLocaleString("no-NO"); // floor instead of rounding, so parties slightly below threshold cannot seem to be above it (e.g. show 3.999% as 3.9% instead of 4.0%)
63+
const format = (x, total) => showFraction ? (roundDown(100*x/total, decimals) + " %") : x.toLocaleString("no-NO"); // round *down* so parties slightly below threshold cannot seem to be above it (e.g. show 3.999% as 3.9% instead of 4.0%)
6064
var head = table.tHead.insertRow();
6165
var cell = document.createElement("th");
6266
cell.innerHTML = "Valgdistrikt";
@@ -317,9 +321,10 @@ function update() {
317321
}
318322

319323
var showFraction = document.getElementById("showfraction").checked;
324+
var decimals = parseInt(document.getElementById("decimals").value);
320325

321-
printTable(voteTable, votes, mergeParties, mergeDistricts, "Distriktsstemmer", showFraction, true, true);
322-
printTable(seatTable, seats, mergeParties, mergeDistricts, "Distriktsmandater", showFraction, true, true);
326+
printTable(voteTable, votes, mergeParties, mergeDistricts, "Distriktsstemmer", showFraction, true, true, decimals);
327+
printTable(seatTable, seats, mergeParties, mergeDistricts, "Distriktsmandater", showFraction, true, true, decimals);
323328
};
324329

325330
function showTables(showVotes, showSeats) {

0 commit comments

Comments
 (0)