From 97460fea0f9449bb10f205365fb1b03e1f18b359 Mon Sep 17 00:00:00 2001
From: Bastien DUMONT <bdumont@grandlyon.com>
Date: Thu, 26 Oct 2023 08:50:36 +0000
Subject: [PATCH] fix(conso): adjust placeholder values between 0 & 1

---
 .../MaxConsumptionCard.spec.tsx.snap          | 16 ++++----
 src/components/Charts/Bar.tsx                 | 39 +++++++++++--------
 .../__snapshots__/BarChart.spec.tsx.snap      | 16 ++++----
 yarn.lock                                     |  4 +-
 4 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/src/components/Analysis/MaxConsumptionCard/__snapshots__/MaxConsumptionCard.spec.tsx.snap b/src/components/Analysis/MaxConsumptionCard/__snapshots__/MaxConsumptionCard.spec.tsx.snap
index 3ab240615..c7b9d8844 100644
--- a/src/components/Analysis/MaxConsumptionCard/__snapshots__/MaxConsumptionCard.spec.tsx.snap
+++ b/src/components/Analysis/MaxConsumptionCard/__snapshots__/MaxConsumptionCard.spec.tsx.snap
@@ -208,7 +208,7 @@ exports[`MaxConsumptionCard component should be rendered correctly 1`] = `
             </g>
             <g
               class="barFill"
-              transform="translate(54.6875, 0)"
+              transform="translate(54.6875, 168.73935065329292)"
             >
               <defs>
                 <lineargradient
@@ -235,13 +235,13 @@ exports[`MaxConsumptionCard component should be rendered correctly 1`] = `
       a4,4 0 0 1 4,-4
       h210.75
       a4,4 0 0 1 4,4
-      v176
+      v7.260649346707083
       h-218.75
       z"
               />
             </g>
             <g
-              transform="translate(54.6875, 0)"
+              transform="translate(54.6875, 168.73935065329292)"
             >
               <defs>
                 <lineargradient
@@ -269,7 +269,7 @@ exports[`MaxConsumptionCard component should be rendered correctly 1`] = `
       a4,4 0 0 1 4,-4
       h210.75
       a4,4 0 0 1 4,4
-      v176
+      v7.260649346707083
       h-218.75
       z"
               />
@@ -291,7 +291,7 @@ exports[`MaxConsumptionCard component should be rendered correctly 1`] = `
             </g>
             <g
               class="barFill"
-              transform="translate(328.125, 19.57862281603289)"
+              transform="translate(328.125, 168.73935065329292)"
             >
               <defs>
                 <lineargradient
@@ -318,13 +318,13 @@ exports[`MaxConsumptionCard component should be rendered correctly 1`] = `
       a4,4 0 0 1 4,-4
       h210.75
       a4,4 0 0 1 4,4
-      v156.4213771839671
+      v7.260649346707083
       h-218.75
       z"
               />
             </g>
             <g
-              transform="translate(328.125, 19.57862281603289)"
+              transform="translate(328.125, 168.73935065329292)"
             >
               <defs>
                 <lineargradient
@@ -352,7 +352,7 @@ exports[`MaxConsumptionCard component should be rendered correctly 1`] = `
       a4,4 0 0 1 4,-4
       h210.75
       a4,4 0 0 1 4,4
-      v156.4213771839671
+      v7.260649346707083
       h-218.75
       z"
               />
