From cdfb867037a39c10400e6e58e66d71f2f47e7561 Mon Sep 17 00:00:00 2001
From: Pierre-Antoine Rouby <pierre-antoine.rouby@inrae.fr>
Date: Fri, 20 Oct 2023 16:27:07 +0200
Subject: [PATCH] Results: CustomPlot: Add custom time axes.

---
 src/View/Results/CustomPlot/Plot.py | 43 +++++++++++++++++++++++++++++
 src/View/Results/translate.py       |  7 +++++
 2 files changed, 50 insertions(+)

diff --git a/src/View/Results/CustomPlot/Plot.py b/src/View/Results/CustomPlot/Plot.py
index a539112a..4712c45b 100644
--- a/src/View/Results/CustomPlot/Plot.py
+++ b/src/View/Results/CustomPlot/Plot.py
@@ -19,6 +19,7 @@
 import logging
 
 from functools import reduce
+from datetime import datetime
 
 from tools import timer
 from View.Tools.PamhyrPlot import PamhyrPlot
@@ -139,6 +140,46 @@ class CustomPlot(PamhyrPlot):
                 color='r',
             )
 
+    def _customize_x_axes_time(self, ts, mode="time"):
+        # Custom time display
+        nb = len(ts)
+        mod = int(nb / 5)
+        mod = mod if mod > 0 else nb
+
+        fx = list(
+            map(
+                lambda x: x[1],
+                filter(
+                    lambda x: x[0] % mod == 0,
+                    enumerate(ts)
+                )
+            )
+        )
+
+        if mode == "time":
+            t0 = datetime.fromtimestamp(0)
+            xt = list(
+                map(
+                    lambda v: (
+                        str(
+                            datetime.fromtimestamp(v) - t0
+                        ).split(",")[0]
+                        .replace("days", self._trad["days"])
+                        .replace("day", self._trad["day"])
+                    ),
+                    fx
+                )
+            )
+        else:
+            xt = list(
+                map(
+                    lambda v: str(datetime.fromtimestamp(v).date()),
+                    fx
+                )
+            )
+
+        self.canvas.axes.set_xticks(ticks=fx, labels=xt, rotation=45)
+
     def _draw_time(self):
         results = self.data
         reach = results.river.reach(self._reach)
@@ -213,6 +254,8 @@ class CustomPlot(PamhyrPlot):
                 color='r',
             )
 
+        self._customize_x_axes_time(ts)
+
     @timer
     def draw(self):
         self.canvas.axes.cla()
diff --git a/src/View/Results/translate.py b/src/View/Results/translate.py
index ae9dcad1..4de61cc7 100644
--- a/src/View/Results/translate.py
+++ b/src/View/Results/translate.py
@@ -27,6 +27,13 @@ class ResultsTranslate(PamhyrTranslate):
     def __init__(self):
         super(ResultsTranslate, self).__init__()
 
+        self._dict['day'] = _translate(
+            "Results", "day"
+        )
+        self._dict['days'] = _translate(
+            "Results", "days"
+        )
+
         self._sub_dict["table_headers_reach"] = {
             "name": _translate("Results", "Reach name"),
         }
-- 
GitLab