Skip to content

Commit

Permalink
Show days without sessions on graph
Browse files Browse the repository at this point in the history
  • Loading branch information
jake158 committed Apr 4, 2024
1 parent 1efb97f commit 615e0b8
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions pomodorodiscord/src/graphs.py
Original file line number Diff line number Diff line change
@@ -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__))
Expand All @@ -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()

Expand Down

0 comments on commit 615e0b8

Please sign in to comment.