From 615e0b891442c0dffec51a11db2c117892a874ad Mon Sep 17 00:00:00 2001 From: freeram Date: Thu, 4 Apr 2024 03:08:42 -0600 Subject: [PATCH] Show days without sessions on graph --- pomodorodiscord/src/graphs.py | 56 +++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/pomodorodiscord/src/graphs.py b/pomodorodiscord/src/graphs.py index a5546d7..33eb28c 100644 --- a/pomodorodiscord/src/graphs.py +++ b/pomodorodiscord/src/graphs.py @@ -1,7 +1,8 @@ import os import sys import matplotlib.pyplot as plt -from matplotlib.ticker import MaxNLocator +import matplotlib.dates as mdates +from datetime import datetime, timedelta # Making sure you can run `python3 src/graphs.py` current_dir = os.path.dirname(os.path.abspath(__file__)) @@ -11,43 +12,66 @@ from src.utils import load_data +def generate_date_range(start_date, end_date): + """Generate a list of dates from start_date to end_date.""" + date_range = [start_date + timedelta(days=x) + for x in range((end_date - start_date).days + 1)] + return date_range + + +def fill_missing_dates(data, date_range): + """Fill missing dates in data with zero values.""" + return [data.get(date.strftime("%Y-%m-%d"), 0) for date in date_range] + + def graph_pomodoro_sessions(data): - dates = list(data.get('sessions_by_date', {}).keys()) - sessions = list(data.get('sessions_by_date', {}).values()) + original_dates = list(data.get('sessions_by_date', {}).keys()) + + # Convert string dates to datetime objects + dates = [datetime.strptime(date, "%Y-%m-%d") for date in original_dates] + + # Generate a complete date range + date_range = generate_date_range(min(dates), max(dates)) + + # Fill missing dates with zero sessions + sessions = fill_missing_dates(data['sessions_by_date'], date_range) plt.figure(figsize=(12, 8)) - plt.bar(dates, sessions, color='blue', alpha=0.5, width=1) + plt.bar(date_range, sessions, color='blue', alpha=0.5, width=1) plt.title('Pomodoro Sessions per Day') plt.xlabel('Days') plt.ylabel('Pomodoro Sessions') - - # Format x-axis to display dates properly ax = plt.gca() - ax.xaxis.set_major_locator(MaxNLocator(20)) + ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) + ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) plt.xticks(rotation=45) - plt.tight_layout() plt.show() def graph_hours_studied(data): - dates = list(data.get('seconds_by_date', {}).keys()) - seconds = list(data.get('seconds_by_date', {}).values()) - hours = [s / 3600 for s in seconds] + original_dates = list(data.get('seconds_by_date', {}).keys()) + + # Convert string dates to datetime objects + dates = [datetime.strptime(date, "%Y-%m-%d") for date in original_dates] + + # Generate a complete date range + date_range = generate_date_range(min(dates), max(dates)) + + # Fill missing dates with zero hours + hours = [seconds / 3600 for seconds in fill_missing_dates(data['seconds_by_date'], date_range)] plt.figure(figsize=(12, 8)) - plt.bar(dates, hours, color='blue', alpha=0.5, width=1) + plt.bar(date_range, hours, color='blue', alpha=0.5, width=1) plt.title('Hours Studied per Day') plt.xlabel('Days') plt.ylabel('Hours Studied') - - # Format x-axis to display dates properly ax = plt.gca() - ax.xaxis.set_major_locator(MaxNLocator(20)) + ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) + ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) plt.xticks(rotation=45) - plt.tight_layout() plt.show()