Plot E1RM, reps, & weight on user progress sparkline, also reduced generated svg size by half

This commit is contained in:
Peter Stockings
2023-12-09 12:42:27 +11:00
parent dd093e3819
commit cc44591eea
2 changed files with 63 additions and 21 deletions

41
db.py
View File

@@ -490,21 +490,30 @@ class DataBase():
# Extracting values and calculating value ranges for SVG dimensions
estimated_1rm = [t['estimated_1rm'] for t in topsets]
repetitions = [t['repetitions'] for t in topsets]
weight = [t['weight'] for t in topsets]
start_dates = [t['start_date'] for t in topsets]
min_date, max_date = min(start_dates), max(start_dates)
min_value, max_value = min(estimated_1rm), max(estimated_1rm)
min_e1rm, max_e1rm = min(estimated_1rm), max(estimated_1rm)
min_reps, max_reps = min(repetitions), max(repetitions)
min_weight, max_weight = min(weight), max(weight)
# Calculate viewBox dimensions
date_range = max_date - min_date
value_range = max_value - min_value
vb_width, vb_height = date_range.days, value_range
e1rm_range = max_e1rm - min_e1rm
reps_range = max_reps - min_reps
weight_range = max_weight - min_weight
vb_width, vb_height = date_range.days, e1rm_range
vb_width *= 200 / vb_width # Scale to 200px width
vb_height *= 75 / vb_height # Scale to 75px height
# Scale estimated_1rm values for SVG plotting
estimated_1rm_scaled = [((value - min_value) / value_range) * vb_height for value in estimated_1rm]
precision = 3
estimated_1rm_scaled = [round(((value - min_e1rm) / e1rm_range) * vb_height, precision) for value in estimated_1rm]
repetitions_scaled = [round(((value - min_reps) / reps_range) * vb_height, precision) for value in repetitions]
weight_scaled = [round(((value - min_weight) / weight_range) * vb_height, precision) for value in weight]
total_span = date_range.days or 1
relative_positions = [(date - min_date).days / total_span for date in start_dates]
relative_positions = [round((date - min_date).days / total_span, precision) for date in start_dates]
# Convert relative positions and scaled estimated 1RM values to numpy arrays
x = np.array(relative_positions)
@@ -519,13 +528,31 @@ class DataBase():
# 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)
estimated_1rm_points = zip(estimated_1rm_scaled, relative_positions, messages)
repetitions_points = zip(repetitions_scaled, relative_positions, messages)
weight_points = zip(weight_scaled, relative_positions, messages)
estimated_1rm = {
'label': 'E1RM',
'color': '#2ca02c',
'points': list(estimated_1rm_points)
}
repetitions = {
'label': 'Reps',
'color': '#388fed',
'points': list(repetitions_points)
}
weight = {
'label': 'Weight',
'color': '#bd3178',
'points': list(weight_points)
}
# Return exercise data with SVG dimensions and data points
return {
'exercise_name': topsets[0]['exercise_name'],
'vb_width': vb_width,
'vb_height': vb_height,
'data_points': list(data_points),
'plots': [estimated_1rm, repetitions, weight],
'best_fit_points': list(best_fit_points),
}