From 7c85d2a5ed360433e93577ea84f387ca72c1a4eb Mon Sep 17 00:00:00 2001
From: Numbtus <matias.mennecart.etu@univ-lille.fr>
Date: Fri, 29 Nov 2024 10:12:39 +0100
Subject: [PATCH] Fix legend & change colors

---
 res/stages/main-stage.fxml                    |  4 +-
 .../DataVisualizationController.java          |  7 ++--
 .../classification/model/LoadableData.java    | 32 ++++++++++++++--
 .../sae/classification/utils/ViewUtil.java    | 38 +++++++++++++++++--
 .../classification/view/DataStageView.java    |  9 +++--
 .../classification/view/MainStageView.java    |  5 ++-
 6 files changed, 76 insertions(+), 19 deletions(-)

diff --git a/res/stages/main-stage.fxml b/res/stages/main-stage.fxml
index c2f1a36..1e6fdc9 100644
--- a/res/stages/main-stage.fxml
+++ b/res/stages/main-stage.fxml
@@ -44,11 +44,11 @@
                                              </Label>
                                              <VBox layoutY="345.0" prefHeight="78.0" prefWidth="678.0">
                                                 <children>
-                                                   <HBox fx:id="legend" alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
+                                                   <VBox fx:id="legend" alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="10.0">
                                                       <padding>
                                                          <Insets left="2.0" right="2.0" />
                                                       </padding>
-                                                   </HBox>
+                                                   </VBox>
                                                 </children></VBox>
                                           </children>
                                           <HBox.margin>
diff --git a/src/main/java/fr/univlille/sae/classification/controller/DataVisualizationController.java b/src/main/java/fr/univlille/sae/classification/controller/DataVisualizationController.java
index b0e8c06..5ef74cc 100644
--- a/src/main/java/fr/univlille/sae/classification/controller/DataVisualizationController.java
+++ b/src/main/java/fr/univlille/sae/classification/controller/DataVisualizationController.java
@@ -8,6 +8,7 @@ import javafx.scene.chart.NumberAxis;
 import javafx.scene.chart.ScatterChart;
 import javafx.scene.control.Label;
 import javafx.scene.layout.HBox;
