From 982a7ffd655a746a3edf82cecff82031b674d2e8 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Wed, 24 Jul 2024 15:23:52 +0100 Subject: [PATCH] vote_chart: sort parsed votes The SQL query retrieving the votes is deterministicly sorted, but goes through a HashMap, for ease of processing and transfer, which does not maintaing order. Sort the resulting object in the Javascript to keep a consistent order in the graph. --- src/vote.rs | 2 ++ static_files/vote_chart.js | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/vote.rs b/src/vote.rs index 09239f6..d4e5d42 100644 --- a/src/vote.rs +++ b/src/vote.rs @@ -115,6 +115,8 @@ pub async fn fetch_vote_data(week: u8, mut db: Connection) -> Opti } + // The hash map makes storing and retrieving the data really easy, *but* + // it does lose the order of the original array (which is sorted via the SQL). let mut vote_data = HashMap::>::new(); let mut next_truth_number = 1; for raw_vote in raw_votes { diff --git a/static_files/vote_chart.js b/static_files/vote_chart.js index 7bdae87..45b3ab2 100644 --- a/static_files/vote_chart.js +++ b/static_files/vote_chart.js @@ -32,11 +32,15 @@ async function main() { return; } + // Sort by label to maintain the same graph order, as it goes through a hash map in the backend. + datasets.sort((a, b) => a.label > b.label) + const chart_canvas = document.getElementById("vote_chart") let chart function create_chart(keys, data) { let main_axis; let aspect_ratio; + // TODO: Move to the event, to check and not re-create for no reason. if (window.innerWidth > window.innerHeight) { main_axis = 'x' aspect_ratio = 2