diff --git a/db.py b/db.py index 24b7e72..db953ad 100644 --- a/db.py +++ b/db.py @@ -1,5 +1,6 @@ import os import psycopg2 +import numpy as np from psycopg2.extras import RealDictCursor from datetime import datetime from urllib.parse import urlparse @@ -505,6 +506,17 @@ class DataBase(): total_span = date_range.days or 1 relative_positions = [(date - min_date).days / total_span for date in start_dates] + # Convert relative positions and scaled estimated 1RM values to numpy arrays + x = np.array(relative_positions) + y = np.array(estimated_1rm_scaled) + + # Calculate the slope (m) and y-intercept (b) of the line of best fit + m, b = np.polyfit(x, y, 1) + + # Generate points along the line of best fit + y_best_fit = [m * xi + b for xi in x] + best_fit_points = zip(y_best_fit, relative_positions) + # Create messages and zip data for SVG plotting messages = [f'{t["repetitions"]} x {t["weight"]}kg ({t["estimated_1rm"]}kg E1RM) on {t["start_date"].strftime("%d %b %y")}' for t in topsets] data_points = zip(estimated_1rm_scaled, relative_positions, messages) @@ -514,5 +526,6 @@ class DataBase(): 'exercise_name': topsets[0]['exercise_name'], 'vb_width': vb_width, 'vb_height': vb_height, - 'data_points': list(data_points) + 'data_points': list(data_points), + 'best_fit_points': list(best_fit_points), } \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index afa1e0f..808f5ae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ flask-htmx==0.2.0 python-dateutil==2.8.2 minify-html==0.10.3 jinja2-fragments==0.3.0 -Werkzeug==2.2.2 \ No newline at end of file +Werkzeug==2.2.2 +numpy==1.19.5 \ No newline at end of file diff --git a/templates/partials/sparkline.html b/templates/partials/sparkline.html index 43220c0..9265280 100644 --- a/templates/partials/sparkline.html +++ b/templates/partials/sparkline.html @@ -1,5 +1,5 @@ -{% set fill = "#dcfce7" %} -{% set stroke = "#bbf7d0" %} +{% set fill = "#2ca02c" %} +{% set stroke = "#2ca02c" %} {% set stroke_width = 4 %} {% set margin = 2 %} @@ -11,6 +11,14 @@ {% endfor %} {% endmacro %} +{% macro path_best_fit(best_fit_points, vb_height) %} + {% for value, position in best_fit_points %} + {% set x = (position * vb_width)+margin %} + {% set y = (vb_height - value)+margin %} + {% if loop.first %}M{{ x }} {{ y }}{% else %} L{{ x }} {{ y }}{% endif %} + {% endfor %} +{% endmacro %} + {% macro circles(data_points, vb_height) %} {% for value, position, message in data_points %} {% set x = (position * vb_width)+margin %} @@ -22,7 +30,7 @@ on mouseout add .hidden to #popover-{{ unique_id }}"> - + {% endfor %} {% endmacro %} @@ -38,7 +46,8 @@ - + + {{ circles(data_points, vb_height) }}