diff --git a/src/components/Charts/Bar.tsx b/src/components/Charts/Bar.tsx
index cc5f723be..da8f0f73f 100644
--- a/src/components/Charts/Bar.tsx
+++ b/src/components/Charts/Bar.tsx
@@ -76,14 +76,31 @@ const Bar = ({
     setCompareAnimationEnded(true)
   }
 
+  /** Get date of each fluid to display placeholder bar for multifluid */
+  const lastDataDates = fluidStatus.map(
+    fluid => fluid.lastDataDate ?? DateTime.local(0)
+  )
+  const lastDataDate = DateTime.max(...lastDataDates)
+
+  const dataMissingUpcoming = Boolean(
+    dataload.date > lastDataDate && dataload.date < DateTime.local()
+  )
+
   let value = dataload.value
-  /**
-   * Use 10% of average as the placeholder if average is above 1,
-   * otherwise use a minimum of 1 as the placeholder.
-   */
-  if (value === -1 && average) {
-    value = average > 1 ? average * 0.1 : 1
+
+  /** Adjust value for upcoming data (placeholder bars) */
+  if (dataMissingUpcoming && average) {
+    if (average < 0) {
+      // Use 1 if average is negative
+      value = 1
+    } else if (average > 1) {
+      // Use 10% of average if average is above 1
+      value = average * 0.1
+    } else {
+      value = average
+    }
   }
+
   const yScaleValue = yScale(value) ?? 0
   const yScaleCompareValue = yScale(compareDataload?.value ?? 0) ?? 0
 
@@ -130,16 +147,6 @@ const Bar = ({
     return compare ? xScale.bandwidth() / 2 : xScale.bandwidth()
   }
 
-  /** Get date of each fluid to display placeholder bar for multifluid */
-  const lastDataDates = fluidStatus.map(
-    fluid => fluid.lastDataDate ?? DateTime.local(0)
-  )
-  const lastDataDate = DateTime.max(...lastDataDates)
-
-  const dataMissingUpcoming = Boolean(
-    dataload.date > lastDataDate && dataload.date < DateTime.local()
-  )
-
   const topRoundedRect = (
     x: number,
     y: number,
diff --git a/src/components/Charts/__snapshots__/BarChart.spec.tsx.snap b/src/components/Charts/__snapshots__/BarChart.spec.tsx.snap
index c4118a6aa..57ffaf197 100644
--- a/src/components/Charts/__snapshots__/BarChart.spec.tsx.snap
+++ b/src/components/Charts/__snapshots__/BarChart.spec.tsx.snap
@@ -106,7 +106,7 @@ exports[`BarChart component should render correctly 1`] = `
         </g>
         <g
           class="barFill"
-          transform="translate(33.43750000000003, 0)"
+          transform="translate(33.43750000000003, 309.3554761977037)"
         >
           <defs>
             <lineargradient
@@ -133,13 +133,13 @@ exports[`BarChart component should render correctly 1`] = `
       a4,4 0 0 1 4,-4
       h125.75
       a4,4 0 0 1 4,4
-      v326
+      v16.644523802296305
       h-133.75
       z"
           />
         </g>
         <g
-          transform="translate(33.43750000000003, 0)"
+          transform="translate(33.43750000000003, 309.3554761977037)"
         >
           <defs>
             <lineargradient
@@ -167,7 +167,7 @@ exports[`BarChart component should render correctly 1`] = `
       a4,4 0 0 1 4,-4
       h125.75
       a4,4 0 0 1 4,4
-      v326
+      v16.644523802296305
       h-133.75
       z"
           />
@@ -189,7 +189,7 @@ exports[`BarChart component should render correctly 1`] = `
         </g>
         <g
           class="barFill"
-          transform="translate(200.62500000000003, 35.89414182939363)"
+          transform="translate(200.62500000000003, 309.3554761977037)"
         >
           <defs>
             <lineargradient
@@ -216,13 +216,13 @@ exports[`BarChart component should render correctly 1`] = `
       a4,4 0 0 1 4,-4
       h125.75
       a4,4 0 0 1 4,4
-      v290.1058581706064
+      v16.644523802296305
       h-133.75
       z"
           />
         </g>
         <g
-          transform="translate(200.62500000000003, 35.89414182939363)"
+          transform="translate(200.62500000000003, 309.3554761977037)"
         >
           <defs>
             <lineargradient
@@ -250,7 +250,7 @@ exports[`BarChart component should render correctly 1`] = `
       a4,4 0 0 1 4,-4
       h125.75
       a4,4 0 0 1 4,4
-      v290.1058581706064
+      v16.644523802296305
       h-133.75
       z"
           />
diff --git a/yarn.lock b/yarn.lock
index 5560e50ac..6012e789d 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -12836,9 +12836,9 @@ msgpack5@^4.0.2:
     readable-stream "^2.3.6"
     safe-buffer "^5.1.2"
 
-"mui-bottom-sheet@git+https://github.com/cozy/mui-bottom-sheet.git#v1.0.9":
+"mui-bottom-sheet@https://github.com/cozy/mui-bottom-sheet.git#v1.0.9":
   version "1.0.8"
-  resolved "git+https://github.com/cozy/mui-bottom-sheet.git#3dc4c2a245ab39079bc2f73546bccf80847be14c"
+  resolved "https://github.com/cozy/mui-bottom-sheet.git#3dc4c2a245ab39079bc2f73546bccf80847be14c"
   dependencies:
     "@juggle/resize-observer" "^3.1.3"
     jest-environment-jsdom-sixteen "^1.0.3"
-- 
GitLab