Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve graph titles #115

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions analysis/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import click
import pandas
from matplotlib import pyplot
from matplotlib.dates import num2date

from analysis import click_types, utils

Expand Down Expand Up @@ -41,7 +40,7 @@ def main(from_date, from_offset, d_out):

utils.makedirs(d_out)

figs_cols = plot(by_day, by_week)
figs_cols = plot(by_day, by_week, get_plot_title(from_date, from_offset))
for fig, col in figs_cols:
f_stem = utils.slugify(col)
fig.savefig(d_out / f"{f_stem}.png")
Expand Down Expand Up @@ -77,7 +76,14 @@ def get_date_ranges_from_offset(data_frame, from_offset):
yield _DateRange(table_name, from_, to_)


def plot(by_day, by_week):
def get_plot_title(from_date, from_offset):
if from_date is not None:
return f"Event activity for the period from {utils.date_format(from_date)} to the report run date"
if from_offset is not None:
return f"The last {from_offset} days of event activity"


def plot(by_day, by_week, plot_title):
cols = by_day.columns.union(by_week.columns)
for col in cols:
fig, ax = pyplot.subplots(figsize=(15, 7))
Expand All @@ -99,8 +105,7 @@ def plot(by_day, by_week):

ax.grid(True)
ax.margins(x=0)
min_ts, max_ts = [num2date(x) for x in ax.get_xlim()]
ax.set_title(f"From {min_ts:%Y-%m-%d} to {max_ts:%Y-%m-%d}", fontsize="medium")
ax.set_title(plot_title)
ax.set_ylabel("Event Counts")
ax.set_ylim(0)
ax.legend(loc="upper right")
Expand Down
7 changes: 1 addition & 6 deletions analysis/render_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,9 @@ def b64encode(path):
return f"data:{mtype};base64, {encoded}"


def date_format(date):
"""Formats the given date as, for example, "1 January 2023"."""
return f"{date:%-d %B %Y}" # the - removes the leading zero, but not on Windows


# register template filters
ENVIRONMENT.filters["b64encode"] = b64encode
ENVIRONMENT.filters["date_format"] = date_format
ENVIRONMENT.filters["date_format"] = utils.date_format


def render_report(data):
Expand Down
5 changes: 5 additions & 0 deletions analysis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ def slugify(s):
# remove leading and trailing dashes and underscores
s = s.strip("-_")
return s.lower()


def date_format(date):
"""Formats the given date as, for example, "1 January 2023"."""
return f"{date:%-d %B %Y}" # the - removes the leading zero, but not on Windows