Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import plotly.express as px
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
# Configuración de la página
|
| 7 |
st.set_page_config(page_title="Generador de Gráficos Personalizado", layout="wide")
|
|
@@ -29,11 +30,31 @@ st.sidebar.header("Configuración del Gráfico")
|
|
| 29 |
chart_title = st.sidebar.text_input("Título del Gráfico", "Generador de Gráfico")
|
| 30 |
|
| 31 |
# Tipo de gráfico
|
| 32 |
-
chart_type = st.sidebar.selectbox("Tipo de Gráfico", ["Línea", "Área", "Dispersión", "Barras", "Donut"])
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Selector de número de variables Y
|
| 39 |
num_y_vars = st.sidebar.number_input("Número de variables Y", min_value=1, max_value=100, value=1, step=1, key="num_y_vars")
|
|
@@ -76,11 +97,11 @@ use_multiple_colors = st.sidebar.checkbox("Usar múltiples colores", value=True
|
|
| 76 |
selected_color = st.sidebar.color_picker("Color", "#24CBA0", key="single_color")
|
| 77 |
|
| 78 |
# Definir colores
|
| 79 |
-
if use_multiple_colors
|
| 80 |
-
num_colors =
|
| 81 |
-
colors = [hex_to_rgba(
|
| 82 |
for i in range(num_colors)]
|
| 83 |
-
border_colors = [hex_to_rgba(
|
| 84 |
for i in range(num_colors)]
|
| 85 |
else:
|
| 86 |
color = hex_to_rgba(selected_color, alpha=opacity)
|
|
@@ -145,9 +166,9 @@ else:
|
|
| 145 |
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 146 |
elif chart_type == "Barras":
|
| 147 |
if horizontal_bars:
|
| 148 |
-
fig = px.bar(data, x=y_names_list
|
| 149 |
else:
|
| 150 |
-
fig = px.bar(data, x="X", y=y_names_list, barmode='stack'
|
| 151 |
fig.update_traces(hovertemplate=hovertemplate)
|
| 152 |
if use_multiple_colors:
|
| 153 |
if len(y_names_list) == 1: # Solo una variable Y
|
|
@@ -184,6 +205,23 @@ else:
|
|
| 184 |
figs.append(fig)
|
| 185 |
for fig in figs:
|
| 186 |
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
# Añadir anotación para el título
|
| 189 |
if chart_type != "Donut":
|
|
@@ -218,4 +256,4 @@ else:
|
|
| 218 |
fig.update_layout(xaxis_autorange=True, yaxis_autorange=True)
|
| 219 |
|
| 220 |
# Información adicional
|
| 221 |
-
st.write("
|
|
|
|
| 2 |
import plotly.express as px
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
# Configuración de la página
|
| 8 |
st.set_page_config(page_title="Generador de Gráficos Personalizado", layout="wide")
|
|
|
|
| 30 |
chart_title = st.sidebar.text_input("Título del Gráfico", "Generador de Gráfico")
|
| 31 |
|
| 32 |
# Tipo de gráfico
|
| 33 |
+
chart_type = st.sidebar.selectbox("Tipo de Gráfico", ["Línea", "Área", "Dispersión", "Barras", "Donut", "Línea temporal"])
|
| 34 |
|
| 35 |
+
# Función para generar rangos de fechas
|
| 36 |
+
def generate_date_range(start_date, end_date, freq):
|
| 37 |
+
return pd.date_range(start=start_date, end=end_date, freq=freq).strftime('%Y-%m-%d').tolist()
|
| 38 |
+
|
| 39 |
+
if chart_type == "Línea temporal":
|
| 40 |
+
time_unit = st.sidebar.selectbox("Unidad de tiempo", ["Días", "Meses", "Años"])
|
| 41 |
+
|
| 42 |
+
if time_unit == "Días":
|
| 43 |
+
start_date = st.sidebar.date_input("Fecha de inicio", datetime(2023, 1, 1))
|
| 44 |
+
end_date = st.sidebar.date_input("Fecha de fin", datetime(2023, 12, 31))
|
| 45 |
+
x = generate_date_range(start_date, end_date, 'D')
|
| 46 |
+
elif time_unit == "Meses":
|
| 47 |
+
start_date = st.sidebar.date_input("Fecha de inicio", datetime(2023, 1, 1))
|
| 48 |
+
end_date = st.sidebar.date_input("Fecha de fin", datetime(2023, 12, 31))
|
| 49 |
+
x = generate_date_range(start_date, end_date, 'M')
|
| 50 |
+
elif time_unit == "Años":
|
| 51 |
+
start_year = st.sidebar.number_input("Año de inicio", min_value=2000, max_value=2100, value=2020)
|
| 52 |
+
end_year = st.sidebar.number_input("Año de fin", min_value=2000, max_value=2100, value=2023)
|
| 53 |
+
x = [str(year) for year in range(start_year, end_year + 1)]
|
| 54 |
+
else:
|
| 55 |
+
# Ingresar valores para los ejes
|
| 56 |
+
x_values = st.sidebar.text_area("Valores para X (separados por comas)", "2013,2014,2015,2016,2017,2018")
|
| 57 |
+
x = x_values.split(",")
|
| 58 |
|
| 59 |
# Selector de número de variables Y
|
| 60 |
num_y_vars = st.sidebar.number_input("Número de variables Y", min_value=1, max_value=100, value=1, step=1, key="num_y_vars")
|
|
|
|
| 97 |
selected_color = st.sidebar.color_picker("Color", "#24CBA0", key="single_color")
|
| 98 |
|
| 99 |
# Definir colores
|
| 100 |
+
if use_multiple_colors:
|
| 101 |
+
num_colors = len(x)
|
| 102 |
+
colors = [hex_to_rgba(st.sidebar.color_picker(f"Color {i+1}", predefined_colors[i % len(predefined_colors)], key=f"color_{i}"), alpha=opacity)
|
| 103 |
for i in range(num_colors)]
|
| 104 |
+
border_colors = [hex_to_rgba(st.sidebar.color_picker(f"Color de Borde {i+1}", predefined_colors[i % len(predefined_colors)], key=f"border_color_{i}"), alpha=border_opacity)
|
| 105 |
for i in range(num_colors)]
|
| 106 |
else:
|
| 107 |
color = hex_to_rgba(selected_color, alpha=opacity)
|
|
|
|
| 166 |
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 167 |
elif chart_type == "Barras":
|
| 168 |
if horizontal_bars:
|
| 169 |
+
fig = px.bar(data, x=y_names_list, y="X", orientation='h', barmode='stack')
|
| 170 |
else:
|
| 171 |
+
fig = px.bar(data, x="X", y=y_names_list, barmode='stack')
|
| 172 |
fig.update_traces(hovertemplate=hovertemplate)
|
| 173 |
if use_multiple_colors:
|
| 174 |
if len(y_names_list) == 1: # Solo una variable Y
|
|
|
|
| 205 |
figs.append(fig)
|
| 206 |
for fig in figs:
|
| 207 |
st.plotly_chart(fig)
|
| 208 |
+
elif chart_type == "Línea temporal":
|
| 209 |
+
if horizontal_bars:
|
| 210 |
+
fig = px.bar(data, x=y_names_list, y="X", orientation='h', barmode='stack')
|
| 211 |
+
else:
|
| 212 |
+
fig = px.bar(data, x="X", y=y_names_list, barmode='stack')
|
| 213 |
+
fig.update_traces(hovertemplate=hovertemplate)
|
| 214 |
+
if use_multiple_colors:
|
| 215 |
+
if len(y_names_list) == 1: # Solo una variable Y
|
| 216 |
+
fig.update_traces(marker_color=colors[:len(x)], marker_line_color=border_colors[:len(x)], marker_line_width=border_width)
|
| 217 |
+
else:
|
| 218 |
+
for i, trace in enumerate(fig.data):
|
| 219 |
+
trace.marker.color = colors[i % len(colors)]
|
| 220 |
+
trace.marker.line.color = border_colors[i % len(border_colors)]
|
| 221 |
+
trace.marker.line.width = border_width
|
| 222 |
+
else:
|
| 223 |
+
fig.update_traces(marker_color=color, marker_line_color=border_color, marker_line_width=border_width)
|
| 224 |
+
fig.update_layout(bargap=0.2)
|
| 225 |
|
| 226 |
# Añadir anotación para el título
|
| 227 |
if chart_type != "Donut":
|
|
|
|
| 256 |
fig.update_layout(xaxis_autorange=True, yaxis_autorange=True)
|
| 257 |
|
| 258 |
# Información adicional
|
| 259 |
+
st.write("")
|