+import javafx.scene.layout.VBox;
 import javafx.stage.Stage;
 
 public abstract class DataVisualizationController {
@@ -20,7 +21,7 @@ public abstract class DataVisualizationController {
     Label AxesSelected;
 
     @FXML
-    HBox legend;
+    VBox legend;
 
 
     @FXML
@@ -147,9 +148,9 @@ public abstract class DataVisualizationController {
 
 
 
-    public void loadLegend(HBox hbox) {
+    public void loadLegend(VBox vBox) {
         this.legend.getChildren().clear();
-        this.legend.getChildren().addAll(hbox.getChildren());
+        this.legend.getChildren().addAll(vBox.getChildren());
     }
 
 
diff --git a/src/main/java/fr/univlille/sae/classification/model/LoadableData.java b/src/main/java/fr/univlille/sae/classification/model/LoadableData.java
index 670de56..d1b6696 100644
--- a/src/main/java/fr/univlille/sae/classification/model/LoadableData.java
+++ b/src/main/java/fr/univlille/sae/classification/model/LoadableData.java
@@ -50,18 +50,40 @@ public abstract class LoadableData {
     public static void setClassificationTypes(Set<String> classificationTypes) {
         LoadableData.classificationTypes = classificationTypes;
         LoadableData.classification.clear();
-
+        int nbOfColors = classificationTypes.size() + 1;
         int nb = 0;
         for(String s : classificationTypes) {
             // Génération de couleurs avec une plage évitant le blanc
 
-            LoadableData.classification.put(s, getColor(nb++));
+            LoadableData.classification.put(s, getColor(nb++, nbOfColors));
+        }
+
+        LoadableData.classification.put("undefined", getColor(nb,nbOfColors));
+    }
+
+
+    private static Color getColor(int nb, int totalColors) {
+        // Ratio pour répartir les couleurs uniformément
+        double ratio = (double) nb / (double) totalColors;
+
+        // Utilisation de fonctions trigonométriques pour des transitions douces
+        double red = 0.5 + 0.4 * Math.sin(2 * Math.PI * ratio); // Oscille entre 0.1 et 0.9
+        double green = 0.5 + 0.4 * Math.sin(2 * Math.PI * ratio + Math.PI / 3); // Décalage de phase
+        double blue = 0.5 + 0.4 * Math.sin(2 * Math.PI * ratio + 2 * Math.PI / 3); // Décalage de phase
+
+        // Réduction de la luminosité pour éviter le blanc et gris clair
+        double maxComponent = Math.max(red, Math.max(green, blue));
+        if (maxComponent > 0.8) {
+            red *= 0.8 / maxComponent;
+            green *= 0.8 / maxComponent;
+            blue *= 0.8 / maxComponent;
         }
 
-        LoadableData.classification.put("undefined", getColor(nb));
+        // Conversion en objet Color
+        return Color.color(red, green, blue);
     }
 
-    private static Color getColor(int i) {
+   /* private static Color getColor(int i) {
         double ratio = (double) i / classificationTypes.size();
 
         // Réduire les composantes pour éviter les tons clairs
@@ -73,6 +95,8 @@ public abstract class LoadableData {
     }
 
 
+    */
+
     /**
      * Définit la classification de l'objet.
      * @param classification classification à définir.
diff --git a/src/main/java/fr/univlille/sae/classification/utils/ViewUtil.java b/src/main/java/fr/univlille/sae/classification/utils/ViewUtil.java
index a0e92fd..2217c06 100644
--- a/src/main/java/fr/univlille/sae/classification/utils/ViewUtil.java
+++ b/src/main/java/fr/univlille/sae/classification/utils/ViewUtil.java
@@ -4,12 +4,14 @@ import fr.univlille.sae.classification.controller.DataStageController;
 import fr.univlille.sae.classification.controller.MainStageController;
 import fr.univlille.sae.classification.model.ClassificationModel;
 import fr.univlille.sae.classification.model.LoadableData;
+import javafx.geometry.Pos;
 import javafx.scene.chart.ScatterChart;
 import javafx.scene.chart.XYChart;
 import javafx.scene.control.ContextMenu;
 import javafx.scene.control.Label;
 import javafx.scene.control.MenuItem;
 import javafx.scene.layout.HBox;
+import javafx.scene.layout.VBox;
 import javafx.scene.paint.Color;
 import javafx.scene.shape.Circle;
 import javafx.scene.shape.Rectangle;
@@ -56,19 +58,46 @@ public class ViewUtil {
     }
 
 
-    public static HBox loadLegend() {
+    public static VBox loadLegend() {
         //Color
 
         Map<String, Color> colors = new HashMap<>(Map.copyOf(LoadableData.getClassifications()));
         Rectangle rectangle = new Rectangle(10, 10);
         rectangle.setFill(colors.remove("undefined"));
         Label label = new Label("undefined");
-        HBox hbox = new HBox();
+        VBox legend = new VBox();
+        legend.setAlignment(Pos.CENTER);
+        HBox line = new HBox();
+        line.setSpacing(10);
+        line.setAlignment(Pos.CENTER);
+
         HBox tempHBox = new HBox();
         tempHBox.getChildren().addAll(rectangle, label);
-        hbox.getChildren().add(tempHBox);
+        line.getChildren().add(tempHBox);
+
+        String[] colorsString = colors.keySet().toArray(new String[0]);
+        for(int i = 0 ; i < colorsString.length ; i+= 7) {
+            for(int j = 0 ; i+j < colorsString.length && j < i+7 ; j++) {
+                if(j%7 == 0 && i != 0 ) {
+                    legend.getChildren().add(line);
+                    line = new HBox();
+                    line.setSpacing(10);
+                    line.setAlignment(Pos.CENTER);
+                }
+
+                tempHBox = new HBox();
+                label = new Label(colorsString[i+j]);
+                rectangle = new Rectangle(10, 10);
+                rectangle.setFill(colors.get(colorsString[i+j]));
+                tempHBox.getChildren().addAll(rectangle, label);
+                line.getChildren().add(tempHBox);
 
+            }
+        }
+
+        if(colorsString.length < 7) legend.getChildren().add(line);
 
+/**
         for(String s : colors.keySet()) {
             Circle c = new Circle(5);
             c.setFill(colors.get(s));
@@ -78,8 +107,9 @@ public class ViewUtil {
 
             hbox.getChildren().add(tempHBox);
         }
+ */
 
-        return hbox;
+        return legend;
     }
 
 }
diff --git a/src/main/java/fr/univlille/sae/classification/view/DataStageView.java b/src/main/java/fr/univlille/sae/classification/view/DataStageView.java
index f14feab..dd613b1 100644
--- a/src/main/java/fr/univlille/sae/classification/view/DataStageView.java
+++ b/src/main/java/fr/univlille/sae/classification/view/DataStageView.java
@@ -14,6 +14,7 @@ import javafx.scene.Node;
 import javafx.scene.chart.ScatterChart;
 import javafx.scene.chart.XYChart;
 import javafx.scene.layout.HBox;
+import javafx.scene.layout.VBox;
 import javafx.scene.shape.Circle;
 import javafx.scene.shape.Rectangle;
 import javafx.stage.Stage;
@@ -155,8 +156,8 @@ public class DataStageView extends DataVisualizationView implements Observer {
                 }
                 scatterChart.getData().addAll(serieList.values());
 
-                HBox hBox = ViewUtil.loadLegend();
-                controller.loadLegend(hBox);
+                VBox vBox = ViewUtil.loadLegend();
+                controller.loadLegend(vBox);
             }
         } catch (Exception e) {
             System.err.println("Erreur de mise à jour : " + e.getMessage());
@@ -199,8 +200,8 @@ public class DataStageView extends DataVisualizationView implements Observer {
                 series4.setName("indéfini");
                 scatterChart.getData().add(series4);
             }
-            HBox hBox = ViewUtil.loadLegend();
-            controller.loadLegend(hBox);
+            VBox vBox = ViewUtil.loadLegend();
+            controller.loadLegend(vBox);
         } catch (Exception e) {
             System.err.println("Erreur de mise à jour : " + e.getMessage());
         }
diff --git a/src/main/java/fr/univlille/sae/classification/view/MainStageView.java b/src/main/java/fr/univlille/sae/classification/view/MainStageView.java
index 39d6cb9..ad6aff4 100644
--- a/src/main/java/fr/univlille/sae/classification/view/MainStageView.java
+++ b/src/main/java/fr/univlille/sae/classification/view/MainStageView.java
@@ -13,6 +13,7 @@ import javafx.scene.chart.ScatterChart;
 import javafx.scene.chart.XYChart;
 import javafx.scene.control.*;
 import javafx.scene.layout.HBox;
+import javafx.scene.layout.VBox;
 import javafx.scene.shape.*;
 import javafx.stage.Stage;
 
@@ -169,8 +170,8 @@ public class MainStageView extends DataVisualizationView implements Observer {
                 scatterChart.getData().addAll(serieList.values());
 
 
-                HBox hBox = ViewUtil.loadLegend();
-                controller.loadLegend(hBox);
+                VBox vBox = ViewUtil.loadLegend();
+                controller.loadLegend(vBox);
             }
 
 
-- 
GitLab