From 8cb13d8281561d4a500c6b91b60fa87381cecfe2 Mon Sep 17 00:00:00 2001
From: Robert Goldmann <deadlocker@gmx.de>
Date: Sun, 5 Mar 2023 16:38:11 +0100
Subject: [PATCH] Fixed #737 - ssort categories alphabetically in bar charts

---
 .../budgetmaster/charts/DefaultCharts.java    |  4 ++--
 ...omesAndExpendituresPerMonthByCategories.js | 22 ++++++++++---------
 ...comesAndExpendituresPerYearByCategories.js | 18 +++++++--------
 3 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/charts/DefaultCharts.java b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/charts/DefaultCharts.java
index b67257ca1..fe9c88e2f 100644
--- a/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/charts/DefaultCharts.java
+++ b/BudgetMasterServer/src/main/java/de/deadlocker8/budgetmaster/charts/DefaultCharts.java
@@ -40,7 +40,7 @@ public class DefaultCharts
 
 	private static final Chart CHART_INCOMES_AND_EXPENDITURES_PER_MONTH_BY_CATEGORIES = new Chart("charts.default.incomesAndExpendituresPerMonthByCategories",
 			getChartFromFile("charts/IncomesAndExpendituresPerMonthByCategories.js"),
-			ChartType.DEFAULT, 25, ChartDisplayType.BAR, ChartGroupType.MONTH, "incomesAndExpendituresPerMonthByCategories.png");
+			ChartType.DEFAULT, 27, ChartDisplayType.BAR, ChartGroupType.MONTH, "incomesAndExpendituresPerMonthByCategories.png");
 
 	private static final Chart CHART_REST_PER_MONTH = new Chart("charts.default.restPerMonth",
 			getChartFromFile("charts/RestPerMonth.js"),
@@ -52,7 +52,7 @@ public class DefaultCharts
 
 	private static final Chart CHART_INCOMES_AND_EXPENDITURES_PER_YEAR_BY_CATEGORIES = new Chart("charts.default.incomesAndExpendituresPerYearByCategories",
 			getChartFromFile("charts/IncomesAndExpendituresPerYearByCategories.js"),
-			ChartType.DEFAULT, 5, ChartDisplayType.BAR, ChartGroupType.YEAR, "incomesAndExpendituresPerYearByCategories.png");
+			ChartType.DEFAULT, 7, ChartDisplayType.BAR, ChartGroupType.YEAR, "incomesAndExpendituresPerYearByCategories.png");
 
 	private static final Chart CHART_AVERAGE_TRANSACTION_AMOUNT_PER_CATEGORY = new Chart("charts.default.averageTransactionAmountPerCategory",
 			getChartFromFile("charts/AverageTransactionAmountPerCategoryBar.js"),
diff --git a/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerMonthByCategories.js b/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerMonthByCategories.js
index 5d6acc00f..c0c464d7a 100644
--- a/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerMonthByCategories.js
+++ b/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerMonthByCategories.js
@@ -17,19 +17,20 @@ const COLOR = 1;
 const INCOME = 2;
 const EXPENDITURE = 3;
 
-var categoryNames = [];
-var categoryColors = [];
+
+var categories = new Map();
 
 for(var i = 0; i < transactionData.length; i++)
 {
     var currentTransaction = transactionData[i];
-    if(!categoryNames.includes(currentTransaction.category.name))
+    if(!categories.has(currentTransaction.category.name))
     {
-        categoryNames.push(currentTransaction.category.name);
-        categoryColors.push(currentTransaction.category.color);
+        categories.set(currentTransaction.category.name, currentTransaction.category.color);
     }
 }
 
+var sortedCategories = new Map([...categories.entries()].sort());
+
 var dates = [];
 var values = [];
 
@@ -42,10 +43,10 @@ for(var i = 0; i < transactionData.length; i++)
     {
         dates.push(date);
         values.push([
-            categoryNames, // NAME
-            categoryColors, // COLOR
-            new Array(categoryNames.length).fill(0), // INCOME
-            new Array(categoryNames.length).fill(0)  // EXPENDITURE
+            Array.from(sortedCategories.keys()), // NAME
+            Array.from(sortedCategories.values()), // COLOR
+            new Array(sortedCategories.size).fill(0), // INCOME
+            new Array(sortedCategories.size).fill(0)  // EXPENDITURE
         ]);
     }
 
@@ -201,7 +202,8 @@ Plotly.newPlot("containerID", plotlyData, plotlyLayout, plotlyConfig);
 REGEX_CATGEORY_NAME = new RegExp("(.*)\\s\\d+.\\d%");
 
 var plotContainer = document.getElementById('containerID');
-plotContainer.on('plotly_click', function(data){
+plotContainer.on('plotly_click', function(data)
+{
     if(data.event.shiftKey !== true)
     {
         return;
diff --git a/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerYearByCategories.js b/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerYearByCategories.js
index ff3413d9a..1b5b5a253 100644
--- a/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerYearByCategories.js
+++ b/BudgetMasterServer/src/main/resources/charts/IncomesAndExpendituresPerYearByCategories.js
@@ -17,19 +17,19 @@ const COLOR = 1;
 const INCOME = 2;
 const EXPENDITURE = 3;
 
-var categoryNames = [];
-var categoryColors = [];
+var categories = new Map();
 
 for(var i = 0; i < transactionData.length; i++)
 {
     var currentTransaction = transactionData[i];
-    if(!categoryNames.includes(currentTransaction.category.name))
+    if(!categories.has(currentTransaction.category.name))
     {
-        categoryNames.push(currentTransaction.category.name);
-        categoryColors.push(currentTransaction.category.color);
+        categories.set(currentTransaction.category.name, currentTransaction.category.color);
     }
 }
 
+var sortedCategories = new Map([...categories.entries()].sort());
+
 var dates = [];
 var values = [];
 
@@ -42,10 +42,10 @@ for(var i = 0; i < transactionData.length; i++)
     {
         dates.push(date);
         values.push([
-            categoryNames, // NAME
-            categoryColors, // COLOR
-            new Array(categoryNames.length).fill(0), // INCOME
-            new Array(categoryNames.length).fill(0)  // EXPENDITURE
+            Array.from(sortedCategories.keys()), // NAME
+            Array.from(sortedCategories.values()), // COLOR
+            new Array(sortedCategories.size).fill(0), // INCOME
+            new Array(sortedCategories.size).fill(0)  // EXPENDITURE
         ]);
     }
 
-- 
GitLab