Skip to content

Commit ccbd978

Browse files
committed
Enhance results view and add voting analysis features
- Introduced detailed voting analysis in the ResultsView, including co-approval rates, approval distributions, and "Anyone But" analysis. - Updated results.html to display new voting patterns and analysis tables. - Improved JavaScript for dynamic chart color adjustments based on dark mode preferences. - Enhanced CSS for better styling of voting analysis tables and charts. - Adjusted base.html and index.html for improved layout and button styling.
1 parent c45a830 commit ccbd978

8 files changed

Lines changed: 1039 additions & 39 deletions

File tree

approval_polls/staticfiles/embed_instructions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ $(document).ready(function () {
3535
document.execCommand("copy");
3636
if ($("#alert-success").length === 0) {
3737
$(
38-
"<div id = 'alert-success' class='alert alert-success' style='margin-top:10px'>" +
38+
"<div id = 'alert-success' class='alert alert-success alert-margin-top'>" +
3939
"<strong>Success!</strong> Text copied to clipboard!.</div>",
4040
).insertAfter($("#copyText"));
4141
}
4242
} catch (err) {
4343
if ($("#alert-danger").length === 0) {
4444
$(
45-
"<div id = 'alert-danger' class='alert alert-danger' style='margin-top:10px'>" +
45+
"<div id = 'alert-danger' class='alert alert-danger alert-margin-top'>" +
4646
"<strong>Oops, unable to copy!</strong>" +
4747
" To copy the text to clipboard: Ctrl+C!.</div>",
4848
).insertAfter($("#copyText"));

approval_polls/staticfiles/results.js

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,71 @@ document.addEventListener("DOMContentLoaded", async function () {
3030
// Build a debug table of ballots (optional)
3131
buildVotesTable(rawBallots, choices);
3232

33+
// Helper function to detect dark mode
34+
function isDarkMode() {
35+
return (
36+
window.matchMedia &&
37+
window.matchMedia("(prefers-color-scheme: dark)").matches
38+
);
39+
}
40+
41+
// Get chart colors based on dark mode
42+
function getChartColors() {
43+
if (isDarkMode()) {
44+
// Dark mode colors - brighter and more saturated
45+
return {
46+
backgroundColor: [
47+
"rgba(75, 192, 192, 0.8)",
48+
"rgba(255, 99, 132, 0.8)",
49+
"rgba(255, 206, 86, 0.8)",
50+
"rgba(54, 162, 235, 0.8)",
51+
"rgba(153, 102, 255, 0.8)",
52+
"rgba(255, 159, 64, 0.8)",
53+
],
54+
borderColor: [
55+
"rgba(75, 192, 192, 1)",
56+
"rgba(255, 99, 132, 1)",
57+
"rgba(255, 206, 86, 1)",
58+
"rgba(54, 162, 235, 1)",
59+
"rgba(153, 102, 255, 1)",
60+
"rgba(255, 159, 64, 1)",
61+
],
62+
};
63+
} else {
64+
// Light mode colors
65+
return {
66+
backgroundColor: [
67+
"rgba(75, 192, 192, 0.6)",
68+
"rgba(255, 99, 132, 0.6)",
69+
"rgba(255, 206, 86, 0.6)",
70+
"rgba(54, 162, 235, 0.6)",
71+
"rgba(153, 102, 255, 0.6)",
72+
"rgba(255, 159, 64, 0.6)",
73+
],
74+
borderColor: [
75+
"rgba(75, 192, 192, 1)",
76+
"rgba(255, 99, 132, 1)",
77+
"rgba(255, 206, 86, 1)",
78+
"rgba(54, 162, 235, 1)",
79+
"rgba(153, 102, 255, 1)",
80+
"rgba(255, 159, 64, 1)",
81+
],
82+
};
83+
}
84+
}
85+
3386
// 2. Create Chart.js pie chart
3487
const ctx = document.getElementById("spavChart").getContext("2d");
88+
const chartColors = getChartColors();
3589
const spavChart = new Chart(ctx, {
3690
type: "pie",
3791
data: {
3892
labels: choices.map((c) => c.choice_text),
3993
datasets: [
4094
{
4195
data: choices.map(() => 0),
42-
backgroundColor: [
43-
"rgba(75, 192, 192, 0.6)",
44-
"rgba(255, 99, 132, 0.6)",
45-
"rgba(255, 206, 86, 0.6)",
46-
"rgba(54, 162, 235, 0.6)",
47-
"rgba(153, 102, 255, 0.6)",
48-
"rgba(255, 159, 64, 0.6)",
49-
],
50-
borderColor: [
51-
"rgba(75, 192, 192, 1)",
52-
"rgba(255, 99, 132, 1)",
53-
"rgba(255, 206, 86, 1)",
54-
"rgba(54, 162, 235, 1)",
55-
"rgba(153, 102, 255, 1)",
56-
"rgba(255, 159, 64, 1)",
57-
],
96+
backgroundColor: chartColors.backgroundColor,
97+
borderColor: chartColors.borderColor,
5898
borderWidth: 1,
5999
},
60100
],
@@ -64,11 +104,28 @@ document.addEventListener("DOMContentLoaded", async function () {
64104
plugins: {
65105
legend: {
66106
position: "bottom",
107+
labels: {
108+
color: isDarkMode() ? "#e0e0e0" : "#000000",
109+
},
67110
},
68111
},
69112
},
70113
});
71114

115+
// Listen for dark mode changes and update chart colors
116+
if (window.matchMedia) {
117+
const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
118+
darkModeQuery.addEventListener("change", (e) => {
119+
const newColors = getChartColors();
120+
spavChart.data.datasets[0].backgroundColor = newColors.backgroundColor;
121+
spavChart.data.datasets[0].borderColor = newColors.borderColor;
122+
spavChart.options.plugins.legend.labels.color = e.matches
123+
? "#e0e0e0"
124+
: "#000000";
125+
spavChart.update();
126+
});
127+
}
128+
72129
// 3. SPAV allowing multiple seats, now with a debug log
73130
function spav(choices, ballots, seats) {
74131
const debugLines = [];

0 commit comments

Comments
 (0)