Add ability to plot saved queries using plotly, need to check performance in production, also need to improve generate_plot function

This commit is contained in:
Peter Stockings
2024-11-09 16:49:08 +11:00
parent 23def088bb
commit c7013e0eac
5 changed files with 85 additions and 13 deletions

View File

@@ -2,6 +2,8 @@ import colorsys
from datetime import datetime, date, timedelta
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.io as pio
def get_workouts(topsets):
# Ensure all entries have 'WorkoutId' and 'TopSetId', then sort by 'WorkoutId' and 'TopSetId'
@@ -439,4 +441,37 @@ def get_distinct_colors(n):
rgb = colorsys.hls_to_rgb(hue, 0.6, 0.4) # Fixed lightness and saturation
hex_color = '#{:02x}{:02x}{:02x}'.format(int(rgb[0]*255), int(rgb[1]*255), int(rgb[2]*255))
colors.append(hex_color)
return colors
return colors
def generate_plot(df, title):
"""
Analyzes the DataFrame and generates an appropriate Plotly visualization.
Returns the Plotly figure as a div string.
"""
if df.empty:
return "<p>No data available to plot.</p>"
num_columns = len(df.columns)
# Simple logic to decide plot type based on DataFrame structure
if num_columns == 1:
# Single column: perhaps a histogram or bar chart
column = df.columns[0]
if pd.api.types.is_numeric_dtype(df[column]):
fig = px.histogram(df, x=column, title=title)
else:
fig = px.bar(df, x=column, title=title)
elif num_columns == 2:
# Two columns: scatter plot or line chart
col1, col2 = df.columns
if pd.api.types.is_numeric_dtype(df[col1]) and pd.api.types.is_numeric_dtype(df[col2]):
fig = px.scatter(df, x=col1, y=col2, title=title)
else:
fig = px.bar(df, x=col1, y=col2, title=title)
else:
# More than two columns: heatmap or other complex plots
fig = px.imshow(df.corr(), text_auto=True, title=title)
# Convert Plotly figure to HTML div
plot_div = pio.to_html(fig, full_html=False)
return plot_div