From 4affdd7b8679b7fc97f5ef08d7fb4dd0d5b4dcf8 Mon Sep 17 00:00:00 2001 From: "friedjof@noweck.info" Date: Tue, 22 Oct 2024 18:10:30 +0200 Subject: [PATCH] fix: charts --- analytics/tasks.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/analytics/tasks.py b/analytics/tasks.py index 1c3f8e0..fe671b2 100644 --- a/analytics/tasks.py +++ b/analytics/tasks.py @@ -133,12 +133,11 @@ def map_weekday_hour_diagram() -> str: heatmap_data = [[0 for _ in range(24)] for _ in range(7)] # 7 days, 24 hours for entry in weekday_hour_counts: - heatmap_data[entry['weekday'] - 1][entry['hour']] = entry[ - 'total'] # Weekday is 1-based (1=Sunday, 7=Saturday) + heatmap_data[entry['weekday'] - 1][entry['hour']] = entry['total'] # Weekday is 1-based (1=Sunday, 7=Saturday) # Create the heatmap fig, ax = plt.subplots(figsize=(12, 7)) - cax = ax.matshow(heatmap_data, cmap='YlGnBu') + cax = ax.matshow(heatmap_data, cmap='summer') # Set axis labels ax.set_xticks(range(24)) @@ -149,6 +148,11 @@ def map_weekday_hour_diagram() -> str: # Add color bar fig.colorbar(cax) + # Add text annotations + for i in range(7): + for j in range(24): + ax.text(j, i, str(heatmap_data[i][j]), va='center', ha='center', color='black') + ax.set_xlabel('Hour of the Day') ax.set_ylabel('Weekday') ax.set_title('Average Number of Running Machines per Weekday and Hour') @@ -197,6 +201,9 @@ def last_3_weeks_of_running_machines() -> str: plt.xticks(rotation=45) plt.tight_layout() + # Set date format on x-axis + ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d (%a)')) + buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) @@ -254,7 +261,7 @@ def last_3_weeks_of_machine_status() -> str: plt.tight_layout() # Set date format on x-axis - ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) + ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d (%a)')) # Save the plot to a buffer buf = io.BytesIO() @@ -285,13 +292,14 @@ def used_machines_per_type_and_building() -> str: # Create the heatmap fig, ax = plt.subplots() - heatmap_data = [[data[building][machine_number] for machine_number in machine_numbers] for building in buildings] + heatmap_data = [[data[building][machine_number] for machine_number in machine_numbers] for building in + buildings] # if Invalid shape (0,) for image data if len(heatmap_data) == 0: heatmap_data = [[0]] - cax = ax.matshow(heatmap_data, cmap='YlGnBu') + cax = ax.matshow(heatmap_data, cmap='summer') # Set axis labels ax.set_xticks(range(len(machine_numbers))) @@ -302,6 +310,11 @@ def used_machines_per_type_and_building() -> str: # Add color bar fig.colorbar(cax) + # Add text annotations + for i in range(len(buildings)): + for j in range(len(machine_numbers)): + ax.text(j, i, str(heatmap_data[i][j]), va='center', ha='center', color='black') + ax.set_xlabel('Machine Number') ax.set_ylabel('Building') ax.set_title('Used Machines per Number and Building')