Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import plotly.
|
| 3 |
import pandas as pd
|
| 4 |
-
from datetime import datetime
|
| 5 |
|
| 6 |
# Configuración de la página
|
| 7 |
st.set_page_config(page_title="Generador de Franjas de Tiempo Apiladas", layout="wide")
|
|
@@ -11,11 +11,6 @@ def hex_to_rgba(hex_color, alpha=1.0):
|
|
| 11 |
hex_color = hex_color.lstrip('#')
|
| 12 |
return f'rgba({int(hex_color[0:2], 16)},{int(hex_color[2:4], 16)},{int(hex_color[4:6], 16)},{alpha})'
|
| 13 |
|
| 14 |
-
# Lista de colores predefinidos
|
| 15 |
-
predefined_colors = [
|
| 16 |
-
"#FF5C5C", "#5CCFFF", "#FFA500", "#90EE90", "#9370DB", "#FFD700"
|
| 17 |
-
]
|
| 18 |
-
|
| 19 |
# Sidebar para la configuración del gráfico
|
| 20 |
st.sidebar.header("Configuración del Gráfico")
|
| 21 |
|
|
@@ -50,36 +45,29 @@ for y_name, (start_date, end_date) in y_values.items():
|
|
| 50 |
|
| 51 |
data = pd.DataFrame(data_list)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
end_date = pd.to_datetime(end_date)
|
| 62 |
-
|
| 63 |
-
fig.add_trace(go.Bar(
|
| 64 |
-
y=[y_name],
|
| 65 |
-
x=[(end_date - start_date).days],
|
| 66 |
-
base=start_date,
|
| 67 |
-
orientation='h',
|
| 68 |
-
name=y_name,
|
| 69 |
-
marker_color=colors[i % len(colors)],
|
| 70 |
-
text=f'{y_name}: {start_date.date()} - {end_date.date()}',
|
| 71 |
-
textposition='inside',
|
| 72 |
-
width=0.5
|
| 73 |
-
))
|
| 74 |
|
| 75 |
# Actualizar el diseño del gráfico
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
fig.update_layout(
|
| 77 |
-
title=chart_title,
|
| 78 |
xaxis_title="Tiempo",
|
| 79 |
yaxis_title="Variables",
|
| 80 |
-
barmode='stack' if stacked else 'group',
|
| 81 |
xaxis=dict(
|
| 82 |
-
tickformat=
|
| 83 |
titlefont=dict(size=14),
|
| 84 |
tickfont=dict(size=12),
|
| 85 |
),
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import plotly.express as px
|
| 3 |
import pandas as pd
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
|
| 6 |
# Configuración de la página
|
| 7 |
st.set_page_config(page_title="Generador de Franjas de Tiempo Apiladas", layout="wide")
|
|
|
|
| 11 |
hex_color = hex_color.lstrip('#')
|
| 12 |
return f'rgba({int(hex_color[0:2], 16)},{int(hex_color[2:4], 16)},{int(hex_color[4:6], 16)},{alpha})'
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Sidebar para la configuración del gráfico
|
| 15 |
st.sidebar.header("Configuración del Gráfico")
|
| 16 |
|
|
|
|
| 45 |
|
| 46 |
data = pd.DataFrame(data_list)
|
| 47 |
|
| 48 |
+
# Ajustar fechas para simular apilamiento si está activado
|
| 49 |
+
if stacked:
|
| 50 |
+
# Crear una nueva columna para apilar
|
| 51 |
+
data['Stacked_Start'] = data.groupby('Variable').cumcount() * timedelta(days=1)
|
| 52 |
+
data['Stacked_End'] = data['Stacked_Start'] + (data['End'] - data['Start'])
|
| 53 |
+
|
| 54 |
+
# Crear gráfico de franjas de tiempo
|
| 55 |
+
fig = px.timeline(data, x_start="Stacked_Start" if stacked else "Start", x_end="Stacked_End" if stacked else "End", y="Variable", color="Variable", title=chart_title)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Actualizar el diseño del gráfico
|
| 58 |
+
fig.update_yaxes(categoryorder="total ascending")
|
| 59 |
+
|
| 60 |
+
# Personalizar el formato de tiempo en el eje X
|
| 61 |
+
if time_format == "Año":
|
| 62 |
+
tickformat = "%Y"
|
| 63 |
+
else:
|
| 64 |
+
tickformat = "%Y-%m-%d" # Ajusta según el formato deseado
|
| 65 |
+
|
| 66 |
fig.update_layout(
|
|
|
|
| 67 |
xaxis_title="Tiempo",
|
| 68 |
yaxis_title="Variables",
|
|
|
|
| 69 |
xaxis=dict(
|
| 70 |
+
tickformat=tickformat,
|
| 71 |
titlefont=dict(size=14),
|
| 72 |
tickfont=dict(size=12),
|
| 73 |
),
|