atodorov284 commited on
Commit
662f1f0
·
1 Parent(s): b003484

gradient color for gauge

Browse files
streamlit_src/controllers/user_controller.py CHANGED
@@ -340,7 +340,7 @@ class UserController:
340
  Returns:
341
  go.Figure: A Plotly figure representing the gauge plot.
342
  """
343
- color = "green" if value <= guideline else "red"
344
  fig = go.Figure(
345
  go.Indicator(
346
  mode="gauge+number",
@@ -351,3 +351,27 @@ class UserController:
351
  )
352
  fig.update_layout(height=250, width=250, margin=dict(t=0, b=0, l=0, r=0))
353
  return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
  Returns:
341
  go.Figure: A Plotly figure representing the gauge plot.
342
  """
343
+ color = self._get_color(value, guideline)
344
  fig = go.Figure(
345
  go.Indicator(
346
  mode="gauge+number",
 
351
  )
352
  fig.update_layout(height=250, width=250, margin=dict(t=0, b=0, l=0, r=0))
353
  return fig
354
+
355
+ def _get_color(self, value: float, who_limit: float) -> str:
356
+ """
357
+ Calculate a color based on a given pollutant value and WHO guideline.
358
+
359
+ Args:
360
+ value (float): The pollutant concentration value.
361
+ who_limit (float): The WHO guideline value for the pollutant.
362
+
363
+ Returns:
364
+ str: A hex color code representing the calculated color.
365
+ """
366
+ half_who_limit = who_limit / 2
367
+
368
+ if value <= half_who_limit:
369
+ # Green -> Yellow gradient
370
+ return f"rgba({int(255 * value / half_who_limit)}, 255, 0, 1)" # Gradient from green to yellow
371
+ elif value <= who_limit:
372
+ # Yellow -> Red gradient
373
+ excess_value = value - half_who_limit
374
+ return f"rgba(255, {int(255 - (255 * excess_value / half_who_limit))}, 0, 1)" # Gradient from yellow to red
375
+ else:
376
+ # Beyond the WHO limit, fully red
377
+ return "rgba(255, 0, 0, 1)" # Fully red