mirror of
https://github.com/Crocmagnon/checkout.git
synced 2024-11-22 16:18:03 +01:00
Separate data generation from plot production
This commit is contained in:
parent
883fc8e8f9
commit
212b0d9713
1 changed files with 33 additions and 25 deletions
|
@ -49,13 +49,7 @@ class ReportsView(ProtectedViewsMixin, TemplateView):
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def get_products_plot(self, products: ProductQuerySet):
|
def get_products_plot(self, products: ProductQuerySet):
|
||||||
labels = []
|
labels, sold, turnover = self.get_products_data_for_plot(products)
|
||||||
sold = []
|
|
||||||
turnover = []
|
|
||||||
for product in products:
|
|
||||||
labels.append(product.name)
|
|
||||||
sold.append(product.sold)
|
|
||||||
turnover.append(product.turnover / 100)
|
|
||||||
|
|
||||||
fig, ax1 = plt.subplots()
|
fig, ax1 = plt.subplots()
|
||||||
color = "tab:orange"
|
color = "tab:orange"
|
||||||
|
@ -75,7 +69,38 @@ class ReportsView(ProtectedViewsMixin, TemplateView):
|
||||||
image_data.seek(0)
|
image_data.seek(0)
|
||||||
return image_data.getvalue()
|
return image_data.getvalue()
|
||||||
|
|
||||||
|
def get_products_data_for_plot(self, products):
|
||||||
|
labels = []
|
||||||
|
sold = []
|
||||||
|
turnover = []
|
||||||
|
for product in products:
|
||||||
|
labels.append(product.name)
|
||||||
|
sold.append(product.sold)
|
||||||
|
turnover.append(product.turnover / 100)
|
||||||
|
return labels, sold, turnover
|
||||||
|
|
||||||
def by_hour_plot(self, baskets):
|
def by_hour_plot(self, baskets):
|
||||||
|
counts, labels, turnovers = self.get_by_hour_data_for_plot(baskets)
|
||||||
|
fig, ax1 = plt.subplots()
|
||||||
|
hours_in_day = 24
|
||||||
|
color = "tab:orange"
|
||||||
|
ax1.bar(labels, counts, width=1 / hours_in_day, color=color)
|
||||||
|
ax1.tick_params(axis="x", rotation=15)
|
||||||
|
ax1.set_ylabel(_("Basket count by hour"), color=color)
|
||||||
|
|
||||||
|
color = "tab:blue"
|
||||||
|
ax2 = ax1.twinx()
|
||||||
|
ax2.bar(labels, turnovers, width=1 / (hours_in_day * 2), color=color)
|
||||||
|
ax2.set_ylabel(_("Turnover by hour"), color=color)
|
||||||
|
plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter("%.2f€"))
|
||||||
|
|
||||||
|
fig.tight_layout()
|
||||||
|
image_data = StringIO()
|
||||||
|
fig.savefig(image_data, format="svg")
|
||||||
|
image_data.seek(0)
|
||||||
|
return image_data.getvalue()
|
||||||
|
|
||||||
|
def get_by_hour_data_for_plot(self, baskets):
|
||||||
current: datetime.datetime = baskets[0].created_at
|
current: datetime.datetime = baskets[0].created_at
|
||||||
current = current.replace(minute=0, second=0, microsecond=0)
|
current = current.replace(minute=0, second=0, microsecond=0)
|
||||||
end: datetime.datetime = baskets[-1].created_at
|
end: datetime.datetime = baskets[-1].created_at
|
||||||
|
@ -99,21 +124,4 @@ class ReportsView(ProtectedViewsMixin, TemplateView):
|
||||||
counts.append(count)
|
counts.append(count)
|
||||||
turnovers.append(turnover)
|
turnovers.append(turnover)
|
||||||
current = end_slot
|
current = end_slot
|
||||||
fig, ax1 = plt.subplots()
|
return counts, labels, turnovers
|
||||||
hours_in_day = 24
|
|
||||||
color = "tab:orange"
|
|
||||||
ax1.bar(labels, counts, width=1 / hours_in_day, color=color)
|
|
||||||
ax1.tick_params(axis="x", rotation=15)
|
|
||||||
ax1.set_ylabel(_("Basket count by hour"), color=color)
|
|
||||||
|
|
||||||
color = "tab:blue"
|
|
||||||
ax2 = ax1.twinx()
|
|
||||||
ax2.bar(labels, turnovers, width=1 / (hours_in_day * 2), color=color)
|
|
||||||
ax2.set_ylabel(_("Turnover by hour"), color=color)
|
|
||||||
plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter("%.2f€"))
|
|
||||||
|
|
||||||
fig.tight_layout()
|
|
||||||
image_data = StringIO()
|
|
||||||
fig.savefig(image_data, format="svg")
|
|
||||||
image_data.seek(0)
|
|
||||||
return image_data.getvalue()
|
|
||||||
|
|
Loading…
Reference in a new issue